使用ifstream和getline读取文件内容[c++]

假设有一个叫 data.txt 的文件, 它包含以下内容:



使用ifstream和getline读取文件内容[c++]Fry: One Jillion dollars.

使用ifstream和getline读取文件内容[c++][Everyone gasps.]

使用ifstream和getline读取文件内容[c++]Auctioneer: Sir, that's not a number.

使用ifstream和getline读取文件内容[c++]数据读取, 测试 。使用ifstream和getline读取文件内容[c++]

以下就是基于 data.txt 的数据读取操作:



使用ifstream和getline读取文件内容[c++]#include<iostream>

使用ifstream和getline读取文件内容[c++]#include
<fstream>

使用ifstream和getline读取文件内容[c++]#include
<string>

使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
usingnamespacestd;

使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
//输出空行

使用ifstream和getline读取文件内容[c++]
voidOutPutAnEmptyLine()

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] cout
<<"\n";

使用ifstream和getline读取文件内容[c++]}


使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
//读取方式: 逐词读取, 词之间用空格区分

使用ifstream和getline读取文件内容[c++]
//read data from the file, Word By Word

使用ifstream和getline读取文件内容[c++]
//when used in this manner, we'll get space-delimited bits of text from the file

使用ifstream和getline读取文件内容[c++]
//but all of the whitespace that separated words (including newlines) was lost.

使用ifstream和getline读取文件内容[c++]
voidReadDataFromFileWBW()

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] ifstream fin(
"data.txt");

使用ifstream和getline读取文件内容[c++]
strings;

使用ifstream和getline读取文件内容[c++]
while( fin>>s )

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] cout
<<"Read from file:"<<s<<endl;

使用ifstream和getline读取文件内容[c++] }


使用ifstream和getline读取文件内容[c++]}


使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分

使用ifstream和getline读取文件内容[c++]
//If we were interested in preserving whitespace,

使用ifstream和getline读取文件内容[c++]
//we could read the file in Line-By-Line using the I/O getline() function.

使用ifstream和getline读取文件内容[c++]
voidReadDataFromFileLBLIntoCharArray()

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] ifstream fin(
"data.txt");

使用ifstream和getline读取文件内容[c++]
constintLINE_LENGTH=100;

使用ifstream和getline读取文件内容[c++]
charstr[LINE_LENGTH];

使用ifstream和getline读取文件内容[c++]
while( fin.getline(str,LINE_LENGTH) )

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] cout
<<"Read from file:"<<str<<endl;

使用ifstream和getline读取文件内容[c++] }


使用ifstream和getline读取文件内容[c++]}


使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分

使用ifstream和getline读取文件内容[c++]
//If you want to avoid reading into character arrays,

使用ifstream和getline读取文件内容[c++]
//you can use the C++ string getline() function to read lines into strings

使用ifstream和getline读取文件内容[c++]
voidReadDataFromFileLBLIntoString()

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] ifstream fin(
"data.txt");

使用ifstream和getline读取文件内容[c++]
strings;

使用ifstream和getline读取文件内容[c++]
while( getline(fin,s) )

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] cout
<<"Read from file:"<<s<<endl;

使用ifstream和getline读取文件内容[c++] }


使用ifstream和getline读取文件内容[c++]}


使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
//带错误检测的读取方式

使用ifstream和getline读取文件内容[c++]
//Simply evaluating an I/O object in a boolean context will return false

使用ifstream和getline读取文件内容[c++]
//if any errors have occurred

使用ifstream和getline读取文件内容[c++]
voidReadDataWithErrChecking()

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++]
stringfilename="dataFUNNY.txt";

使用ifstream和getline读取文件内容[c++] ifstream fin( filename.c_str());

使用ifstream和getline读取文件内容[c++]
if(!fin )

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] cout
<<"Error opening"<<filename<<"for input"<<endl;

使用ifstream和getline读取文件内容[c++] exit(
-1);

使用ifstream和getline读取文件内容[c++] }


使用ifstream和getline读取文件内容[c++]}


使用ifstream和getline读取文件内容[c++]

使用ifstream和getline读取文件内容[c++]
intmain()

使用ifstream和getline读取文件内容[c++]使用ifstream和getline读取文件内容[c++]
{

使用ifstream和getline读取文件内容[c++] ReadDataFromFileWBW();
//逐词读入字符串

使用ifstream和getline读取文件内容[c++]
OutPutAnEmptyLine();//输出空行

使用ifstream和getline读取文件内容[c++]


使用ifstream和getline读取文件内容[c++] ReadDataFromFileLBLIntoCharArray();
//逐词读入字符数组

使用ifstream和getline读取文件内容[c++]
OutPutAnEmptyLine();//输出空行

使用ifstream和getline读取文件内容[c++]


使用ifstream和getline读取文件内容[c++] ReadDataFromFileLBLIntoString();
//逐词读入字符串

使用ifstream和getline读取文件内容[c++]
OutPutAnEmptyLine();//输出空行

使用ifstream和getline读取文件内容[c++]


使用ifstream和getline读取文件内容[c++] ReadDataWithErrChecking();
//带检测的读取

使用ifstream和getline读取文件内容[c++]
return0;

使用ifstream和getline读取文件内容[c++]}


输出结果为:

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 dataFUNNY.txt for input

Press any key to continue
原文链接: https://www.cnblogs.com/kevin2010_vip/archive/2010/02/03/1662853.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/7878

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月6日 下午6:30
下一篇 2023年2月6日 下午6:31

相关推荐