`

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

阅读更多

假设有一个叫 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 By Word
//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读取文件

    ### 使用`ifstream`和`getline`读取文件 在C++编程语言中,处理文件是一项基本而重要的功能。本文档将详细介绍如何使用`ifstream`类和`getline`函数来读取文件的不同方法,并通过示例代码展示每种方法的具体实现。 ...

    c++读取csv文件

    本文档将详细讲解如何在Visual Studio 2013环境下使用C++语言读取CSV文件,并通过动态分配数组来存储读取到的数据。 首先,我们需要包含必要的头文件,以便使用文件流操作。在C++中,`&lt;fstream&gt;`头文件提供了处理...

    C++_C++_读取txt文档_txt内容读取_按空格分隔_按逗号分隔_

    本篇文章将详细讲解如何使用C++来读取txt文档,并按空格和逗号进行内容分隔。 首先,我们需要理解C++中文件I/O的基础。C++提供了fstream库来处理文件操作,主要包括ifstream(输入文件流)和ofstream(输出文件流)...

    C++:使用getline读取文本文件

    之前在使用C++中的getline读取文本文件时由于没有仔细看getline的定义,导致出了错:在读取文本文件时未读取到文件中的第一行。 错误的源代码如下: vectorreadfile&#40;string s1,vectorv1&#41; { ifstream infile...

    C++保存和读取txt文件

    在C++编程中,保存和读取数据到文本文件(如txt文件)是常见的操作,这对于数据持久化、日志记录或用户配置的存储都至关重要。在这个特定的程序中,我们关注的是如何将当前系统时间保存到txt文件,并在之后读取这个...

    C++读取CSV文件

    本文将详细介绍使用C++语言读取CSV文件的方法和过程。 一、使用ifstream读取CSV文件 在C++中,可以使用ifstream类来读取CSV文件。ifstream是C++标准库中的输入流类,用于读取文件。下面是一个简单的示例代码: ``...

    读取TXT文件程序C++

    在本文中,我们将深入探讨如何使用C++语言编写一个程序来读取包含汉字、数字、字符和字母的TXT文件。 首先,C++中的文件I/O是通过`fstream`库实现的,所以我们需要在代码开头包含这个库: ```cpp #include #...

    C文本文件读取

    在C++语言中,读取文本文件可以使用iostream库中的函数,例如ifstream类。ifstream类提供了一个流式接口,用于读取文本文件。 在C++语言中,读取文本文件的基本步骤如下: 1. 打开文件:使用ifstream类打开要读取...

    linux C++ 文本文件创建写入以及读取

    使用`ifstream`对象来读取文件内容。例如: ```cpp std::ifstream infile("test.txt"); if (!infile) { std::cerr !" ; return -1; } std::string line; while (std::getline(infile, line)) { std::cout...

    C++-读取txt.pdf

    如果文件打开成功,我们可以使用getline()函数逐行读取文件内容: ```cpp string line; while (getline(input_file, line)) { cout ; } ``` 最后,我们需要关闭文件,以释放系统资源: ```cpp input_file.close(); ...

    C++读取文件源码

    本篇文章将详细讲解如何利用C++来读取这些不同类型的文件,以及源码中可能涉及的关键技术和方法。 首先,我们从最基础的文本文件开始。在C++中,我们可以使用标准库中的`fstream`类来实现文件的读取。例如,读取一...

    用C++实现MapInfo 的mif文件读取(源码)

    C++中可以使用STL库中的`std::ifstream`类来读取文件,通过`std::getline()`函数获取每一行,然后使用字符串分割功能来解析数据。为了存储和操作几何对象,我们可以利用`std::vector`来保存点坐标,利用自定义的类来...

    C++文件读写操作C++文件读写操作

    - 对于文本文件,可以使用`getline()`函数逐行读取。例如,`getline(file, line)`将文件中的一行读取到字符串`line`中。 - 使用`&gt;&gt;`操作符可以读取文件中的数据字段。例如,`file &gt;&gt; number;`会将一个整数从文件中...

    c++读取配置文件

    本教程将详细讲解如何使用C++读取配置文件,特别是针对TXT格式的文件,因为它们简单且易于理解和编辑。 1. **基本概念** - **配置文件**:配置文件通常存储应用程序的设置,如数据库连接字符串、用户首选项或系统...

    C++实现逐行从文本文件读取数据

    总之,C++通过`ifstream`类和`getline()`函数提供了一种简单而强大的方式来逐行读取文本文件。`example.cpp`和`B.txt`的组合展示了这一功能的实际应用,可以帮助我们更好地理解和实践这种编程技术。在开发涉及大量...

    c++输入文件流ifstream用法详解_ims的博客-CSDN博客1

    如果文件打开成功,我们就使用`getline`函数逐行读取文件内容并打印出来。最后,确保文件被正确关闭。 ### 错误处理 在进行文件I/O操作时,要时刻注意检查文件流的状态。如果在读取过程中遇到错误,比如文件不存在...

    读取txt文件的c++程序将特定数据从txt文件中摘出来

    2. 使用`getline`函数读取文件的每一行。 3. 字符串操作,如`find`和`substr`,用于查找和提取数据。 4. 错误处理,如检查文件是否成功打开。 5. 主函数`main`的组织结构和基本流程控制。 通过这样的程序,我们可以...

    C++配置文件读取

    本篇文章将深入探讨在Visual C++ 2010环境下,如何使用纯C++代码实现配置文件的读取与写入。 1. 文件I/O基础 在C++中,我们主要通过标准库中的`fstream`类来处理文件输入输出。`ifstream`用于读取文件,`ofstream`...

Global site tag (gtag.js) - Google Analytics