ofstream ifstream getline 对文件的操作 !

#include《fstream 》

ofstream------------以输出的方式 写文件

ifstream-------------以输入的方式读取文件

getline( istream,string)------把一行 读入到string对象中 以换行符结束!


#include<iostream>

using namespace std;
#include<fstream>
#include<string>
int main()
{
ofstream out1("lcf.txt"); ---------------不存在 则创建!!!
if(!out1)
cout<<"文件打开失败"<<endl;
out1<<"nishi"<<endl              -------------清除缓冲区!
<<"11111"<<endl
<<"dlds;l"<<endl;


ifstream  in1("lcf.txt");
if(!in1)
 cout<<"文件打开失败"<<endl;
string  s;
while(getline(in1,s))
cout<<s<<endl;
    system("pause");

}

把 一个文件内容拷贝到另一个文件中

ifstream  in(“lcf.cpp”);

ofstream out(“lcf2.cpp”);

string   s;

while(getline(in,s))

out<<s<<"\n";


向量 加入其中  之后的 编写!!!!

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace  std;
int  main()
{
   vector<string> v;
   ifstream  in("lcf.txt");
   string  line;
   while(getline(in,line))
  v.push_back(line);
   vector<string>::iterator it=v.begin();
   for(;it!=v.end();)
   {
  cout<<*it<<endl;
       it++;
   }
     system("pause");
}


加入 argc  argv 可以查看文件路劲!

ofstream ifstream getline 对文件的操作 !_第1张图片

你可能感兴趣的:(C++,关键字,iostream)