Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

How can you read a specific line from a file (C++)

Recommended Posts

Ekim_Gram    418
Is there a way of using I/O streams (fstream, ostream, ect.) to read a certain line from a text file? Like, say I use the following random number generating code:
int x;
srand(GetTickCount());
x = rand()%10;
If x = 9 for instance, since the random numbers start at 0 and I''m guessing the lines in a .txt file would start at 0 as well, can I read line 10 and print it to the screen or save it to an array?
There''s no town drunk here, we all take turns. Velocity Gaming Force

Share this post


Link to post
Share on other sites
Nervo    344
d00D j00 C0UlD L1Ke 4DD 5pEci4l CH4r4C+ER$ +O 3@CH liNE In TeH tEX+ fiL3 L00P THR0u9h PHoR e@cH oNe @ND d0 that m@tH th4+ W@Y. eV3n 83+t3R Why n0+ nUM83R EAcH LINE +0 wHeN j00 rEcE1V3 +he RaNdOm NUMb3r J00 L00p +HROu9H +0 F1nD +H4T 4c+U4L nuM8ER.

Share this post


Link to post
Share on other sites
n0ob    176
Yah.. I would loop through using getline, letting it terminate at a \n (and supposing you know the layout of the file so that a line won''t go over the buffer size), you could loop through until you get to that line. That''s the way our 3 man team had to do it for a school project, and the teacher didn''t say anything about it (or probably didn''t see that section). He mentioned something about using windows INI system.. but that didn''t go anywhere. As long as it''s a fairly small amount of lines you''re reading, and it''s not too speed dependant, the looping would be fine.

Kings of Chaos

Share this post


Link to post
Share on other sites