- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在 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/
谁能帮我解决这个问题?我有一个 Tomcat 和简单的 JSF 应用程序:https://github.com/gooamoko/jsfbilling/ .当我在 Tomcat 上运行应用程序时,它运
我有两个这样的域类,第一个是 Manager : package com.mnm class Manager { String name; static hasMany = [ pro
当我运行以下代码时,打印输出似乎不正确。 void thread_Calc(int *pos) { printf("recieved %d\n", *pos); sig = -1; man
这个问题在这里已经有了答案: How to access a local variable from a different function using pointers? (10 个答案) 关闭
我编写了一个程序,其中列表构建器方法返回 IEnumerable of string,其中包括大量字符串(100 万个项目),我将其存储在 List of string 中,然后它将所有项目附加到 中
我正在尝试编写一个 IRC 类型的聊天客户端,它具有可以连接到服务器的客户端。我试图让它在本地 atm 上工作(使用 FIFOS 而不是套接字)。 我遇到了以下我似乎无法解决的问题: 接受新的客户端连
我的一个 cronjobs 每天发送一封电子邮件 35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1 我停止使用 cron sud
我使用 WPF 打印路径来处理在我们的应用程序中创建的大型图表。整个图表由视觉效果组成。 所谓的“DesignerPaginator”对图表进行分页(非常简单)。 从这一点来说,我做了以下三件事: -
我尝试在更新之前跟踪系统应用程序并使用: public static boolean isSystemApplication(Context ctx, IContent content) {
我在这里附上了一个查询分析结果,https://explain.depesz.com/s/x9BN 这是查询 EXPLAIN ANALYZE SELECT branche
我正在做一个 CXF(spring) 项目 (HUB)。部署后,我可以看到肥皂和休息服务列表,我通过两个地址打开它。一种是使用本地主机,第二种是使用我电脑的 ip。所以我得到了这些输出。 使用本地主机
这是一个 AnyHashable 不支持枚举转换的简单案例。 enum testEnum: String { case Test } let myObject: AnyHashable = t
我的主要目标是比较存储在数据库和 XLSX 文件中的数据。 为此,我按以下方式创建了两个列表: private class ProductList { public string produc
我从 CMake 3.6 更新到任何最新版本 (3.12.0-rc2),现在我的一个程序无法编译。 奇怪的是,错误消息显示了标准库本身中的 undefined symbol 。这是错误消息: Unde
我希望将我的自定义对话框动画化为从特定点出现,但我无法为对话框设置动画。 该对话框是一个基本的 RelativeLayout,设置为 extends Dialog 类中的布局。 正如这里的一些答案所建
我已经在这个论坛上调查过很多类似的问题,但似乎没有一个能解决我的问题。 我会在底部列出我在这个论坛上看到的一些问题页面,但让我先谈谈我对这个问题的看法。 我正在使用 codeigniter v 2.x
我正在尝试在 RHEL 7 上启动一个 docker-compose 项目作为 systemd 服务。这是我的 systemd 脚本 (/etc/systemd/system/wp.service):
这个问题已经有答案了: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key",
我正在尝试在 RHEL 7 上启动一个 docker-compose 项目作为 systemd 服务。这是我的 systemd 脚本 (/etc/systemd/system/wp.service):
此问题出现在my last question here之后。我想将每个按钮聚焦和失去焦点背景设置为主菜单(ContentPane 即 JPanel)下方的背景颜色,因此按钮看起来像选项卡。它在不同的环
我是一名优秀的程序员,十分优秀!