0

I am transitioning from C to C++ and I am trying to open and read an input file and simultaneously assign variables to the values read in. For example, I have 6 variables: a, b, c, x, y,z and my file: input.dat looks like this:

1     2     3
4     5     6

So in C I would write:

infile = fopen("input.dat","r");
fscanf(infile, "%d \t %d \t %d \n %d \t %d \t %d \n",&a,&b,&c,&x,&y,&z);

I am trying to do the same thing in C++ using ifstream but I am having trouble compiling a simple program:

#include <iostream>
#include <fstream>

using namespace std;

main(){
    int a, b, c, x, y, z;

    ifstream infile("input.dat", ifstream::in); //Open input.dat in input/read mode 

    if(infile.is_open()){
       /*read and assign variables from file - not sure how to do this yet*/
       return 0;
    } else {
        cout << "Unable to open file." << endl;
    }
    infile.close();

    return 0;
}

When I try to compile this I get a ton of errors thrown at me that all look something like:

 "Undefined reference to std::cout"

I am sure it is just some silly mistake but I can't seem to figure it out. I was trying to follow the syntax described in the examples at:http://www.cplusplus.com/doc/tutorial/files/

Question:

1.How to properly use fstream in the above code?

2.How input from files is read and assigned to variables. I understand it can be done with getline. Is it possible to use the extraction operator >> and if so, what would the syntax be for this example?

Ziezi
  • 6,375
  • 3
  • 39
  • 49
btown
  • 11
  • 2
    *How* are you compiling and, more importantly, linking? – Biffen Sep 30 '15 at 16:22
  • 2
    The code provided compiles OK after you add missing int to the main(). – SergeyA Sep 30 '15 at 16:23
  • 5
    So this has nothing at all to do with input from a file, and everything to do with just basic compilation of a C++ program. You should have tried a Hello World and spotted that the problem remains. Sounds like you're writing `gcc`, not `g++`. You also need a return type for `main`. – Lightness Races in Orbit Sep 30 '15 at 16:23
  • Based on your comments I found this thread which explains everything: http://stackoverflow.com/questions/3178342/compiling-a-c-program-with-gcc Thanks for the help - I didn't realise I couldn't use gcc without linking. Will use g++ from now on. – btown Sep 30 '15 at 17:06

2 Answers2

2

Unclear compilation (likely linking) issues aside, reading from stream is straightforward:

infile >> a >> b >> c >> d >> e;

will do the trick, assuming you have your data white-space delimited.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

Try this

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    int a, b, c, x, y, z;

    ifstream infile;
    infile.open("input.dat", ios::in); //Open input.dat in input/read mode 

    if (infile.is_open()) {

        infile >> a >> b >> c >> x >> y >> z;
        cout << a << b << c << x << y << z;
        infile.close(); //you close here since file would really be open here
        return 0;
    }
    else {
        cout << "Unable to open file." << endl;
    }

    return 0;
}

You could replace

infile >> a >> b >> c;

To

getline(infile, PUTSTRINGHERE);

If you wanted to get the whole line as a string variable, though you must include

#include <iostream>
Santosderek
  • 106
  • 6
  • Thanks, that is exactly what I was looking for with the >> operator. I specifically don't want it to be a string so I will use the first one. Do I need to specify anything to indicate new line?(i.e like I do in C with "\n") or will it automatically skip to the next non white space character it sees? – btown Sep 30 '15 at 17:11
  • It will automatically search for the next integer type, ignoring the "\n". Within the if statement if you add 'cout << a << b << c << x << y << z;' after recieving the integers you should see what I mean. – Santosderek Sep 30 '15 at 17:22