472,958 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2301
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.