Solved

C++ program - how to row back 1 line

Posted on 2004-10-07
7
331 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dividesSelf challange 15 82
noX challenge 17 77
wordappend challenge 8 88
silent install of security banner via msiexec command 4 52
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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

743 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

10 Experts available now in Live!

Get 1:1 Help Now