getline

Hi

I'm trying to read data from a file:

Graham, 21, Male, 24606038, a1, yellow,

The data is in different formats, I could write it each on a new line.

getline seems only to read one part or it all as one string.

What would be the best method to read the items please?

Thanks again,

Graham
getline seems only to read one part or it all as one string.

That makes zero sense...
You could take the C approach and use a while loop and getchar to parse the text, since each piece of information you want is separated by a comma you could just iterate through everything and divide up the pieces as you want.
Try something like that
1
2
3
4
int c;
while((c = getchar()) != myfile.eof()) {
// whatever you want...
}
Last edited on
Oh, I'm even slower than normal today.

could I use ifstream ?
I have one line with 6 different entries separated by a ","
Thanks
Graham
You could use std::getline with a comma passed in as the delimiter to read up to the comma.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <fstream>
using namespace std;

const int NUM_DATA = 6;

string data[NUM_DATA];

ifstream inputFile("myFile.txt");

for(int i = 0; i < NUM_DATA; i++)
{
     getline( inputFile, data[i], ',' );

     //do the following to discard the space after the comma
     getline( inputFile, string(), ' ' );
}

inputFile.close();

//now, data[] will store everything between the commas 


You can use a std::stringstream to convert from string to numerical where needed.

http://www.cplusplus.com/reference/iostream/stringstream/
To discard whitespace, use the ws manipulator.

1
2
3
4
5
6
7
8
9
10
11
12
13
string record;
while (getline( f, record ))
  {
  istringstream ss( s );
  string field;
  while (getline( ss, field, ',' ))
    {
    // save, process, whatever with field
    ...
    // skip any leading whitespace
    ss >> ws;
    }
  }

Hope this helps.
Hi again,

Many thanks, I'm nearer now!

The program is throwing this error now though:

Unhandled exception at 0x5147c9c7 (msvcr100d.dll) in read survey.exe: 0xC0000005: Access violation reading location 0xcccccccc.

The latest code is below:


// passwordset.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
const int NUM_DATA = 6;

string data[NUM_DATA];

ifstream inputFile("C:/users/graham/documents/survey.txt");

for(int i = 0; i < NUM_DATA; i++)
{
getline( inputFile, data[i], ',' );
cout << "/nData = " << data[1]

//do the following to discard the space after the comma
getline( inputFile, string(), ' ' );
}

inputFile.close();

string name;
string age;
string sex;
string number;
string classno;
string favcolour;


name = data[1];
age = data[2];
sex = data[3];
number = data[4];
classno = data[5];
favcolour= data[6];

// input section
cout << "Your name is ";
cout << name << endl;
cout << "Your age is ";
cout << age << endl;
cout << "You are a " ;
cout << sex << endl;
cout << "Your school number is" ;
cout << number << endl;
cout << " Your class is" ;
cout << classno << endl;
cout << "Your favourite colour is " ;
cout << favcolour << endl;
cout << "\ninput complete" << endl;



//now, data[] will store everything between the commas


//old part
// const int MAX_CHARS_PER_LINE = 1000;
// string passw;
// ifstream datain("C:/users/graham/documents/survey.txt");
// char charsFromFile[MAX_CHARS_PER_LINE];
// datain.getline(charsFromFile, MAX_CHARS_PER_LINE);
//
// cout << "Enter the your name " << endl;
// cin >> passw;
// if (passw == charsFromFile)
// {
// cout << "Enter friend :)"<< endl;
// cout << "\n Password=";
// cout << passw << endl;
// }
//
// else
// {
// cout << "Intruder! Leave now!"<< endl;
// cout << "\n Password=";
// cout << passw << endl;
// cout << charsFromFile << endl;
// }

//wait for return key
cin.get () ;
system( "PAUSE" );
return 0;
}

So, you've got a memory access error. I wonder why. *cough* getline( foo, string(), ... ) *cough*

I'm going away.

You'll see this value again. It's worth learning what it means.


http://www.davekb.com/browse_programming_tips:0xcccccccc:txt
Sorry I am a beginner ...

I have this code:

for(int i = 0; i < NUM_DATA; i++)
{
getline( inputFile, data[i], ',' );
cout << "/nData = " << data[1]

Getline in my manual uses parameters for location, output string and delimiter.

I don't know enough to see what's wrong :'(

Graham
So, you've got a memory access error. I wonder why. *cough* getline( foo, string(), ... ) *cough*

That just puts a space (if there is one) into a temporary string. I tried similar code and it runs fine for me (ws is the real solution, though I didn't know about that).

That said, GrameR, instead of this

getline( inputFile, string(), ' ' );

do this

inputFile >> ws;

The source of your access violation is due to going over the array bounds. If your array has 6 elements, then the valid indeces for it are 0 through 5, not 1 through 6.
I tried similar code and it runs fine for me ...
Don't take this the wrong way, but so what? What you are doing is Wrong, and shouldn't work. Just because it works for you at the present time doesn't make is safe or correct.

The source of your access violation is due to going over the array bounds. ...

Nice catch! :-)
(It is hard for me to pay attention when OP refuses to use [code] blocks.)
Duoas wrote:
What you are doing is Wrong, and shouldn't work

Not very legible, granted, but not Wrong.

C++ 2003 (12.2.5): A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.
Last edited on
Brilliant!

Thank you very much everyone for all the help and advice :))

Graham
Hmm, I have to retract. Apparently you can bind to non-const reference temporaries in a function call...

Nevertheless, it still isn't a good idea, as it apparently causes problems beyond the obvious.

Also, getline() stops at the first delimiter it finds, not the last in sequence.
I never said it was a good idea :)

I was lazy and just wanted to avoid declaring another variable. Had I known about ws, I would have recommended that.
Last edited on
Topic archived. No new replies allowed.