fseek() 不起作用
我已经使用 a 和 r+ 打开了一个文件,但是当我使用 fseek 和 ftell 时,文件指针始终为 0。 我的文件如下所示:
1 -3
2 -8
我想在两者之间添加另一行,但它是在最后一行之后添加的。 另一个论坛中的某人说,当您在附加中打开文件时,指针始终为零,您必须在 r+ 中打开它,如果这不起作用“您必须读取完整的数据,然后将数据插入变量中,写回来。”但我不明白他们的意思。
任何人都可以帮忙在文件中间插入数字吗?
谢谢!
像这样的东西会起作用吗? 传输数据?
rewind(fp);
fscanf(fp,"%d",&ch);
fprintf(fp1,"%d",ch);
fseek(fp,1,0);
fscanf(fp,"%d",&ch);
fprintf(fp1,"%d",ch);
I have opened a file using a and r+ but when I use fseek and ftell the file pointer is always 0.
My file looks like this:
1 -3
2 -8
And I want to add another line between the two but it is added in the end after the last line.
Someone in another forum said that when you open the file in append the pointer is always zero and you have to open it in r+ and if that doesn't work "you have to read the complete data and then insert the data in the variables and write it back." but I don't understand what they mean by that.
Can anyone help with inserting numbers in the middle of a file?
Thanks!
Would something like this work?
To transfer the data?
rewind(fp);
fscanf(fp,"%d",&ch);
fprintf(fp1,"%d",ch);
fseek(fp,1,0);
fscanf(fp,"%d",&ch);
fprintf(fp1,"%d",ch);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像其他人已经说过的那样,没有简单的方法可以在文件中间插入数据。如果您确实想这样做,可以执行以下步骤:
其他方法是使用二进制文件而不是文本文件。尽管二进制文件有点难学,但一旦您了解了它们的工作原理,您就会发现使用它们与使用数组非常相似。例如,要执行此任务,您甚至不需要使用辅助文件。
Like others already said, there's no easy way to insert data in the middle of a file. If you really want to do this, you can implement the following steps:
Other approach is using binary files instead of text files. Although binary files are a bit harder to learn, once you understand how they work you'll see that working with them is much like working with arrays. To perform this task, for example, you'd not even need to use an auxiliary file.
没有
开放
模式允许您在随机点将数据“插入”到文件中。唯一可以添加数据而不覆盖现有数据的地方是文件末尾(使用模式“a”打开的内容)。如果想在随机位置插入,就需要自己动手了。
一种更简单的方法是完全重写文件(将旧文件的开头传输到新文件,将数据添加到新文件,传输旧文件的其余部分,并在末尾重命名/覆盖) 。
困难的方法:您需要手动将所有数据从插入点“转移”到文件末尾。要做到这一点并不简单。
There is no
open
mode that will allow you to "insert" data into a file at a random point. The only place you can add data without overwriting existing data is the end of the file (what you get opening with mode "a").If you want to insert at a random position, you need to do it yourself.
One of the easier ways is to re-write the file completely (transfer the start of the old file to a new file, add your data to the new file, transfer the rest of the old file, and rename/overwrite at the end).
The hard way: you need to "shift" all the data from your insertion point to the end-of-file manually. That's not trivial to get right.
没有一种简单的方法可以在文件中间插入数据。文件基本上是一个字符数组。要在中间添加一个字符,您需要将插入点后面的所有内容复制到一个位置。对于文件,您需要读取后面的数据并在添加后写入。
一般来说,当您想做这样的事情时,您会创建一个新文件。将旧文件复制到其中,直到要插入的位置,然后写入要插入的数据,然后复制旧文件的其余部分。最后,将新文件重命名为旧文件。
There isn't an easy way to insert data in the middle of the file. A file is basically an array of characters. To add a character in the middle, you need to copy everything following your insertion point down one location. With a file you need to read the data that follows and write it after your addition.
Generally, when you want to do something like this you create a new file. You copy the old file into it up to the point where you want to insert, then you write the data you want to insert, then you copy the rest of the old file. Finally, you rename the new file to the old file.