Home > database >  How can I update the user information using files in c
How can I update the user information using files in c

Time:12-15

How can I update the user information using files in c 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

    #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; 

}

CodePudding user response:

The first task is to identify the steps needed to accomplish your stated goal. Most of the comments point that out. Also from comments it is indicated tasks can be implemented in many different ways. The following code is not complete, but the parts that are there do compile and run. Its purpose is to provide a primer toward your stated goal.

"How can I update the user information using files in c The content of the file"

The following assumes a file ".\\names.txt", in the local directory, with contents resembling your example:

Belick 44 Miami
Sara 21 Boston
John 24 Chicago

Change as needed.
Note, use of struct objects come earlier in learning C than do using linked lists, so this example uses struct as I had no indication that you would be comfortable with linked lists. However, if you needed to expand your stated goals to include removing or adding records, then it would be worth your effort to consider using Linked Lists rather than basic struct objects as memory management is more intrinsic to lists than it would be for struct objects when adding or deleting records.

#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;
}
  

For further help:

  • Related