【问题标题】:C: use of fgets() with fileC:将 fgets() 与文件一起使用
【发布时间】:2015-06-11 18:39:29
【问题描述】:

晚上好 StackOverFlow。 我输入了一个使用列表和文件来管理地址簿的代码,现在我遇到了函数 fgets() 的问题。 在过程 print_file() 中,我使用 fgets() 从文件中读取一行并将其存储到最多 200 个字符的字符串中。调试我的代码,我注意到该文件已普及,但在我使用 fgets() 提取该行以将其存储到字符串中后,字符串结果为空。下面我发布了过程 print_file() 和过程 filling() ,它使用列表的节点来推广文件。谁能帮帮我?

void print_file(FILE* myfile, int num){
    char contact[200];
    int i=0;

    while(i<num){ 
        fgets(contact, 200, myfile);
        i++;
        printf("%d) %s", i, contact);
    }
    fclose(myfile);
}


void filling(FILE* myfile){
    list_struct* l; node_struct* new_node;
    node_struct* tempnode;
    t_contact temp;
    int n_contacts=0, i=0;

    printf("\n*****The file will be filled by a list*****\n");

    l=made_list(); //creating new list
    if(l!=NULL){
        printf("\nList successfully created\n");
    }
    printf("How many contacts do you want to add to the list?   ");
    scanf("%d", &n_contacts);

    for(i=1;i<=n_contacts;i++){
        printf("NAME > ");
        scanf("%s", temp.name);
        printf("SURNAME > ");
        scanf("%s", temp.surname);
        printf("TELEPHONE > ");
        scanf("%s", temp.telephone);
        printf("E-MAIL > ");
        scanf("%s", temp.email);
        printf("\n");
        new_node = made_node(temp); //creating new node
        add_node(l, new_node);
    }// end for --> the list has been popularized

    tempnode = l->head;
    while(tempnode!=NULL){
        fprintf(myfile, "%s %s %s %s\n", tempnode->content.name,   tempnode->content.surname, tempnode->content.telephone, tempnode->content.email);
        tempnode=tempnode->next;
    }
    print_file(myfile, n_contacts);
    fclose(myfile);

}

【问题讨论】:

  • 您需要fflushrewindfseek。也 fclose(myfile); filling : fclose 呼叫两次。

标签: c file fgets


【解决方案1】:

按照您编写的方式,fgets() 将被调用以从文件末尾开始读取。您可以使用 fseek() 定位到文件的开头。

【讨论】:

    【解决方案2】:

    您需要将文件指针重新定位在文件的开头以便从中读取。正如您现在所拥有的那样,当您调用 print_file 时,它​​位于文件的末尾。

    rewind(fp) 应该这样做。

    编辑:

    根据您的评论,我会提出类似的建议 另请注意,您在print_file 内调用了两次fclose() 和 再打一次print_file,这肯定不好。而是从 print_file 函数中删除 fclose,因为名称暗示它只打印。

    /* 
     * extracts a row from the file and then puts it into 
     * a string and prints the result
     * @param[in] myFile file pointer to file opened in rw access
     * @param[in] num number of rows in the file 
     */
    void print_file(FILE* myfile, int num)
    {
      char contact[200];  // allocate on stack
      int i = 0;
    
      if ( myfile != NULL )  // check argument to function
      {
        fflush(myfile);
        rewind(myfile);
    
        for (i = 0; i < num; ++i)
        {
          if (fgets(contact, sizeof(contact), myfile) != NULL);
          {
            printf("%d) %s", i + 1, contact);
          }
        }
      }
    }
    

    或者,您可以通过读取每一行而不关心num来从文件开头读取到文件结尾:

      int i = 1;
      while (fgets(contact, sizeof(contact), myfile) != NULL);
      {
        printf("%d) %s", i++, contact);
      }
    

    【讨论】:

    • void print_file(FILE* myfile, int num){ char *contact; int i=0; fflush(myfile); rewind(myfile); while(i&lt;num){ //extracts a row from the file and then puts it into a string and prints the result free(contact); contact=calloc(200, sizeof(char)); fgets(contact, 200, myfile); i++; printf("%d) %s", i, contact); contact=NULL; } fclose(myfile); }我写对了吗?
    • 您的代码中有内存泄漏。在 printf 之后contact = NULL
    • 我在 printf 之后取消了 contact=NULL 但在 fgets() 之后字符串 contact 仍然为空
    • 如何打开文件?您检查文件是否已创建?
    • 你需要以读写模式打开文件,你只能用 "w" 以写模式打开它,而不是写 "rw" 或者如果你在 windows "rwt" 上
    猜你喜欢
    • 1970-01-01
    • 2020-05-18
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多