Solved

C++ program - how to row back 1 line

Posted on 2004-10-07
7
340 Views
Last Modified: 2006-11-17
I have a short routine:

     ifstream ifs1("in.txt", ios::ate);
     while(getline(ifs1, line111))
        {
          hr=line111.substr(8,2);
          min=line111.substr(11,2);
          sec=line111.substr(14,2);
         }
     ifs1.close();        

I think the line the routine checks is at EOF.

How to row 1 line back so that the routine can check for hr, min and sec?

Thanks.



0
Comment
Question by:ccjong
7 Comments
 
LVL 1

Expert Comment

by:Feldspar
ID: 12255413
I can't really tell what you are asking.  are you looking to go back a line using ifs1.seek()? please explain with more examples
0
 

Author Comment

by:ccjong
ID: 12255444
When open the file with ios::ate, I think the routine is going to the end of the file.

I need to find out the hour, minute and second of the last line of the file in.txt.
Hour (hr) is at position 8 and with 2 characters, similar for minute and second.

I don't know how to get to the last line of the file (before EOF)

Will ifs1.seek() do it? What is the format?

Thanks.

0
 
LVL 22

Assisted Solution

by:cookre
cookre earned 50 total points
ID: 12255808
A simple way is to open it normally and read each line.  When you hit the end the most recently read line is the last in the file.

Unless it's a huge file, this cost is negligible.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Assisted Solution

by:Feldspar
Feldspar earned 200 total points
ID: 12256115
ok, there are 2 ways of doing this.  using cookre's method of reading each line till the end, just get rid of the ios::ate.

 ifstream ifs1("in.txt"); // <---------- no ios::ate
     while(getline(ifs1, line111))
        {
          hr=line111.substr(8,2);
          min=line111.substr(11,2);
          sec=line111.substr(14,2);
         }
     ifs1.close();        

and that should work, as long as the hour, minute, and second are the last line of the file.  if there is another blank line after that it wont work.

if you need it to be faster, you can use seekg() - but you will have to know how long the last line is, im guessing this is 17 bytes but if it isn't then change it.  the code is:

 ifstream ifs1("in.txt"); // <---- ios::ate is not needed, it does not matter
 ifs1.seekg(-17, ios_base::end) // <---- seek to 17 bytes before the end of the file.
 getline(ifs1, line111)
 hr=line111.substr(8,2);
 min=line111.substr(11,2);
 sec=line111.substr(14,2);
 ifs1.close();        

0
 
LVL 1

Assisted Solution

by:Feldspar
Feldspar earned 200 total points
ID: 12256131
sorry, i noticed there is another error in the first example I gave, it should be:

 ifstream ifs1("in.txt");
     while( ! ifs1.eof() ); // <--- continue until no more lines
        {
          getline(ifs1, line111); // <--- get line
          hr=line111.substr(8,2);
          min=line111.substr(11,2);
          sec=line111.substr(14,2);
         }
     ifs1.close()
0
 
LVL 1

Accepted Solution

by:
GFinseth earned 250 total points
ID: 12256738
Try going to the end of the file and then moving backwards until you find a colon.  That's pretty fast and easy.

Here's the code...

long filesize;          // total size of file
long position;         // current file position
char          ch;       // single char read from file
char line111[200]; // buffer for line containing time substring

...

ifstream ifs1("in.txt", ios::ate);  // ios::ate makes starting position EOF
filesize = ifs1.tellg()-1;             // determine size of file

// read file backwards until colon is found (assumes HH:MM:SS format for time)
ch = 0;
for (position=filesize; position >= 0 && ch != ':'; position--)
{
   ifs1.seekg(position);
   ifs1.get(ch);
}
   
      
position -= 12;          // move back 12 more characters
ifs1.seekg(position);    // seek to the calculated position
ifs1.getline(line111,sizeof line111-1);

I hope this helps.
0
 

Author Comment

by:ccjong
ID: 12257811
Thanks for the help. Really appreciate the efforts. I really prefer the ios::ate method.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now