Solved

C++ program - how to row back 1 line

Posted on 2004-10-07
7
367 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …

710 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