473,587 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.eo f());
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.eo f());
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 2003

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

Similar topics

14
3729
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. According to the man pages and the standard, the second argument should be an int. Thanks in advance for your help.
3
1982
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; file.open(filename); file.getline(buffer,128);
4
9817
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 = tmpfile(); /* write into tmpFile */ ...
7
13959
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 tell me that how to know that the file is already open or closed.. any help is highly appreciated...
11
3584
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 appreciated. Enter an interesting string. Too many cooks spoil the broth Error opening C:\myfile.txt for writing. Program termnated. This...
2
4400
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 that extension and use that as my filter criteria for the File Open dialog. Once the user selects a file with that extension (from the File Open...
2
4818
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> #include<fstream>
1
3686
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 = open('c:\python25\integer.txt','r')---->for reading print i.read()---->for printing the contents in that text file i =...
1
64071
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
14
1752
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 ended) to read the file, I find that the data I have written doesn't exist in there. I can't read it back until I run the script again. My question...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.