繁体   English   中英

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

[英]How to read a .txt file from user input in C

我知道如何对程序进行核对以接收文件,但是当我尝试使用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);
    }

这是从文件读取并打印出来的最简单方法。 我假设您要打印文件或对其进行处理。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM