且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++删除txt文件中的最后一个字符

更新时间:2023-02-04 23:01:50

在便携式代码中这样做的唯一方法是读入数据,并写出除了最后一个字符之外的所有字符。

The only way to do this in portable code is to read in the data, and write out all but the last character.

如果你不介意-portable代码,大多数系统提供了截断文件的方法。传统的Unix方法是寻找到你想要文件结束的地方,然后在该点写入0字节的文件。在Windows上,您可以使用SetEndOfFile。其他系统将使用不同的名称和/或方法,但几乎所有系统都具有某种形式的能力。

If you don't mind non-portable code, most systems provide ways to truncate a file. The traditional Unix method is to seek to the place you want the file to end, and then do a write of 0 bytes to the file at that point. On Windows, you can use SetEndOfFile. Other systems will use different names and/or methods, but nearly all will have the capability in some form.