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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from .txt file

Status
Not open for further replies.

TheCrappyProgrammer

Programmer
Sep 17, 2001
41
US
Hi there,

How do I read specific lines from a text file using the CFile class?

Suppose I have a file called "names.txt" that contains the following lines:

Bobby
Tommy
Robert

How would I read the second line of the text file using CFile class in MFC?
 
If the name fields are fixed length (contain the same number of characters, i.e. padded with spaces), you can use the CFile::Seek() function to "jump" a specific number of positions through the file. For example, if each name is 25 characters, then use Seek() to jump to the 26th position and read the next 25 characters.

If the fields are variable length, you have no way of knowing how many positions to "jump" in order to find the beginning of the second line. Instead, you'll have to read each line sequentially until you get to the desired line index.

Hope this helps. :cool:
 
I had this problem a while back. Though my quick post did not give ALL the details it should be enough to simulate getline with CFile

Matt

thread116-84917
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top