C++对txt文本进行读写操作

输入输出,是每一个程序猿的基本功,尤其是对文本的输入和输出。

近期,自己在这方面做了一些总结,不是非常全面,希望在以后学习和工作的过程中慢慢补充,积累点点滴滴。

P.S. 今天天气不错。雾霾散了。天空晴朗。惠风和畅,心情不错。

一、写操作

// set.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
#include "string"
#include "fstream"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	const string data = (string)"hello"+"+"+"world";
	ofstream out(data,ofstream::app);
	out << 3 <<endl;
	out.close();
	return 0;
}



二、读操作

// set.cpp : 定义控制台应用程序的入口点。

// #include "stdafx.h" #include "iostream" #include "string" #include "fstream" #include "vector" #include "sstream" using namespace std; struct personalinfo{ string name; vector<string> phones; }; int _tmain(int argc, _TCHAR* argv[]) { const string data = "C:\\Users\\helei\\Documents\\Visual Studio 2010\\Projects\\set\\Release\\hello+world"; ifstream out(data,ofstream::app); string line,word; vector<personalinfo> people; while (getline(out,line)){ personalinfo info; istringstream record(line); record >> info.name; while (record >> word) info.phones.push_back(word); people.push_back(info); } cout << people[0].name <<" "<< people[0].phones[0] <<" "<< people[0].phones[1] << endl; cout << people[1].name <<" "<< people[1].phones[0] << endl; cout << people[2].name <<" "<< people[2].phones[0] <<" "<< people[2].phones[1] <<" "<< people[2].phones[2] << endl; out.close(); return 0; }



转载于:https://www.cnblogs.com/lxjshuju/p/6841081.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值