IO

getline按行读取内容(C++)

•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束
#include<stdio.h>
#include<string.h>
#define INFO_MAX_SZ 255
#include <fstream>
#include <string>
#include <iostream>

using namespace std;
void readLine()
{
    ifstream f("t.txt", ios::in);
    string buff;
    while(getline(f, buff))
        cout << buff << endl;
}
int main()
{
    readLine();
    return 0;
}

fgets读取行(C)

#include<stdio.h>
#include<string.h>
#define INFO_MAX_SZ 255
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void fgetsLine()
{
    FILE* f = fopen("t.txt", "r");
    if(f == NULL)
        cout << "打开文件失败" << endl;
    char ch[1024];
    while(fgets(ch, 1024, f))
    {
        cout<< ch;
    }
}
int main()
{
    fgetsLine();
    return 0;
}

按字段读取(C++)

#include<stdio.h>
#include<string.h>
#define INFO_MAX_SZ 255
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void reader()
{
    ifstream f("t.txt", ios::in);
    if(!f.is_open())
        cout << "文件打开失败" << endl;
    while(f != NULL)
    {
        string ch;
        f >> ch;
        if(ch == "")
            break;
        cout << ch << endl;
    }
}
int main()
{
    reader();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值