【问题标题】:C++ Reading fstream data from a file does not return correct values. inputFile.tellg returns -1C++ 从文件中读取 fstream 数据不会返回正确的值。 inputFile.tellg 返回 -1
【发布时间】:2019-02-24 04:38:12
【问题描述】:

我正在尝试从输入文件中读取数据,该输入文件在第一行包含整数(表示文件中列出的图像数量),第二行包含浮点数(用于主程序中的其他计算) 并在以图像文件名称结尾的每个后续行上浮动值,所有这些值都在同一个 data.txt 文件中。 data.txt 文件的最后一行包含数据的用途但程序不读取的简要说明。 当我尝试读取数据并将其打印到屏幕上时,值不正确。 data.txt 文件的第一行是 5,但是当我将其打印出来时,我得到一个 0,这就是我将它初始化的内容。第二行是一个浮点值,我相信它也输出为 0,这也是它的初始化值。 其余数据由 while 循环读取,仅打印出部分数据,但不打印任何内容。 我插入了一个 cout

请在附件中找到 data.txt 文件和 main.cpp 文件的示例副本。

data.txt

5
5.50e+11
 4.4960e+11  0.0000e+00  0.0000e+00  4.9800e+04  5.9740e+24       cat.gif
 3.2790e+11  0.0000e+00  0.0000e+00  3.4100e+04  6.4190e+23       dog.gif
 2.7900e+10  0.0000e+00  0.0000e+00  7.7900e+04  3.3020e+23     mouse.gif
 0.0000e+00  0.0000e+00  0.0000e+00  6.0000e+00  1.9890e+30  squirrel.gif
 5.0820e+11  0.0000e+00  0.0000e+00  7.5000e+04  4.8690e+24       fox.gif

This file contains an example of data stored in a text file 

ma​​in.cpp

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

int main( int argc, char* argv[ ] )
{
      float xPosition, yPosition;
      float xVelocity, yVelocity;
      float animalMass;
      string imgFilename;
      string line;
      istringstream inputStream( line );
      auto N = 0;   // Number of items
      auto R = 0; // Real number from file

  cout << "begin" << endl;
  if( argc > 1 )
  {
    string fileName = argv[ 1 ];
    ifstream inputFile( fileName );
    inputFile.open( fileName, ios::in );

    if( !inputFile.is_open( ))
    {
      cout << setw( 5 ) << " " << "Could not open file " << fileName << "." << endl;
      cout << setw( 5 ) << " " << "Terminating program." << endl;
      exit( 1 );
    }
    else
    {
      cout << inputFile.tellg() << endl;
      inputFile >> N;
      inputFile >> R;
      cout << "N is now " << N << endl;
      cout << "R is now " << R << endl;
      cout << inputFile.tellg() << endl;

      while( inputFile >> xPosition >> yPosition
                       >> xVelocity >> yVelocity
                       >> animalMass   >> imgFilename )
      {
        cout << xPosition << " " << imgFilename << endl;
      }       
    }
  } 
}

输出如下:

os:~/Desktop/test$ ./main data.txt

开始

-1

N 现在是 0

R 现在是 0

-1


至少我希望 N 为 5,因为我的类型可能是错误的 读取数据后可能需要 R 或更多计算,我不确定。 我只是不明白为什么文件指针显示它位于位置 -1。

【问题讨论】:

  • 尝试删除inputFile.open( fileName, ios::in );,因为上一行已经打开了文件。另外,声明float R = 0 而不是auto R = 0
  • 它有效。这正是问题所在。我删除了 inputFile.open(fileName, ios::in) 并将 R 声明为 float 而不是 auto。非常感谢你。我不确定为什么前面的语句打开了文件。我认为有必要显式调用 inputFile.open(fileName, ios::in) 来打开文件。

标签: c++ file-io fstream ifstream file-pointer


【解决方案1】:

问题很可能是由R 的声明引起的。

auto R = 0;

上述声明使R 成为int,而不是doublefloat

使用

double R = 0;

你可以使用

auto R = 0.0;

但我不推荐它。当类型冗长且难以键入时,使用auto 是有意义的。对于简单类型,如上所述,最好是显式的。

如果您需要将float 用于R,请使用

float R = 0;

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多