Hi,

I have this txt file containing data in this format:

John HH HHH GGGGKK

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?

Recommended Answers

All 5 Replies

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

getline will get this result?
HH
HHH
GGGGKK

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

HH HHH GGGGKK

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.