468,290 Members | 1,937 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

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 2153
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Khurram Ishfaq Rathor | last post: by
8 posts views Thread by Jim in Arizona | last post: by
1 post views Thread by spacehopper_man | last post: by
reply views Thread by Default User | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.