C Fwrite似乎总是追加,而不是从游标写入
我在linux(xubuntu 13.10)上编写和读取二进制文件的结构。第一:我打开它:C Fwrite似乎总是追加,而不是从游标写入,c,file,binaryfiles,C,File,Binaryfiles,我在linux(xubuntu 13.10)上编写和读取二进制文件的结构。第一:我打开它: dtbFile = fopen(dtbLocation,"ab+"); 下一步:我将向它写入以下结构几次(9) 然后打印所有内容以证明一切正常: flagRemoved=0项值:1项时间戳(表示为localtime): 2013年10月21日星期一11:46:23 flagRemoved=0项目值:2项目时间戳 (以本地时间表示):2013年10月21日星期一11:46:23 flagRemoved=0
dtbFile = fopen(dtbLocation,"ab+");
下一步:我将向它写入以下结构几次(9)
然后打印所有内容以证明一切正常:
flagRemoved=0项值:1项时间戳(表示为localtime):
2013年10月21日星期一11:46:23 flagRemoved=0项目值:2项目时间戳
(以本地时间表示):2013年10月21日星期一11:46:23 flagRemoved=0
项目值:3个项目时间戳(表示为本地时间):周一至十月二十一日
11:46:23 2013 flagRemoved=0项值:4项时间戳(表示
本地时间):周一10月21日11:46:23 2013 flagRemoved=0项目值:5
项目时间戳(表示为本地时间):2013年10月21日星期一11:46:23
flagRemoved=0项值:6项时间戳(表示为localtime):
2013年10月21日星期一11:46:23 flagRemoved=0项目值:7项目时间戳
(以本地时间表示):2013年10月21日星期一11:46:23 flagRemoved=0
项目值:8个项目时间戳(表示为本地时间):周一至十月二十一日
11:46:23 2013 flagRemoved=0项值:9项时间戳(表示
当地时间):2013年10月21日星期一11:46:23
打印以下内容的函数:
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时,它总是寻找文件的结尾,然后写入这个新对象,而不是从游标的开头开始,在那里它会覆盖它的旧对应对象。为什么会这样?根据我在这里读到的内容:它应该写入流中的currentposition,但这是尝试删除所有项目后的打印:
flagRemoved=0
项目值:1个项目时间戳(表示为本地时间):周一至十月二十一日
11:56:08 2013 flagRemoved=0项值:2项时间戳(表示
本地时间):周一至十月21日11:56:08 2013 flagRemoved=0项目值:3
项目时间戳(表示为本地时间):2013年10月21日星期一11:56:08
flagRemoved=0项值:4项时间戳(表示为localtime):
2013年10月21日星期一11:56:08 flagRemoved=0项目值:5项目时间戳
(以本地时间表示):2013年10月21日星期一11:56:08 flagRemoved=0
项目值:6个项目时间戳(表示为本地时间):周一至十月二十一日
11:56:08 2013 flagRemoved=0项值:7项时间戳(表示
本地时间):周一至十月21日11:56:08 2013 flagRemoved=0项目值:8
项目时间戳(表示为本地时间):2013年10月21日星期一11:56:08
flagRemoved=0项值:9项时间戳(表示为localtime):
2013年10月21日星期一11:56:08 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;
}您的分析是正确的。您的解决方案看起来有点复杂,但它似乎是创建文件的唯一方法,但如果文件存在,则不会截断它。不幸的是,
fopen
接口出错(POSIXopen
接口有单独的标志,如果不存在则创建,如果存在则截断,因此您可以给出所有组合,但标准C接口没有)。
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;