C++ 读取文件的几种方式

2014-11-24 14:49:02 · 作者: · 浏览: 42

//c++按行读取


/*


* main.cc


*


* Created on: 2011-12-1


* Author: simondu


*/


#include "head.h"


using namespace std;


int main(int argc, char* argv[])


{


printf("Starting…… \n");


string file = argv[1];


string tmp;


ifstream fin(file.c_str());


while(getline(fin,tmp))


{


cout《tmp《endl;


}


printf("Ending…… \n");


return 0;


}


//写文件


/*


* main.cc


*


* Created on: 2011-12-1


* Author: simondu


*/


#include "head.h"


using namespace std;


int main(int argc, char* argv[])


{


printf("Starting…… \n");


string file = argv[1];


string tmp;


ofstream fin(file.c_str());


if(fin.is_open())


{


fin《"hello\n";


fin《"world\n";


fin.close();


}


printf("Ending…… \n");


return 0;


}