zoukankan      html  css  js  c++  java
  • C++ 读取文件所有内容的方法

    方法一

    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    int main(int argc, char** argv) {
    	ifstream ifs("test.txt");
    	string content( (istreambuf_iterator<char>(ifs) ),
    					 (istreambuf_iterator<char>() ) );
    	cout << content << endl;
    	ifs.close();
    				 
    	return 0;
    }
    

    方法二

    #include <fstream>
    #include <vector>
    using namespace std;
    int main(int argc, char** argv) {
    	ifstream ifs("test.txt");
    	// get the size of file
    	ifs.seekg(0, ios::end);
    	streampos length = ifs.tellg();
    	ifs.seekg(0, ios::beg);
    	vector<char> buffer(length);
    	if (ifs.read(buffer.data(), length)) {
    		// process 
    		ofstream out("output.txt");
    		out.write(buffer.data(), length);
    		out.close();
    	}
    	ifs.close();
    	
    	return 0;
    }
    

    方法三

    #include <string>  
    #include <fstream>  
    #include <sstream>  
    using namespace std;
    int main(int argc, char** argv) {
        std::ifstream t("file.txt");  
        std::stringstream buffer;  
        buffer << t.rdbuf();  
        std::string contents(buffer.str());
        // process
    
        t.close();
        return 0;
    }
    
    
  • 相关阅读:
    hbase
    pig
    flume
    sqoop
    eclipse 提交作业到JobTracker Hadoop的数据类型要求必须实现Writable接口
    hadoop 8步走
    ssh原理
    MapReduce基础
    Arduino数字贴片磁感应传感器(收藏篇)
    去掉input回车自动提交
  • 原文地址:https://www.cnblogs.com/zhonghuasong/p/7506909.html
Copyright © 2011-2022 走看看