Advertisement

hello, some help with C++.

Started by
4 comments, last by jflanglois 18 years, 11 months ago
hi all. i am making a program that reads from a .txt file but i got 1 problom the file is 20 lines and i want to read only the 5th line can anyone give me a code how to do it right? thanks in advance
There aren't really any efficient or clever ways of doing this (unless you use lines of a fixed length1).
Just begin by reading and discarding the first four lines.
Advertisement
i aint so good at this at all :can you please post a full code how do i do that?

and dont i need to throw all lines after 5 too?



thanks in advance
No you just stop reading after you reach the 5th line
However (if you are using windows)

you could arrange your file in such a manner to resemble an INI file. Do a search on your PC and you will find a few. Open one to see what it looks like.

Then you could use the Win32 API functions

GetPrivateProfileString to read string or GetPrivateProfileInt to read integers

I don't have time to prepare an example right now but you can do a search for these functions and it can come up with some examples.

Hope that helps
This should give you an idea (it lacks error-checking, though, so you will have to do that yourself):

#include <iostream>#include <fstream>#include <string>using namespace std;int main() {  string input;  ifstream fin( "in.txt" );  for( unsigned i = 0; i < 5; ++i )    getline( fin, input );  cout << input << endl;  return 0;}

[edit] This should be moved to For Beginners.


jfl.

This topic is closed to new replies.

Advertisement