Solved

C++ program - how to row back 1 line

Posted on 2004-10-07
7
351 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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