输入输出,是每个程序员的基本功,尤其是对文本的输入和输出。最近,自己在这方面做了一些总结,不是很全面,希望在以后学习和工作的过程中慢慢补充,积累点点滴滴。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;
}