473,381 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

Seek to the last line in a text file

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::out
| 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

Nov 30 '05 #1
2 3790
Eric Lilja wrote:
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::out
| 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/iostrea...eam/seekg.html

Cheers! --M

Nov 30 '05 #2
Eric Lilja wrote:

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::out
| std::ios_base::app);


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

http://www.roguewave.com/support/doc...ibug/30-3.html

--
Karl Heinz Buchegger
kb******@gascad.at
Dec 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Shu-Hsien Sheu | last post by:
Hi, Does the seek method would close the file object after using a for loop? My program looks like this: f = open('somefile', 'r') for lines in f: some operations f.seek(0)
3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
1
by: Waitman Gobble | last post by:
Hello, I am new to Python. I am having trouble with zipfile.py. On a Linux machine with python 2.4.2 I have trouble opening a zipfile. Python is complaining about the bit where it does a...
2
by: Harry J. Smith | last post by:
In the code below the two lines marked with ????????????????? do not work properly. The line: inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to beginning of the file...
4
by: Felix Finch | last post by:
I have a perl test program which has about 80 test cases, each of which creates its own schema so I can remove them with DROP SCHEMA xxx CASCADE. Normally each test case creates and drops the same...
0
by: Art Chadbourne | last post by:
Here is the scenario. Access XP using VBA. I have a text field. The text that I have in the field is m-1234. I am running a >= seek. My first seek is m and it returns m-1234. My second seek...
1
by: Art Chadbourne | last post by:
Here is the scenario. Access XP using VBA. I have a text field. The text that I have in the field is m-1234. I am running a >= seek. My first seek is m and it returns m-1234. My second seek...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
6
by: DataSmash | last post by:
Hi group, I have a text file that contains thousands of lines and each line is 256 characters long. This is my task: For each line in the file, move to the 25th character, if the character is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.