Text file Mainipulation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 7
Reputation: maori is an unknown quantity at this point 
Solved Threads: 0
maori maori is offline Offline
Newbie Poster

Text file Mainipulation

 
0
  #1
Dec 16th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Text file Mainipulation

 
0
  #2
Dec 16th, 2007
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.
Last edited by Jishnu; Dec 16th, 2007 at 9:40 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,577
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Text file Mainipulation

 
0
  #3
Dec 16th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: maori is an unknown quantity at this point 
Solved Threads: 0
maori maori is offline Offline
Newbie Poster

Re: Text file Mainipulation

 
0
  #4
Dec 16th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: maori is an unknown quantity at this point 
Solved Threads: 0
maori maori is offline Offline
Newbie Poster

Re: Text file Mainipulation

 
0
  #5
Dec 28th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Text file Mainipulation

 
0
  #6
Dec 29th, 2007
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.
Last edited by Jishnu; Dec 29th, 2007 at 5:23 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Text file Mainipulation

 
0
  #7
Dec 29th, 2007
> 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Text file Mainipulation

 
0
  #8
Dec 29th, 2007
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].
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,577
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Text file Mainipulation

 
0
  #9
Dec 29th, 2007
>>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
  1. #include <sstream>
  2. ...
  3. std::string line = "Name0 1 2 3456 7 8 9 10 \"discription\"";
  4. std::stringstream stream(line);
  5. std::vector<std::string> tokens;
  6. std::string tkn;
  7. while( stream >> tkn)
  8. tokens.push_back(tkn);
When the above finished the tokens vector will contain all the individual words in the string.
Last edited by Ancient Dragon; Dec 29th, 2007 at 7:14 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: maori is an unknown quantity at this point 
Solved Threads: 0
maori maori is offline Offline
Newbie Poster

Re: Text file Mainipulation

 
0
  #10
Dec 29th, 2007
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)
Attached Thumbnails
pic1.gif  
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC