0

I am creating this change function. void cha(struct cont x);, It will ask for the lname then check if its in the file. After that, It will edit.Asks for the lname and fname again. It works but it writes at the bottom of the file.

struct cont 
{
 char lname[20];
 char fname[20];
}s;

void cha(struct cont x)
    {
        FILE *fp;
        char lname[20];
        int flag=0;
        fp=fopen("database.dat","a+");
        if(fp==NULL)
        {
            printf("file error");
        }
        else
        {
            printf("\nenter lname: ");
            gets(lname);

            while(fscanf(fp,"%s %s",x.lname,x.fname)==2)
            {
                if(strcmp(lname,x.lname)==0)
                {
                    printf("enter lname: ");
                    gets(x.lname);
                    printf("enter fname: ");
                    gets(x.fname);

                    fseek(fp,-sizeof(x),SEEK_CUR);
                    fprintf(fp,"%s %s\n",x.lname,x.fname);
                    flag=1;
                    break;

                }
            }

            if(flag==1)
            {
                printf("success!");
            }

            else
            {
                printf("data not found.");
            }
        }
        fclose(fp);
    }
  • 4
    **Never use `gets()`**!! [It is dangerous](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) as it does **not** prevent [buffer overflow](http://en.wikipedia.org/wiki/Buffer_overflow)s – Spikatrix Jan 11 '15 at 08:54
  • Also, end your `printf` format strings with `\n` or use `fflush` – Basile Starynkevitch Jan 11 '15 at 08:55
  • 1
    Did you consider using [sqlite](http://sqlite.org/) ? – Basile Starynkevitch Jan 11 '15 at 08:56
  • 2
    I bet checking the results of your write-operations on a read-only file would be rather.... revealing. – WhozCraig Jan 11 '15 at 08:56
  • this line: 'fp=fopen("database.dat","a+");' says to append to the file on all writes. more likely the line should be: 'fp=fopen("database.dat","w+");' -or- 'fp=fopen("database.dat","r+");' – user3629249 Jan 11 '15 at 09:25

2 Answers2

4
fp=fopen("database.dat","r");

You have opened the file in read mode and you are trying to write to the file

fprintf(fp,"%s %s\n",x.lname,x.fname);

Use a+ to open the file in append mode.

gets() is no more a standard and use fgets() which takes care of buffer overflow.

Man says:

a+

Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

Just write it to another file

FILE *newFile = fopen("newDatabase.dat","w");

if(strcmp(lname,x.lname)==0)
{
    //scan data to tmp var
    fprintf(newFile ,"%s %s\n",x2.lname,x2.fname);
}
else
{
    //write original var
    fprintf(newFile ,"%s %s\n",x.lname,x.fname);

}

you can then change the name of the new file to overwrite the old one if its important

antonpuz
  • 3,256
  • 4
  • 25
  • 48