Solved

C++ program - how to row back 1 line

Posted on 2004-10-07
7
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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…
Six Sigma Control Plans

632 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