stdio data from write not making it into a file

Posted by user1551209 on Stack Overflow See other posts from Stack Overflow or by user1551209
Published on 2012-10-28T22:19:52Z Indexed on 2012/10/28 23:01 UTC
Read the original article Hit count: 175

Filed under:
|
|

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>

© Stack Overflow or respective owner

Related posts about c

    Related posts about file-io