0

Hi, i was wondering what would be the best way of reading a single line from a text file?
For example, i have a file that contains the following data:
dog
cat
bird
deer

How can i read the 3rd line of the file? I was thinking that i would have to store each line in a array and just calling it by the number in that array.

The other question I had was, how can i print to the end of a text file? without disturbing the text data that is already there?

Thanks in advance, Guy Od

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by ~s.o.s~
0

>I was thinking that i would have to store each line in a array and
>just calling it by the number in that array.

Storing the data/lines which won't be used has little meaning; you can just read lines and keep skipping them until you read the line you require [i.e. to get the 3rd line, read and skip the first two]. If the file has a fixed format, e.g. only 20 characters per line and '\n' used as the line separator, you can use the seek method to skip the calculated number of bytes and directly move to the data in consideration.

>how can i print to the end of a text file?

If by `print' you meant `write', you can open the file in append mode. If by `print' you meant display the data, then it shouldn't be a problem considering that you are using a Reader to read your file data.

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.