zoukankan      html  css  js  c++  java
  • c++文件操作之文本文件-读文件

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    void test() {
        ifstream ifs;
        //如若不指定路径,则在该项目同级下生成
        ifs.open("test.txt", ios::in);
        if (!ifs.is_open()) {
            return;
        }
        //读文件
        //第一种
        char buf[1024] = { 0 };
        while (ifs >> buf) {
            cout << buf << endl;
        }
        //第二种
        char buf[1024] = { 0 };
        while (ifs.getline(buf, sizeof(buf))) {
            cout << buf << endl;
        }
        //第三种
        string buf;
        while(getline(ifs,buf)) {
            cout << buf << endl;
        }
        //第四种
        char c;
        while ((c = ifs.get()) != EOF) {
            //这里没有endl;
            cout << c;
        }
        ifs.close();
    }
    int main() {
        test();
        system("pause");
        return 0;
    }
  • 相关阅读:
    easyui好例子,值得借鉴
    DDL 和DML 区别
    兼容IE的文字提示
    搭代理
    美国服务器
    跟随滚动条滚动
    JS Array对象
    JS 内置对象 String对象
    JS 对象
    JS 二维数组
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12103028.html
Copyright © 2011-2022 走看看