fseek() 不起作用

发布于 2024-11-08 15:46:38 字数 482 浏览 0 评论 0原文

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似梦非梦 2024-11-15 15:46:38

就像其他人已经说过的那样,没有简单的方法可以在文件中间插入数据。如果您确实想这样做,可以执行以下步骤:

  1. 创建第二个文件
  2. 将要插入的位置之前的所有数据复制到第二个文件中
  3. 将所需的行插入到第二个文件中
  4. 复制 将剩余数据保存到第二个文件
  5. 删除原始文件
  6. 重命名第二个文件

其他方法是使用二进制文件而不是文本文件。尽管二进制文件有点难学,但一旦您了解了它们的工作原理,您就会发现使用它们与使用数组非常相似。例如,要执行此任务,您甚至不需要使用辅助文件。

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:

  1. Create a second file
  2. Copy all data before the place you want to insert to the second file
  3. Insert the line you want to the second file
  4. Copy the remaining data to the second file
  5. Delete the original file
  6. Rename the second file

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.

魔法唧唧 2024-11-15 15:46:38

没有开放模式允许您在随机点将数据“插入”到文件中。唯一可以添加数据而不覆盖现有数据的地方是文件末尾(使用模式“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.

看海 2024-11-15 15:46:38

没有一种简单的方法可以在文件中间插入数据。文件基本上是一个字符数组。要在中间添加一个字符,您需要将插入点后面的所有内容复制到一个位置。对于文件,您需要读取后面的数据并在添加后写入。

一般来说,当您想做这样的事情时,您会创建一个新文件。将旧文件复制到其中,直到要插入的位置,然后写入要插入的数据,然后复制旧文件的其余部分。最后,将新文件重命名为旧文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文