Hi Guys

i'am trying to learn text file manipulation and i've got a problem with one particuler subject and unsure of how to acchive what i need the text file contains lines in this format

Name0 1 2 3456 7 8 9 10 "discription"
Name1 1 2 3458 7 8 9 10 "discription"
etc
etc

what the problem is i cant understand on how to only grap parts of the line
ie in a textbox i would like it to read

discription 3456
then next line down
discription 3458
and so on till end of the text file

could someone point me in the right direction here please

TIA
Maori

It doesn't appear as if you have tried to develop an algorithm of your own. I can't write the entire code for you but I can tell you this much. Use an ifstream object to take values from the file repeatedly. Then for moving to a specified location in the file you can use either the file-handling functions or their equivalent manipulators. Alternatively, use the read() function for reading data.

read the line into a std::string object then you can use its find and substr methods to chop off the part of the string you don't want.

Thanks Guys

at least i know where to begin now will try and work this out and hopefully get it going :)
as a complete C++ newbie

thanks for the quick replies


/r
Maori

hiya guys

hope you all had a great xmas and happy new yr to all

I think i'am getter closer to what i need now after a few hair pulls

i'am using sscanf and its working ok i think but how can i get it to ignore the " and spaces parts

in the string

Name1 1 2 3458 7 8 9 10 "discription 1 is here"

at the moment it just prints out "discription

TIA

Try using fgets() instead. I found out something useful regarding I/O in the 'Read me' threads in the C or C++ forum. Unfortunately, I don't remember it too well.

> i'am using sscanf and its working ok i think but how can i get it to ignore the " and spaces parts
But you started off using C++ ?
So why aren't you still using C++ APIs, like AD mentioned in post #3

I'm not sure how reliable Ancient Dragon's idea would be, as the text (description) is going to change at every line so using find seems non-intuitive here. But he hasn't explained exactly what he intends to do here...

What I would do is to split the line using the space as a delimiter.

Name0 1 2 3456 7 8 9 10 "discription"

So you would have:-

[token1] = Name0
[token2] = 1
[token3] = 2
[token4] = 3456


Then all you would need to do is spit out [token4] and [token9].

>>I'm not sure how reliable Ancient Dragon's idea would be, as the text (description) is going to change at every line so using find seems non-intuitive here.
I was expecting the OP to actually read his textbook about the find method to see how it works, but maybe I was expecting too much (hint: use find to locate the spaces). However, after some more thought the find method would not have been the best choice anyway.

its easy using stringstream class because it works on in-memory strings like fstream does on file streams so you can use the >> operator to extract each of the individual words

#include <sstream>
...
std::string line = "Name0 1 2 3456 7 8 9 10 \"discription\"";
std::stringstream stream(line);
std::vector<std::string> tokens;
std::string tkn;
while( stream >> tkn)
   tokens.push_back(tkn);

When the above finished the tokens vector will contain all the individual words in the string.

Comments
Good example

hi guys

wow loads of help and advise here its great for a n00b like me and much appreciated

But he hasn't explained exactly what he intends to do here...

oops sorry about that what i want to do is

1, read a text file (done)
2, put it in a string (done)
3, get the info out of each line and put it into a grid (sort of thing see pic) so the tokens (i think) are sepearated (this one is where i'am getting confussed)
4, after changing one of the tokens save the file (not looked at this part yet)

Attachments pic1.gif 1.32 KB

I already showed you how to do #3. After do that for one line the vector will be an array of 10 strings. If you want to change the 8th element then access it just like any other array tokens[8] = "some other string" >>after changing one of the tokens save the file (not looked at this part yet)
simply rewrite the tokens vector

for(int i = 0; i < tokens.size(); i++)
{
   outfile << tokens[i] << " ";
}
outfile << "\n";
This article has been dead for over six months. Start a new discussion instead.