【问题标题】:Not Fully read from the text file未完全从文本文件中读取
【发布时间】:2015-07-31 14:05:02
【问题描述】:

我正在尝试从 C++ 中的文本文件中读取一些整数。这是一个奇怪的问题,它只读取 13 个数字,但我的文件包含 25 个数字。我搜索但没有找到任何东西,有人建议添加ios::binary,但没有工作。 为什么?? 这是这部分代码。

#include <iostream>
#include <vector>
#include <cmath> 
#include <fstream>
using namespace std;

int main()
{

    ifstream myfile;
    myfile.open("Nvector.txt");
    vector<int> N;
    for(int j=0; j<25; j++)
    {
        int input;
        myfile>> input;
        N.push_back (input);
    }

    system("PAUSE");
    return 0;

}

【问题讨论】:

  • 文件呢?
  • 你应该像这样检查myfile&gt;&gt; input;的结果:if(!(myfile&gt;&gt; input)) { std::cout &gt;&gt; "Error" &gt;&gt; std::endl; }
  • 抱歉打错了:std::cout &lt;&lt; "Error" &lt;&lt; std::endl; 当然。
  • @πάνταῥεῖ 我发现问题不在于文件,可能与矢量有关。

标签: c++ filereader


【解决方案1】:

您可能想检查您正在读取的文本文件,可能存在特殊的行尾字符会导致整数从向量中连接/删除,也可能存在与向量相关的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多