先在储存工程文件的文件夹中新建两个文件:in.txt和out.txt,分别用于存放需要读入的内容和写进的内容。
例如在in.txt中输入
a 1
v 3.2
d 0.2
q 0
w 12
f 0.1
0 0
运行程序
1 #include <fstream> 2 using namespace std; 3 4 int main() 5 { 6 ifstream in; //输入流对象 7 ofstream out; //输出流对象 8 char ch; 9 double w; 10 in.open("in.txt"); //打开相应文件 11 out.open("out.txt"); 12 while(!in.eof()) //判断文件是否读完,eof是文件结束符 13 { 14 in >> ch >> w; 15 out << ch << " " << w << endl; 16 } 17 in.close(); //关闭文件 18 out.close(); 19 return 0; 20 }
再打开out.txt,文件中显示的内容和in.txt的一样。