Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file input

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
how do i skip to a certain line in a file, and then read that line??
 
Hi,
U can use seekg()or seekp()to place file pointer to required postion and then read the file from that position.
Hope this helps ur case. If not please specify the problem in detail.
Regards,
Hemant Kapadia.
 
hi!

as it appears u are going to need this line later in the programme for some processing [whatever] you can just read the line in a separate buffer - some string [char array] like:

Code:
char buffer[129];
fin.getline(buffer, 129, '/n'); /* assumes fin is an ifstream object already connected to a file */

generally speaking, the prototype of getline is:

istream& getline (char *s, streamsize n, char delim );

getline extracts characters from the input stream storing them into succesive locations in the buffer pointed to by char *s - a char array for instance. it will read n-1 characters (in the above example 128 characters - the maximum number of characters per line as far as i know - very unlikely to be that much anyway). the char delim is the character up to which getline is going to read [that is if it does not read n-1 characters first]. in other words, if the delimiter character is encountered before n-1 chars have been read the reading is stopped and the delimiter is extracted from the stream, but not stored.
specifying the delimiter is optional. if none is specified the '/n' char is assumed, eg the end of line.
reading is also stopped of the eof (end-of-file) character is found.

so you can just write:

Code:
fin.getline(buffer, 129);

ah, also, getline autmatically appends the null char '/0' to the buffer.

ok. hope that helps, and doesnt just confuse. i dont think im very good at explaining hehe. too many words :)

if this doesnt work, elaborate on the problem, just as Kapadia says. i think her method might be much better, but it all depends on the specs of ur case.
Avendeval Sedai
[sub]yavoor@yahoo.com[/sub]

 
Hi,
I agree that getline() could be used, but in specified case question is <b>skiping certain lines and then read from a certain point</b>. Also if fin.getline(buffer, 129) is called then the default delimeter is '\n'. And certainly that won't help to skip lines and read from that point onwards. Yes it will surely help in case of reading from a file till a '\n'. So I think some more specification of what exactly needs to be done is required in this case.
And Sedai, I think u r pretty good at explaining thingz:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top