Thread: Please Some Advice?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Question Please Some Advice?

    Hey guys If I want to just read the last line of a txt file and display it how could I do that?

    for example if I want to read the whole file with this info but I need to cout just the last line ?

    "N2014776582","M0210400060667804600_3","https://extpws09.chubb.com/eLossDirect/eLossFaxEDI.asp?route=east","0","03/07/06 14:41:51","03/07/06 14:51:46","M0210400060667804600","00001","CS-FNOL","LIAB","00000","005","00742","S","SUCCESS"
    "N2014776582","M0210401060607750200","FNOL.ATTACH. [email protected]","0","03/01/06 14:32:29","03/01/06 14:36:59","M0210401060607750200","00001","CS-FNOL","NONFORM","00000","000","00002","1001","Non-Recognizable Form"

    I would appreciate some suggestions thank you

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    by the way the file is going to change once in a while so how could I create a loop that will run until the last line and display it ?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something along this line.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
     
    int main()
    {
       std::ifstream file("file.txt");
       std::string line, last;
       while ( getline(file, line) )
       {
          if ( line != "" )
          {
             last = line;
          }
       }
       std::cout << "This is the last line of the file:\n"
                 << last << '\n';
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  3. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 01:06 PM
  4. need hardware advice....
    By forgottenPWord in forum Tech Board
    Replies: 5
    Last Post: 03-23-2003, 09:12 AM
  5. Newbie: Seek advice
    By gogo in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2001, 10:04 AM