I/O operation, file operation behaviou

R

raan

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< string> openFiles;
std::string fileName;
std::string type;
};

void CFileXP::eek:pen(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::eek:ut | 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::eek:ut | ios::app);
fd << "</" + *it + "S>" + "\r\n";
fd.close();
}
openFiles.erase(openFiles.begin(), openFiles.end());
}

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

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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?
 
B

BobR

raan said:
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< string> openFiles;
std::string fileName;
std::string type;
};

void CFileXP::eek:pen( std::string _type ){
// > set said:
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::eek:ut | ios::trunc);
// note: s/b ...., std::ios_base::eek:ut | 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::eek:fstream 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::eek:pen(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::eek:fstream fd( fileName.c_str(),
std::ios_base::eek:ut | 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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fstream File i/o 1
Does the open operation succeed? 2
fstream - write a file 3
ValueError: I/O operation on closed file. with python3 0
Help with binary I/O 4
help with file I/O 4
I need help 1
TF-IDF 2

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top