user1326934
user1326934

Reputation: 3

How to read Data that include space

I have a txt file that include information as follow:

"

Jack

Jim

Bill's Father

"

I have no problem reading the all the data with ifstream using something like:

ifstream myReadFile; myReadFile.open(mFilePath);

myReadFile >>a>>b>>c;

But I find that the last one only has "Bill's" without "Father" because they are separated with space. So I wondering if there is a way to include space in one datum and separate them only with \t or endl (tab or new line) Thank you!!

Upvotes: 0

Views: 96

Answers (1)

Castilho
Castilho

Reputation: 3187

It can be done with getline:

myReadFile.getline(a,sizeof(a));

This will read a maximum of sizeof(a) characters from the stream until the end of the line.

With ifstream::get you can specify the delimiter you want.

myReadFile.get(a, sizeof(a), '\t');

This will read a maximum of sizeof(a) characters from the stream until it finds a '\t'.

Upvotes: 1

Related Questions