- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一些代码在服务器上运行,直到它被移动到不同的系统。问题似乎出在这里:
给定一个我在别处定义的结构:
user1_type user1; /* structure containing user data */
user1_type *user1_ptr=&user1;
这个例程将记录追加到文件的末尾
if ((dfile=fopen(filename,"ab+"))==NULL)
error_message("Unable to open file for append.",filename,1);
else { /* append data */
user1.recid=ftell(dfile); /* update file position */
fwrite(user1_ptr,sizeof(user1),1,dfile);
fflush(dfile);
fclose(dfile);
我可以确认数据已附加到文件中,但 user1.recid 的值始终返回 0 - 知道为什么吗?
更新:看起来问题不是所有实现都需要在 fopen() 之后使用 fseek()。我显然需要一个 fseek(dfile,0,SEEK_END);在我追加时执行 ftell() 之前。但是如果我想从文本或二进制文件的开头读取,是否也习惯于在 fopen 之后放置一个 fseek()?它是否因文件类型而异?
最佳答案
来自 MSDN 的文档 ftell
The position returned by
ftell()
is expressed as an offset relative to the beginning of the streamIf no I/O operation has yet occurred on a file opened for appending, the file position is the beginning of the file.
这使您相对于开头的偏移量为 0。
因此,当您调用 user1.recid=ftell(dfile);
时,流上还没有发生任何 I/O 操作,因此 ftell()
返回 0,表示该文件指针位置在开头。
关于c++ - 在二进制追加中获取 ftell() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896230/
我想向二进制文件添加一个结构,但首先我需要检查该文件是否存储了以前的数据,如果没有,我可以添加该结构,否则我必须读取所有存储的数据并粘贴结构在正确的位置,但我对如何检查文件是否为空感到困惑,我考虑尝试
我正在尝试从二进制文件中读取数据。我得到了一个包含所有变量的结构来提取数据。我正在使用 fread 函数,我需要获取文件的总大小作为 freads 输入之一。我正在使用 ftell 函数查找大小,但在
这个问题在这里已经有了答案: ftell at a position past 2GB (4 个答案) 关闭 9 年前。 根据 unix 手册页 ftell 要么返回 -1 以指示错误。 函数定义为
我遇到了一个问题,ftell 返回的值不正确。我的代码在 linux 上的 netbeans 中运行时报告正确,但完全相同的代码在 windows 上的 netbeans 中运行时(使用 mingw)
我正在查看 SO 帖子 fseek does not work when file is opened in "a" (append) mode当我们以“a”模式打开文件时,我对 ftell() 返回
有没有办法在原始文件描述符而不是 FILE* 上执行 ftell() 所做的(返回文件中的当前位置)?我认为应该有,因为您可以使用 lseek() 查找原始文件描述符。 我知道我可以使用 fdopen
我想计算文件中的字母数。我已经尝试使用 Windows 和 Linux 测试编辑器编写文件,但两者都给出了相同的值(7)。该文件有 5 个字符('P','2','\n','#','\n'),为什么 f
fseek(f, 0, SEEK_END); size = ftell(f); 如果 ftell(f) 告诉我们当前文件位置,这里的大小应该是文件末尾到开头的偏移量。为什么大小不是 ftell(f
我正在我的 Windows 操作系统上查看文件 Agents.h,我想查看没有注释的 C++ 代码。我将它们剥离出来,以便用我的旧程序更清楚地看到代码,但令我惊讶的是它花了大约 2秒完成。文件的大小是
我试图让我的程序为每个 child 一次读取一行(每行包含一个 int)。每次我执行此读取时,它都会继续读取第一行。 这是我的代码的基础。 void forkChildren (int nChildr
我正在尝试编写一个函数,该函数将按字符串中的程序名称终止实例 unsigned int kill_all_program_instances(const char * program_name){
所以我有一个文本文件,其中存储了数据。现在我想计算每个数据在其中的位置,所以我使用了下面的方法。 while (fscanf(fp, "%d", &id) != EOF) {
#include #include int main() { long int count; FILE *file=NULL; file=fopen("sample.txt","r+
我有一些代码在服务器上运行,直到它被移动到不同的系统。问题似乎出在这里: 给定一个我在别处定义的结构: user1_type user1; /* structure cont
这个问题在这里已经有了答案: Why does ftell() shows wrong position after fread()? (3 个答案) 关闭 5 年前。 我有一个名为 myfile.
我正在编写一个程序,将许多文件连接成一个。我的目标是让用户插入名称为“xxx”的文件数量“n”,然后我将从文件“xxx001.txt”读取到“xxx00n.txt”。每当文件打开时,我使用 fseek
我需要循环遍历目录data并读取字符串中满足特定条件的每个文件并对其执行某些操作。由于某种原因,它在 fseek 调用后失败(输出只是目录中第一个文件的名称)。 知道我做错了什么吗? #include
在 32 位系统上,如果以二进制模式打开的文件的当前位置指示器超过 2GB 点,ftell 将返回什么?在C99标准中,这是未定义的行为,因为ftell必须返回一个long int(最大值为 >2**
如何在 Java 中查找文本文件?我正在处理一个基于文本的日志文件,并且想要检查我的工作,以便它可以在未来调用我的程序时恢复。 我正在寻找类似于 C stdio fseek()/ftell() 功能的
这一行 Assert(pos == ftell(file)); 在我的代码中使用,当文件包含非 ASCII 字符时,此行失败。 我该怎么办? 为了清楚起见,这里更新了整个函数: int getTerm
我是一名优秀的程序员,十分优秀!