#### Archived

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

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

This topic is 5451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631710
• Total Posts
3001846
×