I am trying to read data from an input file that contains an integer on the first line ( represents the number of images listed in the file ), a float on the second line( this is used for other calculations within the main program ) and float values on each subsequent line that end with a name of an image file all in the same data.txt file. The last line of the data.txt file contains a brief description of what the data is used for but is not read by the program. When I try to read the data and print it out to the screen the values are incorrect. The first line of the data.txt file is a 5 but when I print it out I get a 0 which is what I initialized it to. The second line is a float value I believe and that is also output as a 0 which is what it is initialized to also. The rest of the data is read in by a while loop with only parts of the data being printed out but nothing prints. I inserted a cout << inputFile.tellg << endl; statement to see where the file pointer is pointing to but it is returning -1. I am totally stuck on this. Any insight would greatly be appreciated. Thank you for your time and expertise.
Please find attached a sample copy of the data.txt file as well as the main.cpp file.
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
main.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;
}
}
}
}
OUTPUT as follows:
os:~/Desktop/test$ ./main data.txt
begin
-1
N is now 0
R is now 0
-1
At a minimum I would expect N to be 5 since I may have the type wrong for R or maybe more calculations are required once the data is read, I am not sure. I just don't get why the file pointer is showing it is at location -1.
The problem is most likely caused by the declaration of R
.
auto R = 0;
The above declaration makes R
an int
, not a double
or float
.
Use
double R = 0;
You could use
auto R = 0.0;
but I don't recommend it. Use of auto
makes sense when the type is long winded and cumbersome to type. For simple types, as above, it is better to be explicit.
If you need to use float
for R
, use
float R = 0;