从C ++中的.txt文件读取时出错 [英] Error in reading from a .txt file in c++

查看:80
本文介绍了从C ++中的.txt文件读取时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    //reading the text file
    ifstream inputFile("testfile1.txt");
    inputFile.open("testfile1.txt");
    while(!inputFile.eof())
    //eof till end of file, reads the txt till end of file
    {
        string str;
        getline(inputFile,str);
        cout <<str<< endl;

    }
        inputFile.close();

    return 0;
}

//我遇到的问题是它无法读取文件或其中的任何内容.不执行任何操作,表明程序以退出代码结尾:0.任何人都可以检查代码中的错误

// The problem that i am having is that it doesn not read the file or anything in it. Doing nothing it says Program ended with exit code: 0. Could anyone check the mistake in the code

推荐答案

第一个错误:您两次打开输入文件.根据C ++标准,关于第二个打开请求(直接调用 open 成员)的行为:

First Bug: You are opening the input file twice. Per the C++ standard, regarding the behavior of your second open request (the direct call to the open member):

C ++ 11§27.9.1.9 [ifstream.members/3]

void open(const char * s,ios_base :: openmode mode = ios_base :: in);

效果:调用 rdbuf()-> open(s,mode | ios_base :: in).如果那个功能不返回空指针调用clear(),否则调用setstate(failbit)(可能会引发ios_base :: failure(27.5.5.4)).

Effects: Calls rdbuf()->open(s, mode | ios_base::in). If that function does not return a null pointer calls clear(), otherwise calls setstate(failbit) (which may throw ios_base::failure (27.5.5.4)).

因此

会问一个问题, rdbuf()-> open(...)是做什么的?好吧, std :: ifstream 使用 filebuf 进行缓冲,并再次按照标准:

which therefore asks the question, what does rdbuf()->open(...) do ? Well, a std::ifstream uses a filebuf for it's buffering, and once again, per the standard:

C ++ 11§27.9.1.4[filebuf.members/2]

basic_filebuf< charT,traits> * open(const char * s,ios_base :: openmode模式);

效果:如果 is_open()!= false,则返回空指针.否则,根据需要初始化filebuf....

Effects: If is_open() != false, returns a null pointer. Otherwise, initializes the filebuf as required. ...

简而言之,双重打开会使流进入失败状态,这意味着从那时起所有与数据相关的操作都将彻底失败.

In short, your double-open is putting your stream into a fail-state, which means all data-related operations with it are going to fail outright from that point on.

第二个错误:在循环条件表达式中不正确使用.eof.修复第一个错误后,您将遇到此问题.在以下问题中,解释不正确的原因要比我在这里可以解释的要好得多.

Second Bug: Improper use of .eof in a loop conditional expression. you'll run into this once you fix the first bug. The reasons this is not being done correctly are explained in the following question far better than I can explain it here.

为什么在循环条件内考虑iostream :: eof错误吗?

可以说,检查您的IO操作,而不仅仅是检查流的eof状态.养成这个习惯并坚持下去.

Suffice it to say, check your IO operations, not just the eof-state of the stream. Get into that habit and stick with it.

修复这两个问题,您的代码实际上可以简化为以下形式:

Fixing both, your code can literally be reduced to simply this:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream inputFile("testfile1.txt");
    std::string str;
    while (std::getline(inputFile, str))
        std::cout << str << std::endl;
}

很显然,如果您要编写更健壮的代码,则可能要在其中执行一些错误处理,例如:

Obviously if you're shooting for more robust code, you probably want to perform some error handling in there, something like:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main()
{
    std::ifstream inputFile("testfile1.txt");
    if (!inputFile)
    {
        std::cerr << "Failed to open file\n";
        return EXIT_FAILURE;
    }

    std::string str;
    while (std::getline(inputFile, str))
        std::cout << str << std::endl;
}

这篇关于从C ++中的.txt文件读取时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆