getline()

S

salvo

Hi,

I'm having some trouble in parsing a simple text file which is formatted
as follows:

....
string\tstring\tstring\n
....

I tried to parse it with the following statments:

....
getline( file, string1, '\t' ); //first string
file.get(ch); //first \t
getline( file, string2, '\t' ); //second string
file.get(ch); //second \t
getline( file, string3, '\t' ); //third string
file.get(ch); // \n
....

As I read in some documentation, the getline doesn't put the \t in the
buffer. That's why I use the get() method. Anyhow, the file is not parsed
correctly. I tried to debug it with gdb, but I still don't get what I am
doing wrong.

Thanks in advance for any suggestion.


Regards
Salvo
 
W

wittempj

Just stream the file into strings, one read action will stop when it
encounters whitespace.
-#include <iostream>
-#include <vector>
-#include <fstream>
-#include <string>

-using namespace std;
-
-
-int main()
-{
- vector<string> v;
- ifstream f;
- f.open("c:\\temp\\data.txt");
-
- string s;
- while( f >> s )
- v.push_back(s);
- f.close();
-
- for(vector<string>::iterator it=v.begin(); it!=v.end(); ++it)
- {
- cout << *it;
- }
- cout << endl;
-
- return 0;
-}
 
S

Shezan Baig

salvo said:
Hi,

I'm having some trouble in parsing a simple text file which is formatted
as follows:

...
string\tstring\tstring\n
...

I tried to parse it with the following statments:

...
getline( file, string1, '\t' ); //first string
file.get(ch); //first \t
getline( file, string2, '\t' ); //second string
file.get(ch); //second \t
getline( file, string3, '\t' ); //third string


This should be delimited by '\n' (according to your sample file)

file.get(ch); // \n
...

As I read in some documentation, the getline doesn't put the \t in the
buffer. That's why I use the get() method. Anyhow, the file is not parsed
correctly. I tried to debug it with gdb, but I still don't get what I am
doing wrong.

Thanks in advance for any suggestion.


Regards
Salvo

This is what the documentation at cplusplus.com says:

"If the delimiter is found it is extracted but not not stored. Use get
if you don't want this character to be extracted."

So you don't need to call file.get(ch), because the delimiter is
already extracted (but not stored in your output string).

Hope this helps,
-shez-
 
S

salvo

This should be delimited by '\n' (according to your sample file)

Yes. I mistyped.
This is what the documentation at cplusplus.com says:

"If the delimiter is found it is extracted but not not stored. Use get
if you don't want this character to be extracted."

So you don't need to call file.get(ch), because the delimiter is
already extracted (but not stored in your output string).

Thanks, now it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top