0

I have a text file that I am trying to read several lines of data in from with ifstream so I can then add them to a linkedlist. I'm using a main class/function that my teacher provided me so I did not personally write any of it. I've written a few other classes to go along with it which I can provide if necessary, but when debugging, the issues start as soon as the main function starts to try and read in the data.

#include <iostream>
#include <fstream>
#include "datalogger.h"

using namespace std;

int main(int argc, char** argv) {

datalogger dl;

if (argc != 2) {
    cout << "Usage: " << argv[0] << " <datafile>" << endl;
    exit(0);
}

// Read the data

char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;

while (!infile.eof()) {
    infile >> timestamp;
    infile >> temperature;
    infile >> windspeed;

    if (!infile.eof()) {
        dl.addData(timestamp, temperature, windspeed);
    }
}

// Output the report
dl.printReport();

return(0);
}

The contents of the datafile (smallerdata.txt) are as follows:

1480906168 -226 361
1480906168 -224 270
1480906175 -222 326
1480906179 -218 236
1480906187 -218 145
1480906189 -216 109
1480906189 -212 145
1480906190 -208 153
1480906197 -204 90

The timestamp for the first line read in should be 1480906168 & the temperature and windspeed should be -226 & 361 respectively. Instead, my debugger is giving me these values while paused on breakpoints on lines 24-27:

timestamp = 0

temperature = 2.1219889530967339e-314

windspeed = 3.184022971431627e-314

Where the heck are these values coming from & why?

ThomasJazz
  • 99
  • 9
  • Did you provide the valid path as an argument. Can you print or inspect the `argv[1]` value? – K. Kirsz Jul 10 '17 at 07:01
  • 1
    [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/q/5605125/1025391) – moooeeeep Jul 10 '17 at 07:09
  • @moooeeeep That is a great point. This main function is generally poorly written, but still it should work for the example data. – K. Kirsz Jul 10 '17 at 07:11

2 Answers2

0

I suggest you to add check that file is opened, like:

ifstream infile(datafile);
if (!infile.is_open())
{
    cout << "File " << argv[1] << " cannot be open!" << endl;
    exit(0);
}

And also change the condition for loop, e.g.:

while (infile.good()) {
    infile >> timestamp;
    infile >> temperature;
    infile >> windspeed;
    // just to simplify debugging
    cout << "timestamp   = " << timestamp << endl
        << "temperature = " << temperature << endl
        << "windspeed   = " << windspeed << endl;
    // your data processing
    if (!infile.eof()) {
         dl.addData(timestamp, temperature, windspeed);
    }
}
VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • I can't change any of the code in the main function as it was provided to us and "not to be changed". I pasted the conditional though and it looks as though it isn't even opening the file. Really not sure what to do if it can't open the file but I'm not allowed to edit the source code. – ThomasJazz Jul 10 '17 at 06:35
  • So, use debugger to see that file is really opened, and if no, check path to file you provide in the command line while run the program – VolAnd Jul 10 '17 at 06:39
0

Why you reading into double when you have integer values in the file ?

double temperature;
double windspeed;

Should be

int temperature;
int windspeed;
kj192
  • 1
  • 3
  • ThomasJazz said in comments to my answer, that provided code snippet cannot be changed, so data types can be `int` for all data represented in file sample, but question is not about data type, and any integer input in decimal representation can be read to `double` or `float` – VolAnd Jul 10 '17 at 13:47