| | |
Easy help: Start and stop reading in the middle of a file
![]() |
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
I'm trying to make a program that will read names and numbers from a file, but I'm quite inexperienced in C++ in general, and most tutorials and help threads I read tend to make me confused.
So far, I have used a test file with a text like "NameA 01234 NameB 11111 NameC 55555". I want the names in one string and the numbers in another one, which I have managed to do with the code:
My problem now is that the file I actually want to read from is more complicated, and looks more like:
"LotsOfTextThat;IAmNotInterestedIn; MATRIXNameA 01234 NameB 11111 NameC 55555; MoreTextThatIsOfNoUse".
In other words, I want to start reading after the word "MATRIX" and stop at the character ";". How do I edit my code to make it do that?
So far, I have used a test file with a text like "NameA 01234 NameB 11111 NameC 55555". I want the names in one string and the numbers in another one, which I have managed to do with the code:
C++ Syntax (Toggle Plain Text)
ifstream in("test.txt"); string inName[3], inChar[3]; for (int i= 0; i<3; i++) in >> plant[i].name >> plant[i].charString
My problem now is that the file I actually want to read from is more complicated, and looks more like:
"LotsOfTextThat;IAmNotInterestedIn; MATRIXNameA 01234 NameB 11111 NameC 55555; MoreTextThatIsOfNoUse".
In other words, I want to start reading after the word "MATRIX" and stop at the character ";". How do I edit my code to make it do that?
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
I just saw that I messed up a bit. The code was supposed to be like this:
and what I want to do is to start reading after the word "MATRIX" and stop before the character ";"
C++ Syntax (Toggle Plain Text)
ifstream in("test.txt"); string inName[3], inChar[3]; for (int i= 0; i<3; i++) in >> inName[i] >> inChar[i]
and what I want to do is to start reading after the word "MATRIX" and stop before the character ";"
And Secondly. Why dont you just search for the word Matrix in Your File?
That way You will read the next words into strings
C++ Syntax (Toggle Plain Text)
string inName[3],inChar[3]; while(in.eof()) string s; in>>s; if(s=="matrix") { for (int i= 0; i<3; i++) { in >> inName[i] >> inChar[i]; } break; } else //Do nothing }
That way You will read the next words into strings
Last edited by Sky Diploma; Oct 6th, 2008 at 7:22 am.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: socket programming
- Next Thread: Game evaluation - Mastermind
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






