【C++】文件流 (File Streams)

    xiaoxiao2022-07-15  145

    //================================== // C++程序设计 // 文件流(File Streams) //================================== //================================== // ifstream fin(filename,openmode=ios::in); // ofstream fout(filename,openmode=ios::out); //ifstream 和ofstream是类型名,表示输入和输出文件流 //fin和fout是文件流的名称,filename 是外部文件名 //================================== //================================== //openmode是打开方式 //ifstream的默认打开方式是ios::in //ofstream的默认打开方式是ios::out //在打开已经存在的输入文件和 //新建一个输出文件时可以省略这个参数 //================================== #include<fstream> using namespace std; int main() { ifstream fin("file_in.txt"); //定义输入流fin ofstream fout("file_out.txt"); //定义输出流fout //================================== //getline(fin,str)从输入流中读入一行数据,放入string变量str中 //读入str到中,每行的换行符会被丢弃,输出是补上换行符 //================================== for(string str;getline(fin,str);) //从输入流中获取数据 fout<<str<<endl; //数据输出 }

     

    最新回复(0)