【问题标题】:How can I update the user information using files in c如何使用 c 中的文件更新用户信息
【发布时间】:2022-01-17 18:32:13
【问题描述】:

如何使用 c 中的文件更新用户信息 文件内容: Belick 44 迈阿密 萨拉 21 波士顿 约翰 24 芝加哥 姓名年龄城市

我想更改 Sara 的年龄 ex:55 因此,文件将被更新,如图所示 Belick 44 迈阿密 萨拉 55 波士顿 约翰 24 芝加哥

    #include <stdio.h>
    #include <string.h>
    int main (void) {
       FILE *ptr; 
     int age;`enter code here`
     char name[50];
     char n[50];
     int newAge;
     char city[50];
       ptr = fopen("update.txt", "r+");
       if (ptr==NULL) {
           printf("Unable to open the file...\n");
       }
     /*
    The content of the file: 
              Belick 44 Miami
              Sara 21 Boston
              John 24 Chicago
    
              name age city
      I would like to change Sara's age ex: 55
      so, the file will be updated as shown 
              Belick 44 Miami
              Sara 55 Boston
              John 24 Chicago
    */
       else 
       { 
           do {
           printf("your name: "); 
           scanf("%s", n); 
           printf("Enter your new age: "); 
           scanf("%d", &newAge);
           fscanf(ptr,"%s %d %s", name, &age, city);
           age = newAge;
           fprintf(ptr,"%s %d %s\n", name, age, city);
           }
           while(strcmp(n, name)!=0);
           fclose(ptr);
       }
        return 0; 

}

【问题讨论】:

  • 用可变长度数据(如文本)更新文件总是很重要的。一种相对简单的方法是创建一个结构来保存文件中的每条记录。然后将所有文件读入内存,修改内存中想要的记录,然后从头开始重新创建文件并将数据写回。
  • 如果你真的不想按照@Someprogrammerdude 的建议去做,你可以查看fseek 并跟踪你在文件中的位置。你看过this question吗?
  • 按照@BG_Cw 的建议来回搜索将起作用,但如果您替换的文本与文件中已经存在的文本长度完全相同。如果新文本较短,则旧文本的一部分仍将在文件中。如果新文本较长,那么您将覆盖其他不相关的文本。
  • 因为您的文件内容似乎由一致的信息块组成,每个信息块的每条记录都具有相同的#字段,因此更新文件不必很复杂。步骤可能是 1)打开文件以将内容读入内存(结构数组或列表。) 2)关闭文件。 3)在内存(结构实例或列表节点)中搜索名称。 4)修改该名称的年龄。 5)打开相同的文件进行写入(而不是追加)并将内存写入文件。 6)关闭文件。如果您被介绍给链表,那我认为最能满足您的需求。否则,一个结构数组就可以了。

标签: c file


【解决方案1】:

首要任务是确定实现既定目标所需的步骤。大多数cmets都指出了这一点。 cmets 还指出,任务可以以许多不同的方式实现。以下代码不完整,但其中的部分确实可以编译和运行。其目的是为您的既定目标提供入门知识。

“如何使用c文件的内容更新用户信息”

以下假设在本地目录中有一个文件".\\names.txt",其内容类似于您的示例:

Belick 44 Miami
Sara 21 Boston
John 24 Chicago

根据需要进行更改。
注意,在学习 C 时,使用 struct 对象比使用链表更早,所以这个例子使用了 struct,因为我没有迹象表明你会适应linked lists。但是,如果您需要扩展您的既定目标以包括删除或添加记录,那么值得您考虑使用链接列表而不是基本结构对象,因为内存管理对于列表来说比结构对象更内在添加或删除记录。

#define MAX_PATH 260

typedef struct {
    char name[80];
    int age;
    char city[80];
} record_s;

record_s * read_records(const char *filename, int count);
void line_count(const char *fn, int *count);

int main(int argc, char *argv[])
{
    //read file into memory (array of struct)
    record_s *records  =  read_records(".\\names.txt", &count);
    //write user prompt to know what record field to edit (name, age, city)
    //write scan statements to read in user input choices
    //create function to pass pointer to struct object to search and edit field
    //create function to write updated records to same file
    //
    //finally, free memory created in first function  
    free(records);
    
    return 0;
}

//read file into 'count' instances of struct
record_s * read_records(const char *filename, int *count)
{
    int lines;
    int i = 0;
    char line[MAX_PATH] = {0};
    char *tok = NULL;
    char *delim = {" \n\t"};
    record_s *recs = NULL;
    
    FILE *fp = fopen(filename, "r");
    if(fp)
    {
        //get count of lines in file
        line_count(filename,  &lines);
        //create count instances of struct
        recs = malloc(sizeof(*recs)*lines);
        if(recs)
        {   //populate instances of struct with record fields
            while(fgets(line, sizeof line, fp))
            {
                tok = strtok(line, delim);
                if(tok)
                {
                    strcpy(recs[i].name, tok);
                    tok = strtok(NULL, delim);
                    if(tok)
                    {
                        recs[i].age = atoi(tok);
                        tok = strtok(NULL, delim);
                        if(tok)
                        {
                            strcpy(recs[i].city, tok);
                        }
                    }
                }
                i++;
            }
        }
        fclose(fp);
        *count = lines;
    }
    return recs;
}
 
//get count of lines in file
void line_count(const char *fn, int *count)
{
    int cnt = 0;
    char line[MAX_PATH] = {0};
    FILE *fp = fopen(fn, "r");
    if(fp)
    {
        while(fgets(line, sizeof line, fp))
        {
            cnt++;
        }
        fclose(fp);            
    }
    *count = cnt;
}
  

如需进一步帮助:

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2016-10-03
    相关资源
    最近更新 更多