如何编辑已在C ++中创建的文件txt [英] How to edit file txt already created in C++

查看:210
本文介绍了如何编辑已在C ++中创建的文件txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何创建导出数据到文件txt,但如果我已经txt文件,如何编辑该文件txt不适用于数据已经存在..
这也意味着添加一个新的数据线已经有数据..

I have know how to create export data to file txt,but If I already txt file, how to edit that file txt which don't apply to data have already exist.. This also mean add a new data line in a txt file already have data..

推荐答案

您可以使用fstream(#include< fstream>): / p>

You can use fstream (#include < fstream >):

// declare variable "file"
std::fstream file;
// open file named "data.txt" for writing (std::fstream::app lets you add text to the end of the file)
file.open("data.txt", std::fstream::in | std::fstream::out | std::fstream::app);
// could not open file
if(!file.is_open()) {
    // do something, e.g. print error message: std::cout << "Couldn't open file." << endl;
// file is open, you can write to it
} else {
    file << "\n"; // add a new line to the file
    file.close(); // close file
}

这篇关于如何编辑已在C ++中创建的文件txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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