C ++删除txt文件中的最后一个字符 [英] C++ delete last character in a txt file

查看:750
本文介绍了C ++删除txt文件中的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于删除txt文件中的最后一个字符的指南。例如,如果我的txt文件包含1234567,我需要C ++代码删除最后一个字符,使它成为123456.

Hi guys I need some guide on deleting the last character on a txt file. For example if my txt file contains 1234567, I need the C++ code to delete the last character so that it becomes 123456. Thanks guys.

推荐答案

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

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.

这篇关于C ++删除txt文件中的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆