473,406 Members | 2,352 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,406 software developers and data experts.

Opening a File for writing and reading

I'm dealing with a log management program: I must save some records to a
file... and occasionally read previously written records.
because it's a log file, all the writes will be at the end of the file.

for studying this problem I made the following example program.

It works in a very strange way:
every time it open the file it copies the last char. i.e. if the first time
the file contains "home" second time I execute the prog it will contain
"homee".
then I have problems in seeking inside the file.

could anyone help me solve this problem??

thanx alot
Giulio

----------------------------
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
fstream File("test.txt",ios_base::out|ios_base::in );
if (File.fail() ) { cout << "errore in apertura"; system ("pause");
exit( 1);}
// File << "a \n asd";
File.seekg(ios_base::beg);
do{
string str2;
File >> str2;
cout << "'" << str2 << "'" << endl;
}while(!File.eof());
cout << "lettura 1"<< endl;
cout << "tellp : "<<File.tellp()<< endl;
cout << "tellg : "<<File.tellg()<< endl;

File.seekp(ios_base::end);
cout << "scrittura 1"<< endl;
cout << "tellp : "<<File.tellp()<< endl;
cout << "tellg : "<<File.tellg()<< endl;
File << "ultima" << endl;

cout << "-------------------------------";
cout << "lettura "<< endl;
cout << "tellp : "<<File.tellp()<< endl;
cout << "tellg : "<<File.tellg()<< endl;
File.seekg(ios_base::beg);
do{
string str2;
File >> str2;
cout << "'" << str2 << "'" << endl;
}while(!File.eof());
cout << "tellp : "<<File.tellp()<< endl;
cout << "tellg : "<<File.tellg()<< endl;

File.close();
system ("pause");
return 0;
}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.514 / Virus Database: 312 - Release Date: 28/08/2003
Jul 19 '05 #1
0 1993

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

Similar topics

14
by: Mike - EMAIL IGNORED | last post by:
Using RH9, from this fragment: fstream theStream_; string fPath = "myFile"; int mode = O_EXCL | O_CREAT; theStream_.open(fPath.c_str(),mode); I get the failure shown below. ...
3
by: Alex Gerdemann | last post by:
Hello, I'm writing a program using the g++ compiler that runs under Cygwin in Windows, and have written the following: std::ifstream file; char buffer; std::string myString;...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
7
by: emanshu | last post by:
HI all, I an designing an application in C++. i want to open file requested by end user but i want to reflect an error to user if file is already opened by some other application.. will anybody...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
2
abdoelmasry
by: abdoelmasry | last post by:
hi any one can help me plz ?? my code is about opening file as binary then get data from file to edit and rewrite data to file this is the code: #include <cstdlib> #include <iostream>...
1
by: raghu | last post by:
i want to know the difference between 'r' mode and 'r+' mode 1.i = open('c:\python25\integer.txt','w')-------->for writiing i.write('hai')--------->written some content in text file i =...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
14
by: W Marsh | last post by:
Hi. In my application I do something very simple - I open a file, lock it exclusively, write some data to it and close it. If I re-open it in the same process (I mean before the script has...
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: 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: 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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.