gpt4 book ai didi

c - Fwrite 似乎总是追加而不是从游标写入

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:33 25 4
gpt4 key购买 nike

我正在 linux (xubuntu 13.10) 上的二进制文件中写入和读取结构。首先:我打开它:

dtbFile = fopen(dtbLocation,"ab+");

接下来:我将向它写入以下结构几次 (9)

typedef struct{
char flagRemoved; //This flag indicates if the struct is still used. If its not 0 (as in 00000000 or \0), the thing is removed
int value;
time_t timeStamp;
}__attribute__( ( packed ) ) DTBItem_HDD; //Prevent padding to save RAM and HDD

然后打印所有内容以证明一切正常:

flagRemoved = 0 Item value: 1 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 2 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 3 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 4 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 5 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 6 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 7 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 8 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013 flagRemoved = 0 Item value: 9 Item Timestamp (expressed as localtime): Mon Oct 21 11:46:23 2013

打印的函数:

int printHDDContent(void){
//First: we are always at EOF so rewind.
rewind(dtbFile);
DTBItem_HDD hddItem;
DTBItem item; //Used to print the contents of the DTBItem_HDD
int iAmountRead; //Used to find eof
while (1){ //Cant put anything usefull here, since EOF is detected elswhere
iAmountRead = fread(&hddItem,sizeof(hddItem),1,dtbFile);
if (iAmountRead != 1){
if (!feof(dtbFile)){ //Check if EOF, and if not its error time!
return 11;
}
break; //EOF, break out of the while loop
}
//Convert it to a DTBItem and print it, but only if flagRemoved is still 0
printf("flagRemoved = %d\n",hddItem.flagRemoved);
if (!hddItem.flagRemoved){ //Check the flagRemoved
item.value = hddItem.value;
item.timeStamp = hddItem.timeStamp;
printDTBItem(&item);
}
}
return 0;
}

那么,这就是我提出问题的地方。接下来我要做的是通过将 flagRemoved 属性设置为非 0 的值,将它们标记为已删除,从而从文件中删除项目。为此,我编写了以下代码:

if (shouldBeRemoved){
int itemSeek = -1 * (int)sizeof(hddItem);
printf("Removing..(%d, %zu)\n",hddItem.value, hddItem.timeStamp);
iErr = fseek(dtbFile,itemSeek,SEEK_CUR);
if (iErr) return 11; //Error while seeking (unknown file error)
hddItem.flagRemoved = 1; //Set the removedFlag
iErr = fwrite(&hddItem,sizeof(DTBItem_HDD),1,dtbFile); //Write the item back to the file
if (iErr != 1) return 11; //Should be one, bacuse one item is written. If not: unknown error with file
}

这就是它出错的地方:每当我在这里使用 fwrite 时,它​​总是寻找到文件的末尾,然后写入这个新对象,而不是从游标的开始,它会覆盖它的旧对象。为什么会这样?根据我在这里阅读的内容:on CPP Reference它应该写入流中的当前位置,但这是尝试删除所有项目后的打印:

flagRemoved = 0 Item value: 1 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 2 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 3 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 4 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 5 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 6 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 7 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 8 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 0 Item value: 9 Item Timestamp (expressed as localtime): Mon Oct 21 11:56:08 2013 flagRemoved = 1

最后多出的1表示多了一项,已经删除了。

最佳答案

嗯,整个错误是在读/写/附加模式下打开它:这种模式总是附加它的写入,无论如何。因为我不能保证文件存在,所以我把文件打开代码改成如下:

int initDatabase(char* dtbLocation){
int iErrCode = 0;
if (dtbFile == NULL){
dtbFile = fopen(dtbLocation,"ab+"); //Open the file in read/write-append binary mode (do not overwrite current content, create if not exists) at the end of the file
if (dtbFile == NULL){
iErrCode = 2; //The file could not be opened
} else if (fclose(dtbFile) == EOF){ //Close the file to re-open it in the correct way
iErrCode = 12; //Something went wrong while closing file
} else {
dtbFile = fopen(dtbLocation,"rb+"); //Open in read/write, file must exist
if (dtbFile == NULL){
iErrCode = 2; //The file could not be opened
}
}
}
} else {
iErrCode = 1; //The file was already opened
}
return iErrCode;

关于c - Fwrite 似乎总是追加而不是从游标写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19491384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com