Easy help: Start and stop reading in the middle of a file

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

Join Date: Oct 2008
Posts: 3
Reputation: ApMignonne is an unknown quantity at this point 
Solved Threads: 0
ApMignonne ApMignonne is offline Offline
Newbie Poster

Easy help: Start and stop reading in the middle of a file

 
0
  #1
Oct 6th, 2008
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:
  1. ifstream in("test.txt");
  2. string inName[3], inChar[3];
  3. for (int i= 0; i<3; i++)
  4. 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3
Reputation: ApMignonne is an unknown quantity at this point 
Solved Threads: 0
ApMignonne ApMignonne is offline Offline
Newbie Poster

Re: Easy help: Start and stop reading in the middle of a file

 
0
  #2
Oct 6th, 2008
I just saw that I messed up a bit. The code was supposed to be like this:
  1. ifstream in("test.txt");
  2. string inName[3], inChar[3];
  3. for (int i= 0; i<3; i++)
  4. in >> inName[i] >> inChar[i]

and what I want to do is to start reading after the word "MATRIX" and stop before the character ";"
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Easy help: Start and stop reading in the middle of a file

 
0
  #3
Oct 6th, 2008
And Secondly. Why dont you just search for the word Matrix in Your File?

  1. string inName[3],inChar[3];
  2. while(in.eof())
  3. string s;
  4. in>>s;
  5. if(s=="matrix")
  6. {
  7. for (int i= 0; i<3; i++)
  8. {
  9. in >> inName[i] >> inChar[i];
  10. }
  11. break;
  12. }
  13. else
  14. //Do nothing
  15. }

That way You will read the next words into strings
Last edited by Sky Diploma; Oct 6th, 2008 at 7:22 am.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3
Reputation: ApMignonne is an unknown quantity at this point 
Solved Threads: 0
ApMignonne ApMignonne is offline Offline
Newbie Poster

Re: Easy help: Start and stop reading in the middle of a file

 
0
  #4
Oct 6th, 2008
Well, that's just about the help I wanted

As I said, my programming skills are not too great. After a while, I figured out that you must have meant "while(!in.eof())", and now my program works perfectly fine.

Thanks!
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC