码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-10 12:56:50      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

 

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

技术分享Fry: One Jillion dollars.
技术分享[Everyone gasps.]
技术分享Auctioneer: Sir, that‘s not a number.
技术分享数据读取, 测试 。技术分享

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

技术分享#include <iostream>
技术分享#include <fstream>
技术分享#include <string>
技术分享
技术分享using namespace std;
技术分享
技术分享//输出空行
技术分享void OutPutAnEmptyLine()
技术分享{
技术分享    cout<<"\n";
技术分享}
技术分享
技术分享//读取方式: 逐词读取, 词之间用空格区分
技术分享//read data from the file, Word BWord
技术分享//when used in this manner, we‘ll get space-delimited bits of text from the file
技术分享//but all of the whitespace that separated words (including newlines) was lost
技术分享void ReadDataFromFileWBW()
技术分享{
技术分享    ifstream fin("data.txt");  
技术分享    string s;  
技术分享    while( fin >> s ) 
技术分享    {    
技术分享        cout << "Read from file: " << s << endl;  
技术分享    }
技术分享}
技术分享
技术分享//读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
技术分享//If we were interested in preserving whitespace, 
技术分享//we could read the file in Line-By-Line using the I/O getline() function.
技术分享void ReadDataFromFileLBLIntoCharArray()
技术分享{
技术分享    ifstream fin("data.txt"); 
技术分享    const int LINE_LENGTH = 100
技术分享    char str[LINE_LENGTH];  
技术分享    while( fin.getline(str,LINE_LENGTH) )
技术分享    {    
技术分享        cout << "Read from file: " << str << endl;
技术分享    }
技术分享}
技术分享
技术分享//读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
技术分享//If you want to avoid reading into character arrays, 
技术分享//you can use the C++ string getline() function to read lines into strings
技术分享void ReadDataFromFileLBLIntoString()
技术分享{
技术分享    ifstream fin("data.txt");  
技术分享    string s;  
技术分享    while( getline(fin,s) )
技术分享    {    
技术分享        cout << "Read from file: " << s << endl; 
技术分享    }
技术分享}
技术分享
技术分享//带错误检测的读取方式
技术分享//Simply evaluating an I/O object in a boolean context will return false 
技术分享//if any errors have occurred
技术分享void ReadDataWithErrChecking()
技术分享{
技术分享    string filename = "dataFUNNY.txt";  
技术分享    ifstream fin( filename.c_str());  
技术分享    if!fin ) 
技术分享    {   
技术分享        cout << "Error opening " << filename << " for input" << endl;   
技术分享        exit(-1);  
技术分享    }
技术分享}
技术分享
技术分享int main()
技术分享{    ReadDataFromFileWBW(); //逐词读入字符串     OutPutAnEmptyLine(); //输出空行    ReadDataFromFileLBLIntoCharArray(); //逐词读入字符数组    OutPutAnEmptyLine(); //输出空行    ReadDataFromFileLBLIntoString(); //逐词读入字符串    OutPutAnEmptyLine(); //输出空行    ReadDataWithErrChecking(); //带检测的读取    return 0;}

输出结果为:
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
 
 

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

标签:

原文地址:http://www.cnblogs.com/jkred369/p/4414069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!