DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Easy help: Start and stop reading in the middle of a file (http://www.daniweb.com/forums/thread149455.html)

ApMignonne Oct 6th, 2008 6:35 am
Easy help: Start and stop reading in the middle of a file
 
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:
        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?

ApMignonne Oct 6th, 2008 7:11 am
Re: Easy help: Start and stop reading in the middle of a file
 
I just saw that I messed up a bit. The code was supposed to be like this:
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 ";"

Sky Diploma Oct 6th, 2008 7:21 am
Re: Easy help: Start and stop reading in the middle of a file
 
And Secondly. Why dont you just search for the word Matrix in Your File?

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

ApMignonne Oct 6th, 2008 1:25 pm
Re: Easy help: Start and stop reading in the middle of a file
 
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!


All times are GMT -4. The time now is 9:02 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC