Seek to the last line in a text file

E

Eric Lilja

Hello, I'm writing a simple program that upon start-up needs to open a
text file and read a value on the last line. Then all other accesses to
the file will be writes (at the end of it). I'm having two problems
with this...I tried opening the file for both writing and reading with:
std::fstream file("budget.txt", std::ios_base::in | std::ios_base::eek:ut
| std::ios_base::app);

if(!file)
std::cerr << "Error opening file!" << std::endl;

This compiles but it fails to open the file. So I removed the
std::ios_base::in flag and now the file is opened correctly (and its
previous content left intact), but my code for reading doesn't seem to
work.

static double
obtain_balance(std::fstream& file)
{
std::string line;

while(std::getline(file, line))
std::cout << line << std::endl;

/* The last line should always contain the balance,
in the following form:
balance yyyy-mm-dd xxxxxxxx */
assert(line.substr(0, 7) == "balance");

std::string s = "balance yyyy-mm-dd ";
return convert<double, std::string>(line.substr(0, s.length()));
}

I need to read a value on the last line and the only way I could think
to get to the last line is to read the file line by line. But it
doesn't work because nothing is outputted so the assertion fails
because the line variable is empty. I guess this is because I didn't
specify the std::ios_base::in-flag, but when I did the file wouldn't
open as I explained above..

So do I need to first open the file for reading, obtain the balance,
then close it and open it for writing (with app) and what is the best
way to seek to the beginning of the last line when writing?

/ E
 
M

mlimber

Eric said:
Hello, I'm writing a simple program that upon start-up needs to open a
text file and read a value on the last line. Then all other accesses to
the file will be writes (at the end of it). I'm having two problems
with this...I tried opening the file for both writing and reading with:
std::fstream file("budget.txt", std::ios_base::in | std::ios_base::eek:ut
| std::ios_base::app);

You should use std::ios_base::ate instead of std::ios_base::app.
if(!file)
std::cerr << "Error opening file!" << std::endl;

This compiles but it fails to open the file. So I removed the
std::ios_base::in flag and now the file is opened correctly (and its
previous content left intact), but my code for reading doesn't seem to
work.

static double
obtain_balance(std::fstream& file)
{
std::string line;

while(std::getline(file, line))
std::cout << line << std::endl;

/* The last line should always contain the balance,
in the following form:
balance yyyy-mm-dd xxxxxxxx */
assert(line.substr(0, 7) == "balance");

std::string s = "balance yyyy-mm-dd ";
return convert<double, std::string>(line.substr(0, s.length()));
}

I need to read a value on the last line and the only way I could think
to get to the last line is to read the file line by line. But it
doesn't work because nothing is outputted so the assertion fails
because the line variable is empty. I guess this is because I didn't
specify the std::ios_base::in-flag, but when I did the file wouldn't
open as I explained above..

So do I need to first open the file for reading, obtain the balance,
then close it and open it for writing (with app) and what is the best
way to seek to the beginning of the last line when writing?

Since the last line has a known format and length, just use
istream::seekg() to move the get pointer back to the proper location
from the end. (Note: the put pointer is manipulated independently from
the get pointer.) For an example, see:

http://www.cplusplus.com/ref/iostream/istream/seekg.html

Cheers! --M
 
K

Karl Heinz Buchegger

Eric said:
Hello, I'm writing a simple program that upon start-up needs to open a
text file and read a value on the last line. Then all other accesses to
the file will be writes (at the end of it). I'm having two problems
with this...I tried opening the file for both writing and reading with:
std::fstream file("budget.txt", std::ios_base::in | std::ios_base::eek:ut
| std::ios_base::app);

Have you read the documentation what std::ios_base::app does?

http://www.roguewave.com/support/docs/sourcepro/html/stdlibug/30-3.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top