I have this txt file containing data in this format:


I want to extract John as 1 field and HH HHH GGGGKK as another field.

How do I ignore the whitespace and read it as a single field?

getline will get this result?

what I want is to get the whole data ignoring the whitespace and assign to a string or array


getline() would be the function for you.
Just use a space ' ' as a delimiter.

Edited 5 Years Ago by Nick Evan: n/a

As Niek stated u use function getline() to ignore the spaces and print the whole string of characters... now in your case you can also use substring.. eg cout << sentence << substr(1, 45); depend on how u want to output ur string character... to make things less complicated you can use... I dont know coz I have no idea how you declared your strings or whatever

Just to explain how substring works... if you want to take John out of your sentence and print everything else assuming that u have declared the string or char as, lets say, sentence or whatever you can use this emas... cout << sentence << substr(5, 10) that means you are going to print from position 5 and have 10 characters

Hope it will help

Alright, thanks lot :)

yeah, so use it once with'a space as a delimiter and then with '\n'
This will result in two strings

Edited 5 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.