C++ 文件读取

Posted qmzp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 文件读取相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

    ifstream in;
    in.open("test.txt");
    if(!in){
        cout << "open file failed !"<< endl;
        return 1;
    }


//    char ch;
//    // get a char
//    while(in.read(&ch, 1)) {  /// or replace with : in.get(ch)
//        cout << ch << endl;
//    }


    // get a line
    char chr[20];
    while(in.getline(chr, 15)){
        cout << chr <<endl;
    }
    in.close();

    return 0;
}

其他操作可参考:http://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html

以上是关于C++ 文件读取的主要内容,如果未能解决你的问题,请参考以下文章

C++ 代码片段(积累)

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

这些 C++ 代码片段有啥作用?

有趣的 C++ 代码片段,有啥解释吗? [复制]

以下代码片段 C++ 的说明

此 Canon SDK C++ 代码片段的等效 C# 代码是啥?