Is there a way to chose where a program outputs text to a file ?(or read text from a file). As in selecting which line to start from.? Any help is welcome. :)

Recommended Answers

All 6 Replies

Only if the file is highly structured. seekg and seekp will position the file pointer at a specified number of bytes from the beginning of the file, but unless each line has exactly the same number of bytes, trying to place the pointer at the start of the desired line is almost impossible. A common alternative when files aren't perfectly predictable is to read the lines into a container which then makes it easier to find the desired line.

>As in selecting which line to start from.?
Sure, but unless the file is structured enough to seek, you'll have to actually read your way to the correct line. Writing is harder if you mean to insert rather than overwrite. In that case you need to save the subsequent lines and shift them over like you would insert into an array.

commented: somewhat helpful +1

I mean to overwrite, but how does seekg and seekp work?

What if I want to read:

a character.
a string.

In that order.

Does the infile consist of strings and characters jumbled?

commented: cool +1

>As in selecting which line to start from.?
Sure, but unless the file is structured enough to seek, you'll have to actually read your way to the correct line. Writing is harder if you mean to insert rather than overwrite. In that case you need to save the subsequent lines and shift them over like you would insert into an array.

Thanks for your asuggestion I investigated the seekg and seekp functions and found them to be very useful, b ut it turns out that I didn't need them after all. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.