一个文件读写的简易例子

先在储存工程文件的文件夹中新建两个文件:in.txt和out.txt,分别用于存放需要读入的内容和写进的内容。

例如在in.txt中输入

a 1
v 3.2
d 0.2
q 0
w 12
f 0.1
0 0

运行程序

#include <fstream>
using namespace std;

int main()
{
    ifstream in;  //输入流对象
    ofstream out;  //输出流对象
    char ch;  
    double w;
    in.open("in.txt");  //打开相应文件
    out.open("out.txt");
    while(!in.eof()) //判断文件是否读完,eof是文件结束符
    {
        in >> ch >> w;
        out << ch << " " << w << endl;
    }
    in.close();  //关闭文件
    out.close();
    return 0;
}

再打开out.txt,文件中显示的内容和in.txt的一样。

你可能感兴趣的:(一个文件读写的简易例子)