0

I am trying to read a text file of the format:

5
1.00   0.00
0.75   0.25
0.50   0.50
0.25   0.75
0.00   1.00

The code is:

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

int totalDataPoints; // this should be the first line of textfile i.e. 5
std::vector<double> xCoord(0); //starts from 2nd line, first col
std::vector<double> yCoord(0); //starts from 2nd line, second col
double tmp1, tmp2;

int main(int argc, char **argv)
{
    std::fstream inFile;

    inFile.open("file.txt", std::ios::in);

    if (inFile.fail()) {
        std::cout << "Could not open file" << std::endl;
        return(0);
    } 

    int count = 0;

     while (!inFile.eof()) { 
         inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
         count++;
     }

     for (int i = 0; i < totalDataPoints; ++i) {
         std::cout << xCoord[i] << "    " << yCoord[i] << std::endl;
     }
    return 0;
}

I am not getting results. My final aim is to put this as function and call the x, y values as an object of a class.

TheCoder
  • 55
  • 1
  • 8
  • You never set `totalDataPoints` to anything. Similar: [C++ read from file](http://stackoverflow.com/questions/8943949/c-read-from-file) – 001 Nov 05 '15 at 18:05
  • Please do not test `if(stream.fail())` or `if(stream.eof())`, unless you know what are you doing. Just test `if(stream)`. –  Nov 05 '15 at 18:06
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Simon Kraemer Nov 05 '15 at 18:07
  • Possible duplicate of [Read Numeric Data from a Text File in C++](http://stackoverflow.com/questions/14516915/read-numeric-data-from-a-text-file-in-c) – Simon Kraemer Nov 05 '15 at 18:08
  • What happened when you stepped through this code in a debugger? Was it what you expected? – MrEricSir Nov 05 '15 at 18:09

2 Answers2

1

int totalDataPoints; is a global variable and since you are not initializing it with a value it is going to be 0 initialized. Then in your for loop

for (int i = 0; i < totalDataPoints; ++i) {
     std::cout << xCoord[i] << "    " << yCoord[i] << std::endl;
}

You are going to do anything since i < totalDataPoints(0 < 0) is false. I suspect you meant to use

for (int i = 0; i < count; ++i) {
     std::cout << xCoord[i] << "    " << yCoord[i] << std::endl;
}

Or have

totalDataPoints = count;

Before the for loop.

I would also suggest you do not use while (!inFile.eof()) to control the reading of the file. To fix it You can use

 while (inFile >> tmp1 && inFile >> tmp2) { 
     xCoord.push_back(tmp1);
     yCoord.push_back(tmp2);
     count++;
 }

This will ensure the loop will only run while there is data to read. For more information see: Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • yes, it works. for totalDataPoints = xCoord.size(); I removed total points from text file i.e. 5. now I can see the results. How can I make a Class file for this? – TheCoder Nov 05 '15 at 18:29
  • @TheCoder *Class file*? What is that? – NathanOliver Nov 05 '15 at 18:31
  • @TheCoder: It's not maintained according your input which take file.txt cz first line in file.txt is totalDataPoints 5. According this code, you can not take this – Sakib Ahammed Nov 05 '15 at 18:43
1

Just a simple change needed in your code. You can not take totalDataPoints which is provide from file.txt in first line. Then you take every line until reach last.

int count = 0;

    inFile>>totalDataPoints;

     while (!inFile.eof()) {
         inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
         count++;
     }

By for loop you can do this like, Here int count = 0 is unnecessary:

inFile>>totalDataPoints;

    for (int i=0; i<totalDataPoints; i++)
    {
        inFile >> tmp1;
         xCoord.push_back(tmp1);
         inFile >> tmp2;
         yCoord.push_back(tmp2);
    }
Sakib Ahammed
  • 2,452
  • 2
  • 25
  • 29