473,473 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I/O operation, file operation behaviou

Please see the following program. My intention is to open the file
(create it if it does not exist), or if the file exists already it
should be truncated (the entire contents is thrown away) and over
written. "Create if does not exists works fine", What is wrong with
the truncating part ?. fd.is_open returns false always.


#include <iostream>
#include <fstream>
#include <string>
#include <set>

using namespace std;

class CFileXP {
public:
void open(std::string type);
void close(string method);

private:
std::set< stringopenFiles;
std::string fileName;
std::string type;
};

void CFileXP::open(std::string _type)
{
set< string>::iterator it;
type = _type;
fileName = "C:\\temp\\" + type + "XML.xml";
std::fstream fd;
it = openFiles.find(type);
if (it == openFiles.end()) {
openFiles.insert(type);
fd.open(fileName.c_str(), ios::out | ios::trunc);
if (fd.is_open()) {
cout << "File is opened now\n";
}
fd << "<" + type + "S>" + "\r\n";
}
fd.close();
}

void CFileXP::close(string method)
{
set< string>::iterator it;
std::fstream fd;
for (it = openFiles.begin(); it != openFiles.end(); it++) {
fileName = "C:\\temp\\" + *it + "XML.xml";
fd.open(fileName.c_str(), ios::out | ios::app);
fd << "</" + *it + "S>" + "\r\n";
fd.close();
}
openFiles.erase(openFiles.begin(), openFiles.end());
}

int main()
{
CFileXP file;
file.open("MYTPE");
}

Aug 16 '07 #1
2 2313
On 2007-08-16 05:35, raan wrote:
Please see the following program. My intention is to open the file
(create it if it does not exist), or if the file exists already it
should be truncated (the entire contents is thrown away) and over
written. "Create if does not exists works fine", What is wrong with
the truncating part ?. fd.is_open returns false always.
Are you sure you got the name of the file right? You sure you got a
C:\temp folder?

--
Erik Wikström
Aug 16 '07 #2

raan <pa*******@gmail.comwrote in message...
Please see the following program. My intention is to open the file
(create it if it does not exist), or if the file exists already it
should be truncated (the entire contents is thrown away) and over
written. "Create if does not exists works fine", What is wrong with
the truncating part ?. fd.is_open returns false always.

#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;

class CFileXP{ public:
void open(std::string type);
void close(string method);
private:
std::set< stringopenFiles;
std::string fileName;
std::string type;
};

void CFileXP::open( std::string _type ){
// set< string>::iterator it;
type = _type;
fileName = "C:\\temp\\" + type + "XML.xml";
std::cout<<fileName<<std::endl;
// compare to the file name/path on your HD.

// std::fstream fd;
// it = openFiles.find(type);
// if (it == openFiles.end()) {
// openFiles.insert(type);
// fd.open(fileName.c_str(), ios::out | ios::trunc);
// note: s/b ...., std::ios_base::out | std::ios_base::trunc );

You are opening the stream for output only, so, try it this way:

set< string>::iterator it( openFiles.find( type ) );
if( it == openFiles.end() ){
openFiles.insert( type );
std::ofstream fd( fileName.c_str() );

if( not fd.is_open() ){
std::cout << "File failed to open"<<std::endl;
return; // should be "return false;" (or true)
// .... then you could test it where it's 'called'.
} // if(!open)
if( fd.is_open() ){
cout << "File is opened now\n";
} // if(open)
fd << "<" + type + "S>" + "\r\n";
// fd.close(); // next line will close it
} // if(it)
// fd.close();
} // CFileXP::open(string)

void CFileXP::close( string method ){
[snip]
}
bool CFileXP::close( string method ){ // add 'bool' in class def.
for( set< string>::iterator it( openFiles.begin() );
it != openFiles.end(); ++it ){
fileName = "C:\\temp\\" + *it + "XML.xml";
std::ofstream fd( fileName.c_str(),
std::ios_base::out | std::ios_base::app );
if( not fd.is_open() ){
std::cout << "File failed to open"<<std::endl;
return false;
} // if(!open)
fd << "</" + *it + "S>" + "\r\n";
fd.close();
} // for(it)
openFiles.erase( openFiles.begin(), openFiles.end() );
return true;
} // CFileXP::close(string)
>
int main(){
CFileXP file;
file.open("MYTPE");
if( file.close( "MYTPE" ) ){
std::cout<<"file written to disk."<<std::endl;
}
else{
std::cout<<"file FAILED to write!"<<std::endl;
return EXIT_FAILURE;
}
return 0;
} // main()
--
Bob R
POVrookie
Aug 16 '07 #3

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

Similar topics

0
by: Khurram Ishfaq Rathor | last post by:
Hello, I'm developing a web appliation in ASP.Net. My web appliation communicates with Web Services for several operations. Everything is working fine except one operation. Web application...
0
by: John Jenkins | last post by:
Hi, any help on this would be greatly apprciated. I have been given a wsdl file from a customer generated by Oracle 10g Web Services tools. When I use wsdl.exe to attempt to produce proxy classes I...
8
by: Jim in Arizona | last post by:
I've been using an example out of a book to be able to edit the rows in a database. I am getting the following error: ========================================================...
3
by: JuHui | last post by:
I wrote a script to get 100 pages from a server. like below: 1:import httplib 2:conns = httplib.HTTPConnection("www.mytest.com") 3:conn.request("GET", "/") sometimes a socket error was...
1
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum...
1
by: thebigsquid | last post by:
hi, its me again, so this software obviously has serious problems. now i'm getting this error message when i try to access any of it! any ideas much appreciated thanks the big squid An...
1
by: pavya | last post by:
Hi, I have developed one Web application. At that time my system had a FAT file system on it and this application worked properly. But now i have converted FAT file system to NTFS file system and...
0
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the...
2
by: scriptlearner | last post by:
OS: Solaris 9 Python Version: 2.4.4 I need to log certain data in a worker thread; however, I am getting an error now when I use two worker threads. I think the problem comes from the line...
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...
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
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
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...
1
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.