假设有一个叫
data.txt 的文件,
它包含以下内容:
Fry:
[Everyone
Auctioneer:
数据读取,
#include
#include
#include

using

//输出空行
void


{
}

//读取方式:
//read
//when
//but
void


{

{
}

//读取方式:
//If
//we
void


{

{
}

//读取方式:
//If
//you
void


{

{
}

//带错误检测的读取方式
//Simply
//if
void


{

{
}

int


{



}
输出结果为:
Read from file:
Fry:
Read from file: One
Read from file: Jillion
Read from file: dollars.
Read from file: [Everyone
Read from file: gasps.]
Read from file: Auctioneer:
Read from file: Sir,
Read from file: that's
Read from file: not
Read from file: a
Read from file: number.
Read from file: 数据读取,
Read from file: 测试
Read from file: 。
Read from file: Fry: One Jillion
dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: 数据读取, 测试 。
Read from file: Fry: One Jillion
dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: 数据读取, 测试 。
Error
opening
Press any key to continue
本文介绍了使用C++进行文件读取的多种方法,包括逐词读取、逐行读取到字符数组或字符串中,并展示了如何进行错误检测。
1059





