darkpbj
darkpbj

Reputation: 2992

stdio data from write not making it into a file

I'm having a problem with using stdio commands for manipulating data in a file. I short, when I write data into a file, write returns an int indicating that it was successful, but when I read it back out I only get the old data.

Here's a stripped down version of the code:

fd = open(filename,O_RDWR|O_APPEND);

struct dE *cDE = malloc(sizeof(struct dE));

//Read present data
printf("\nreading values at %d\n",off);
printf("SeekStatus <%d>\n",lseek(fd,off,SEEK_SET));
printf("ReadStatus  <%d>\n",read(fd,cDE,deSize));

printf("current Key/Data <%d/%s>\n",cDE->key,cDE->data);

printf("\nwriting new values\n");
//Change the values locally 
cDE->key  = //something new
cDE->data = //something new

//Write them back
printf("SeekStatus  <%d>\n",lseek(fd,off,SEEK_SET));
printf("WriteStatus <%d>\n",write(fd,cDE,deSize));

//Re-read to make sure that it got written back
printf("\nre-reading values at %d\n",off);
printf("SeekStatus <%d>\n",lseek(fd,off,SEEK_SET));
printf("ReadStatus  <%d>\n",read(fd,cDE,deSize));

printf("current Key/Data <%d/%s>\n",cDE->key,cDE->data);

Furthermore, here's the dE struct in case you're wondering:

struct dE {
    int key;
    char data[DataSize];
};

This prints:

reading values at 1072
SeekStatus <1072>
ReadStatus  <32>
current Key/Data <27/old>

writing new values
SeekStatus  <1072>
WriteStatus <32>

re-reading values at 1072
SeekStatus <1072>
ReadStatus  <32>
current Key/Data <27/old>

Upvotes: 0

Views: 78

Answers (1)

William Morris
William Morris

Reputation: 3684

Delete |O_APPEND from the open call.

Upvotes: 1

Related Questions