- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下 C 函数:
int vacateDir(const INODE *pParDirInode)
{
FILE *fp = fopen("fs560", "rb+");
if (fp == NULL) {
fprintf(stderr, "Parent directory could not be opened.\n");
return 0;
}
int i;
char line[MAX_ENTRY_LENGTH];
// Move the file pointer to the start of the directory's first data block.
fseek(fp, DATA_SECTION_HEAD+BLOCK_SIZE*pParDirInode->directBlocks[0] , SEEK_SET);
// Advance the file pointer to the first real entry of the directory.
for (i = 0; i < 4; i++) {
line[0] = '\0';
fgets(line, MAX_ENTRY_LENGTH, fp);
}
printf("The file pointer is now at %lx.\n", ftell(fp));
printf("fputc returned %d.\n", fputc(0, fp));
printf("The file pointer is now at %lx.\n", ftell(fp));
// Alternative approach, which doesn't work either.
//char x[1];
//x[0] = 0;
//fwrite(x, 1, 1, fp);
fclose(fp);
return 1;
}
我期望这个函数做的是将 0 写入单个字节并将文件指针前进 1。我已经用第一个 printf 语句和 hexdump 验证了文件指针在 fputc 调用之前的正确位置。
发生的情况是预期字节未更改为 0,文件指针向前跳转 4081 字节。 fputc 指示的返回值为 0,这是我所期望的。 (EOF 在我的系统上是 0xFF,所以我认为它没有返回错误。)这是输出:
The file pointer is now at 2f247.
fputc returned 0.
The file pointer is now at 30238.
通过将写入的字符更改为 65 (A),我已验证有一个字符正在写入 0x30237。我也尝试过使用 putc,并以 r+ 模式打开文件,但都没有任何效果。
我真的很想知道这里发生了什么,以及如何解决它。我在 here 等地方查看了有关 fputc 的可用文档,但我看不出这个问题有任何原因。
最佳答案
好的,团队,干得好! (说真的,谢谢。)
这是我的工作:
int vacateDir(const INODE *pParDirInode)
{
FILE *fp = fopen("fs560", "rb+");
if (fp == NULL) {
fprintf(stderr, "Parent directory could not be opened.\n");
return 0;
}
int i, pos;
char line[MAX_ENTRY_LENGTH];
// Move the file pointer to the start of the directory's first data block.
fseek(fp, DATA_SECTION_HEAD+BLOCK_SIZE*pParDirInode->directBlocks[0] , SEEK_SET);
// Advance the file pointer to the first real entry of the directory.
for (i = 0; i < 4; i++) {
line[0] = '\0';
fgets(line, MAX_ENTRY_LENGTH, fp);
}
pos = ftell(fp);
fseek(fp, pos, SEEK_SET);
fputc(0, fp);
printf("The file pointer is now at %lx.\n", ftell(fp));
fclose(fp);
return 1;
}
我确信 fflush 的某些应用程序也能正常工作。
所以我从这个问题中得到的教训是,如果你从一个文件中读取(就像我使用 fgets 和 ftell)然后写入一个文件(就像使用 fputc),你必须调用 fseek 并重置文件指针。
关于c - fputc 不工作,将文件指针推进得太远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709440/
prometheus 和 node exporter 都使用 chrony 来同步时间。 [root@MiWiFi-R3-srv node_exporter-0.18.1.linux-amd64]#
由于某种原因,当我调整浏览器窗口的大小时,文本字段下方的按钮似乎变得比文本框更宽。有什么想法吗? SCRN: HTML : Title
我的一个 有问题标题。我对所有这些都使用单一样式,但有一个特定的 header 无法正常工作。 [附截图] 这是我的全部源代码: Title
我是一名优秀的程序员,十分优秀!