| | |
Text file Mainipulation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
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
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.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
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
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.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
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].
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*
>>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
When the above finished the tokens vector will contain all the individual words in the string.
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
C++ Syntax (Toggle Plain Text)
#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);
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.
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
hi guys
wow loads of help and advise here its great for a n00b like me and much appreciated
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)
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...
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)
![]() |
Similar Threads
- File Mainipulation (C++)
Other Threads in the C++ Forum
- Previous Thread: How to run c++ program through command prompt?
- Next Thread: direct access to hdd
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







