Reading Lines From a File

This is a discussion on Reading Lines From a File within the C++ Programming forums, part of the General Programming Boards category; Hi all, Here is the prob, i have a .txt file containing the following lines there are currently 5 lines ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Reading Lines From a File

    Hi all,

    Here is the prob, i have a .txt file containing the following lines there are currently 5 lines but it can keep growing and growing in size.

    I want to be able to read the last 5 lines from the file and display them to the screen. I also want to be able to display any one given line from the file and display it to the screen. For example the 3rd line if requested would display + 123 1579 Tue Feb 22 05:24:47 2005.

    New lines are appended to the end of the file and the cursor is then placed on a new line awaiting further input.

    + 1000 1000 Tue Feb 22 04:33:57 2005
    + 456 1456 Tue Feb 22 05:24:44 2005
    + 123 1579 Tue Feb 22 05:24:47 2005
    + 1000 2579 Tue Feb 22 05:24:50 2005
    - 123 2456 Tue Feb 22 05:24:53 2005

    I can get the first line to display using:

    Code:
    string line;
    ifstream in("trans.txt");
    getline(in, line);
    cout << line;
    And i can get it yo output the entire file using:

    Code:
    string line;
            
        ifstream in("trans.txt");
        while (getline(in, line) )
        {
            cout  << line << endl;
        }
    Its just the getline function thats confusing me really i think, a few tips on using it would be very helpfull.

    Thanks,
    Dangerous Dave

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Its just the getline function thats confusing me really
    It keeps reading data in your file until it encounters an invisible '\n' character. An invisible '\n' character is entered at the end of every line when you hit Return. getline() reads up to and including the '\n' and then discards the terminating '\n' from the input.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    So have you any ideas on how to solve the problem as defined above?
    Dangerous Dave

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    One way would be to create an array of strings. The size of the array would have to be larger than the maximum possible number of lines in your file. Then read lines of data from your file into the array. To work out the index value of the last line of data, you could initialize your array with blanks:
    Code:
    string data[100]  = {""};
    and then use a while loop plus a counter to step through the array until you find a blank. When the while loop terminates, the counter will be one position past the end of the data.
    Last edited by 7stud; 02-22-2005 at 12:33 AM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If each line is exactly the same length, then you can determine the end of the file, the length of a line, and go back five lines from the end. This can be done readily if the line is a single object using the sizeof() operator and tellg(), seekg() methods of ifstream/fstreams. I suppose you could also read each char from the back one at a time keeping track of the number of newline char you have come across using tellg() and seekg() again and maybe peek(), though I've never actually done this. This approach seems cumbersome, but I suppose it could be used if the lines aren't of the same length and you don't want to/can't use the technique already described in the other post.
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by elad
    If each line is exactly the same length...
    + 1000 1000 Tue Feb 22 04:33:57 2005
    + 456 1456 Tue Feb 22 05:24:44 2005

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    To display any given line, walk through a loop that increments a LineNumber count by one every time it hits '\n'. Then when you are at N-1 lines, use getline() to read in the line requested and display it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 01:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21