一 、向一个文件写入数据
为了向一个文档写入数据,需要创建一个ofstream类型的对象:
ofstream output;
为了指定要写入的文件。需要调用output对象的open函数:如下所示
output.open("number.txt");
此语句会创建一个名为number.txt的文件,如果文件已经存在,其内容会被销毁,并创建一个新的文件。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream output;
output.open( "number.txt" );
output << 34 << " " << 45 << " " <<89;
output.close();
cout << "Done" <<endl;
return 0;
}
二、向一个文件读取数据
为了向一个文件读取数据,需要定义一个ifstream类型的变量:
ifstream input;
为了指定要读取的文件,需要调用input对象的open函数:如下
input.open("number.txt");
int main()
{
ifstream input;
input.open( "number.txt" );
int score1,score2,score3;
input >> score1;
input >> score2;
input >> score3;
cout << "Tatal score is " << score1 + score2 + score3 <<endl;
input.close();
cout << "Done" <<endl;
system("pause");
return 0;
}
三、检测文件尾
调用input 对象的eof()函数来检测文件尾
int main()
{
ifstream input;
input.open( "number.txt" );
int score,total=0;
while (!input.eof())
{
input >> score;
cout << score << endl;
total+=score;
}
cout << "Tatal score is " << score <<endl;
input.close();
cout << "Done" <<endl;
system("pause");
return 0;
}
四、fstream
int main()
{
fstream inout;
inout.open("city.txt",ios::out);
inout<<"Dallas"<<" "<<"houston"<<" "<<"Atlanta"<<" ";
inout.close();
inout.open("city.txt",ios::out | ios::app);
inout<<"changchun"<<" "<<"Guiyang"<<" "<<"Beijing";
inout.close();
char city[20];
inout.open("city.txt",ios::in);
while(!inout.eof())
{
inout>>city;
cout<<city<<" ";
}
inout.close();
system("pause");
return 0;
}
五、二进制输入输出
int main()
{
fstream binaryio;
binaryio.open("city.dat",ios::out | ios::binary);
char s[]="Alants";
binaryio.write(s,5);
binaryio.close();
binaryio.open("city.dat",ios::in | ios::binary);
char s2[10];
binaryio.read(s2,5);
s2[5]='\0';
cout<<s2<<endl;
binaryio.close();
system("pause");
return 0;
}
//读取非字符数据
int main()
{
fstream binaryio;
binaryio.open("temp.dat",ios::out | ios::binary);
int value=199;
binaryio.write(reinterpret_cast<char*>(&value),sizeof(value));
binaryio.close();
binaryio.open("temp.dat",ios::in | ios::binary);
int value2;
binaryio.read(reinterpret_cast<char*>(&value2),sizeof(value2));
cout<<value2<<endl;
binaryio.close();
system("pause");
return 0;
}
//二进制数组I/O
int main()
{
const int size=10;
fstream binaryio;
binaryio.open("temp.dat",ios::out | ios::binary);
int s[size]={12,45,78,2,8,78,178,28,177,90};
binaryio.write(reinterpret_cast<char*>(&s),sizeof(s));
binaryio.close();
binaryio.open("temp.dat",ios::in | ios::binary);
int s2[size];
binaryio.read(reinterpret_cast<char*>(&s2),sizeof(s2));
binaryio.close();
for(int i=0;i<size;++i)
cout<<s2[i]<<endl;
system("pause");
return 0;
}
一个文件内容复制到另一个文件
ifstream in;
ofstream out;
in.open("a.txt");
out.open("b.txt")
if(in.fail())
{
return 0;
}
char ch=in.get();
while(!in.eof())
{
out.put(ch);
ch=in.get();
}
in.close();
out.close();
fstream in,out;
in.open("bird.png",ios::in | ios::binary);
out.open("bird.dat",ios::out | ios::binary);
if(in.fail())
{
cout<<"文件不存在!"<<endl;
return 0;
}
char ch=in.get();
while(!in.eof())
{
out.put(ch);
ch=in.get();
}
in.close();
out.close();
六 string 流
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
struct PersonInfo
{
string name;
vector<string> phones;
};
int main (int argc, char *argv[])
{
string line,word;
vector<PersonInfo> people;
while(getline(cin,line))
{
PersonInfo info; //创建一个保存此记录的数据对象
istringstream record(line);//将记录保存到刚读入的行
record>>info.name;//读取名字
while(record>>word)//读取电话号码
{
info.phones.push_back(word);//保存它们
}
people.push_back(info);//将此记录追加到 people 末尾
}
for(auto i=0;i<people.size();++i)
{
cout<<people[i].name<<" ";
for(auto j=0;j<people[i].phones.size();++j)
{
cout<<people[i].phones[j]<<" ";
}
cout<<endl;
}
system("pause");
return 0;
}
小结
iostream 处理控制台IO
fstream 处理命名文件 IO
stringstream 完成内存 string 的 IO