加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c++文件操作之文本文件-读文件

发布时间:2020-12-16 09:06:27 所属栏目:百科 来源:网络整理
导读:#includeiostream #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 ]
#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;
    }
    第二种
    while (ifs.getline(buf,sizeof(buf))) {
        cout << buf <<第三种
    string buf;
    while(getline(ifs,buf)) {
        cout << buf <<第四种
    char c;
    while ((c = ifs.get()) != EOF) {
        这里没有endl;
        cout << c;
    }
    ifs.close();
}
int main() {
    test();
    system(pause"return ;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读