【发布时间】:2017-05-05 03:53:11
【问题描述】:
我知道如何硬核程序来接收文件,但是当我尝试使用 scanf 的类似策略时,没有任何反应。我的意思是我有一个错误检查,可以查看它是否存在以及它是否具有正确的格式,但是每次我输入文件名时,它都不会在 scanf 下方打印 printf 语句。这是为什么?我还发现我正在打开文件,但 while 语句是无限的。这没有任何意义。我尝试了下面显示的另一种解决方案,但结果相同。
void parseFile(struct student_record_node** head)
{
FILE*input;
const int argCount = 4;
char filename[100]="";
const char rowformat[] = "%20s %20s %d %d";
struct student_record record;
struct student_record_node* node = NULL;
printf("\n Please Enter the FULL Path of the .txt file you like to load. \n");
scanf("%s",filename);
input = fopen(filename, "r");
printf("I am here");
if(input == NULL)
{
printf("Error: Unable to open file.\n");
exit(EXIT_FAILURE);
}
while(!feof(input))
{
/* creating blank node to fill repeatedly until end of document*/
memset(&record, 0, sizeof(struct student_record));
if(fscanf(input, rowformat, record.first_name_,record.last_name_,&record.student_id_,&record.student_age_) != argCount)
{
continue;
}
/* set node into the doubly linked list */
node = student_record_allocate();
/* copies values from the blank node reading from document into node in my linked list */
strcpy(node->record_->first_name_, record.first_name_);
strcpy(node->record_->last_name_, record.last_name_);
node->record_->student_id_ = record.student_id_;
node->record_->student_age_ = record.student_age_;
/* check if node right after absolute head is empty if so fills it */
if(*head == NULL)
{
*head = node;
}
else
{
printf(" stuck in loop\n");
/* if current isn't null start linking the node in a list */
appendNode(head,node);
}
}
fclose(input);
printf(" end of parsefile");
}
当我到达 parsefile() 函数并输入 NEW.txt 时,它的格式正确,并且与程序本身位于同一文件夹中。我知道当我输入一个不存在或为空的 .txt 文件时,我的检查正在发挥作用,它会像应有的那样被捕获。
预期的行为是程序应该从 new.txt 加载此列表并将其加载到双向链表中。然后返回给用户选项的菜单。然后可以对双向链接列表进行操作,例如添加学生操作数据、删除、保存和打印当前名册。我在这个程序中使用 gdb 时遇到了麻烦,因为我从 parsefile 收到了 new.txt。
New.txt 内容示例。 (它只是名字,姓氏,ID,年龄)
贝琳达之家 345 50
斯科特皇冠 456 18
失败的解决方案:使用 fgetc 代替 feof
int c = fgetc(input);
while(c != EOF)
{
printf("\n in loop \n");
/* creating blank node to fill repeatedly until end of document*/
memset(&record, 0, sizeof(struct student_record));
if(fscanf(input, rowformat, record.first_name_,record.last_name_,&record.student_id_,&record.student_age_) != argCount)
{
continue;
}
/* set node into the doubly linked list */
node = student_record_allocate();
/* copies values from the blank node reading from document into node in my linked list */
strcpy(node->record_->first_name_, record.first_name_);
strcpy(node->record_->last_name_, record.last_name_);
node->record_->student_id_ = record.student_id_;
node->record_->student_age_ = record.student_age_;
/* check if node right after absolute head is empty if so fills it */
if(*head == NULL)
{
*head = node;
}
else
{
printf(" stuck in loop\n");
/* if current isn't null start linking the node in a list */
appendNode(head,node);
}
c = fgetc(input);
}
【问题讨论】:
-
因为你不读文件。你只打开它
-
这就是全部代码吗?那只是打开一个文件。阅读代码在哪里?
-
该文件不在程序文件夹中,而是在当前工作目录中。
-
“程序什么也不做。我只是得到一个空行,它没有返回菜单”。你可以做得比这更好。使用调试器跟踪程序的执行。
-
scanf("%s",filename);从标准输入读取而不是从命令行读取。也就是说,你需要先运行你的程序,然后输入New.txt。
标签: c user-input scanf