0

我在使用 stdio 命令处理文件中的数据时遇到问题。简而言之,当我将数据写入文件时,write 返回一个表明它成功的 int,但是当我读回它时,我只得到旧数据。

这是代码的精简版本:

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

此外,如果您想知道,这里是 dE 结构:

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

这打印:

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>
4

1 回答 1

1

|O_APPENDopen通话中删除。

于 2012-10-28T22:44:11.667 回答