0

I am currently using MinGW in Windows XP.

I coded a program that takes user's input and puts them into .txt file through

typedef struct data_base{
    char name[254];
    int age;
    int postalcode;
    struct data_base *next;
}person;

I was wondering if there is a way to edit the data of the .txt file.

For example, in the .txt file, I have 3 sets of data based on user's input:

Steven    //name
19        //age
1100      //postal code
Jack 
24
2203
Mary
21
0109

I will ask the user which set of data he wishes to edit. Then after taking the edited data, I want that data to be overwritten in the .txt file at that specific set which user selected.


#include <stdio.h>
#include <stdlib.h>

typedef struct data_base{
    char name[254];
    int age;
    int postalcode;
    struct data_base *next;
}person;

void read()
{
person *curr[20];
int count = 0;
FILE *f;
int editchoice = 0;

f = fopen("personfile.txt","r+");
// Read the data in the file based on user's input

//Display the names: 1. Steven 2.Jack  3.Mary
printf("Editing Whose Data?: \n");
scanf("%d",&editchoice);    

printf("New name: \n");
scanf("%s",&curr[editchoice]->name);
fprintf(f,"%s\n",curr[editchoice]->name);

printf("New age: \n");
scanf("%d",&curr[editchoice]->age);
fprintf(f,"%d\n",curr[editchoice]->age);

printf("New name: \n");
scanf("%d",&curr[editchoice]->postalcode);
fprintf(f,"%d\n",curr[editchoice]->postalcode);

}

I expected the data to be overwritten but it isn't happening. (Sorry for being a beginner.)

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
user2882762
  • 35
  • 1
  • 4
  • 1
    Can you show the codes that you've tried? – Crowman Oct 15 '13 at 13:46
  • Why do you expect the file to be updated? You are not updating it. In order to update the file, you would need to position the file to the record to be updated and overwrite it using `fwrite`. – Carey Gregory Oct 15 '13 at 14:41

2 Answers2

0

Yes you are almost there.

For me you have two choices. Or you change that file in memory (in the list) and after you dump the list into the file. Or you change the file and you reread the file into memory (to that list).

Or

You can change the data in the file by positioning the file cursor at the beginning of the data you want to change and changing it, nevertheless you should also update the data in memory.

My approach to that problem would be:

write a function that giving an "id" position a block of person in the file, this way you can read and write people data to the file.

  • Since the data is variable length, you would have to overwrite the record to be changed and every record after that. – Carey Gregory Oct 15 '13 at 14:02
  • Yes you are right, nevertheless I see that char name[254]; int age; int postalcode; name will hold at maximum 254 bytes (chars). All data in that block has a maximum length of 254 + 4 + 4 = 258 bytes. the chars that are not used can be put with '\0' characters. And that you have block of 258 bytes. – Nuno Martins Oct 15 '13 at 14:09
  • Yes, you could use a fixed record length to avoid having to rewrite most of the file. That's a design decision involving a tradeoff between disk space and performance. – Carey Gregory Oct 15 '13 at 14:13
  • Now depends what will be his approach. Data should be consistent in storage and in memory. – Nuno Martins Oct 15 '13 at 14:15
  • I decided to apply your idea and do : 1) open the file and store all data of the file into linked list 2) close the file 3) re-open the file, but now with "w+", 4) Get new data 5) close the file 6) fprintf all data of linked list and the new data into the file. – user2882762 Oct 15 '13 at 16:22
0

you may read from a file, then write results into another file.

read record by record,

if record has the person you wanna change then change

then

dump records as you read them into a tmp file, this will include the changed record when occurred.

then close both files, remove old file, rename the tmp file.

Community
  • 1
  • 1
aah134
  • 860
  • 12
  • 25
  • What if the file contains millions of records? Rewriting millions of records to change only one is very expensive. – Carey Gregory Oct 15 '13 at 14:14
  • I agree with you, however its a good start for a beginner. especially readying and writing on the same file could cause problem if not done carefully – aah134 Oct 15 '13 at 14:18