0

hello everyone!
i have a question about reading from a binary file.what if i want to start reading a file from a certain line of the file and end the reading to a certain line too?
is there a way to do this using an ifstream?

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by WaltP
0

Sure.
Read the file until you get to the 'certain start line'.
Then continue reading and doing what you need to do with each line.
When you reach the 'certain end line', stop reading.

Edited by WaltP: n/a

0

thanks for your help
so i tried something like this
but i'm still not sure i got the whole idea:

long from = atoi(argv[2]); //argv[2] is the line number i want to begin //reading the file
	long to = atoi(argv[3]); //argv[3] is the line number i want to stop
	ifstream fp;
	streampos pos,finalpos;
	char line[LINESIZE];
	fp.open (argv[1]); //argv[1] is the name of the file
	if (!fp.is_open())
		throw "File not found ";
	pos = from;
	finalpos = to;
	fp.seekg(pos); 
	while(fp.tellg()!=finalpos){
		try{
			fp.getline(line,LINESIZE);		
			
			}
		catch(char d[LINESIZE]){
			cout<<d<<endl;
		}

does this seem ok?i'm a bit confused..

0

does this seem ok?i'm a bit confused..

Not at all. Use my technique. It's simple. tkud's solution needs a lot more understanding of files than you have at this point in your programming.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.