13

I have this in a text file:

John 20 30 40
mike 30 20 10

How do i read from the text file and separate them into variable name, var1, var2, var3. This is my attempt, seems it doesnt work. Help please.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main () {
  string name,result;
  int number1;
  ifstream myfile ("marks.txt");
  if (myfile.is_open())
  {
    while ( !myfile.eof() )
    {
      getline (myfile,name,'\t');
      getline (myfile,var1,'\t');
      getline (myfile,var2,'\t');
      getline (myfile,var3,'\t');
      cout << name << var1 << var2 << var3;


    }

    myfile.close();

  }

  else cout << "Unable to open file";

  return 0;
}

EDIT 1:

Nocturne Suggestion:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;


int main()
{
    ifstream inputFile("marks.txt");
    string line;

    while (getline(inputFile, line))
    {
        istringstream ss(line);

        string name;
        int var1, var2, var3;

        ss >> name >> var1 >> var2 >> var3;
        cout << name << var1 << var2 << var3 << endl << endl;
    }
}

output:

John203040

mike302010

302010

Why another 302010???

diehell
  • 739
  • 2
  • 9
  • 19
  • Is your input file definitely tab-delimited? What output do you get? – Oliver Charlesworth Oct 15 '10 at 22:33
  • "seems it doesn't work" is not helpful. Please give details. – JoshD Oct 15 '10 at 22:41
  • "Seems it doesn't work" is not an adequate description of the problem. What are the symptoms that lead you to the conclusion that this code does not do what you expect it to do? Have you tried attaching a debugger to step through the program and see what is not working correctly? – James McNellis Oct 15 '10 at 22:41

3 Answers3

16

Something like this should work (I don't have a compiler handy, so you may need to tweak this a little):

#include <iostream>
#include <sstream>
using namespace std;


int main()
{
    ifstream inputFile("marks.txt");
    string line;

    while (getline(inputFile, line))
    {
        istringstream ss(line);

        string name;
        int var1, var2, var3;

        ss >> name >> var1 >> var2 >> var3;
    }
}

Edit: Just saw this again, I don’t know why I chose the get line approach earlier. Doesn’t the following (simpler solution) work?

#include <fstream>
using namespace std;

int main()
{ 
    ifstream fin(“marks.txt”);

    string name;
    int var1;
    int var2;
    int var3;

    while (fin >> name >> var1 >> var2 >> var3)
    {
        /* do something with name, var1 etc. */
        cout << name << var1 << var2 << var3 << “\n”;
    }
}
Debajit
  • 46,327
  • 33
  • 91
  • 100
  • Nocturne, Yours work fine. I added #include and cout << name << var1 << var2 << var3 << " "; it prints John203040 mike302010 302010 Why another 302010 ??? – diehell Oct 15 '10 at 22:58
  • You must have a blank line at the end of your file. – Debajit Oct 15 '10 at 23:19
4

It looks like you need to declare var1, var2, and var3.

Also instead of this:

      getline (myfile,name,'\t');
      getline (myfile,var1,'\t');
      getline (myfile,var2,'\t');
      getline (myfile,var3,'\t');

Try this:

  myfile >> name;
  myfile >> var1;
  myfile >> var2;
  myfile >> var3;

Not because what you had is wrong, but the second is cleaner and will handle all white space.

JoshD
  • 12,490
  • 3
  • 42
  • 53
0

I think that when the numbers were printed out in a group with no spaces, the reason was just that you need to put spaces in between the variable, like this:

cout << name << " " << var1 << " " << var2 << " " << var3 << "\n";
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Lisa
  • 1
  • 1