如何在C中最后一行之前添加一行

发布于 2024-08-15 03:25:34 字数 111 浏览 4 评论 0原文

你好,我在 Unix 平台上使用 C 语言工作。请告诉我如何在 C 中的最后一行之前追加一行。我在追加模式下使用了 fopen,但我无法在最后一行之前添加一行。

我只想写入文件中的倒数第二行。

Hi I am working in C on Unix platform. Please tell me how to append one line before the last line in C. I have used fopen in appending mode but I cant add one line before the last line.

I just want to write to the second last line in the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

人事已非 2024-08-22 03:25:34

您不需要覆盖整个文件。您只需:

  • 以“rw”模式打开文件,
  • 读取文件以找到最后一行:将其位置(ftell/ftello)存储在文件及其中内容
  • 返回到最后一行的开头 (fseek/fseeko)
  • 在最后一行之前写下你想要的任何内容
  • 关闭你的文件。

You don't need to overwrite the whole file. You just have to:

  • open your file in "rw" mode,
  • read your file to find the last line: store its position (ftell/ftello) in the file and its contents
  • go back to the beginning of the last line (fseek/fseeko)
  • write whatever you want before the last line
  • write the last line.
  • close your file.
淡紫姑娘! 2024-08-22 03:25:34

在标准 C 中无法直接执行此操作,主要是因为很少有文件系统支持此操作。最简单的方法是将文件读入内存结构(您可能已经拥有它),将行插入内存中,然后再次写出整个结构,覆盖原始文件。

There is no way of doing this directly in standard C, mostly because few file systems support this operation. The easiest way round this is to read the file into an in memory structure (where you probably have it anyway), insert the line in memory, then write the whole structure out again, overwriting the original file.

与酒说心事 2024-08-22 03:25:34

Append 只追加到末尾,而不追加到中间。

您需要读入整个文件,然后将其写入新文件。您可能会幸运地从后面开始,并找到倒数第二个换行符的字节偏移量。然后,您可以阻止编写整个“前奏曲”,添加新行,然后发出剩余的预告片。

Append only appends to the end, not in the middle.

You need to read in the entire file, and then write it out to a new file. You might have luck starting from the back, and finding the byte offset of the second-to-last linefeed. Then you can just block write the entire "prelude", add your new line, and then emit the remaining trailer.

猛虎独行 2024-08-22 03:25:34

您可以找到最后一行结束的位置,将最后一行读入内存,查找回该位置,写入新行,然后写入最后一行。

要找到该位置:查找到末尾,减去缓冲区大小。读缓冲区,寻找
换行符。如果未找到,则向后查找两个缓冲区大小,然后重试。

您需要对 fopen 使用 r+ 模式。

哦,您需要小心文本和二进制模式。您需要使用二进制模式,因为在文本模式下您无法计算跳转位置,只能跳转到从 ftell 获取的位置。您可以通过阅读整个文件并在每行开头调用 ftell 来解决此问题。对于大文件,速度会很慢。

You can find the place where the last line ends, read the last line into memory, seek back to the place, write the new line, and then the last line.

To find the place: Seek to the end, minus a buffer size. Read buffer, look for
newline. If not found, seek backwards two buffer sizes, and try again.

You'll need to use the r+ mode for fopen.

Oh, and you'll need to be careful about text and binary modes. You need to use binary mode, since with text mode you can't compute jump positions, you can only jump to locations you've gotten from ftell. You can work around that by reading through the entire file, and calling ftell at the beginning of each line. For large files, that is going to be slow.

宛菡 2024-08-22 03:25:34

使用fseek跳转到文件末尾,向后读取直到遇到换行符。然后插入你的线。
您可能希望通过计算向后读取的字符数来保存正在读取的“最后一行”,然后将其 strncpy 到正确分配的缓冲区。

Use fseek to jump to end of file, read backwards until you encounter a newline. Then insert your line.
You might want to save the 'last line' you are reading by counting how many chars you are reading backwards then strncpy it to a properly allocated buffer.

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