c++ 读写

来源:互联网 发布:网络专业大学排名 编辑:程序博客网 时间:2024/05/27 06:12
#include<iostream>#include<string>#include<fstream>>using namespace std;void readfile(){   //逐词读取  ifstream fp("data.txt");  string word;  while(fp>>word) {  cout<<word<<endl; } fp.close();}void readfile1() {  //逐行读取  ifstream fp("data.txt");  string str;  while(getline(fp , str)){   cout<<str<<endl; } fp.close();}void writefile(){  ofstream fp("data.txt");  for(int i = 0 ; i <10 ;i++){   fp<<"第" <<i<<"行\n"; }  fp.close();}int main(){  readfile();  readfile1();  writefile();  return 0;}


0 0
原创粉丝点击