gpt4 book ai didi

c - 如何从 C 中的用户输入读取 .txt 文件

转载 作者:行者123 更新时间:2023-11-30 14:57:38 25 4
gpt4 key购买 nike

我知道如何硬核程序来接收文件,但是当我使用 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 加载此列表并将其加载到双向链表中。然后返回到为用户提供选项的菜单。然后可以对双向链接列表进行操作,例如添加学生、操作数据、删除、保存和打印当前名册。由于我从 parsefile 收到 new.txt,因此在该程序中使用 gdb 时遇到问题。

New.txt 内容示例。 (只是名字、姓氏、身份证号、年龄)

贝琳达之家 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);
}

最佳答案

这是读取文件并打印的最简单方法。我假设您想打印该文件或对其执行某些操作。

int c;
FILE *file;
file = fopen("test.txt", "r");
if (file) {
while ((c = getc(file)) != EOF){
putchar(c);
}
fclose(file);
}

关于c - 如何从 C 中的用户输入读取 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com