473,387 Members | 1,942 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,387 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 3792
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
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.