Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I/O operation, file operation behaviou

Reply
Thread Tools

I/O operation, file operation behaviou

 
 
raan
Guest
Posts: n/a
 
      08-16-2007
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: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: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:ut | ios::app);
fd << "</" + *it + "S>" + "\r\n";
fd.close();
}
openFiles.erase(openFiles.begin(), openFiles.end());
}

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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      08-16-2007
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
 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      08-16-2007

raan <(E-Mail Removed)> wrote 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< string> openFiles;
> std::string fileName;
> std::string type;
> };
>
> void CFileXP:pen( 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:ut | ios::trunc);
// note: s/b ...., std::ios_base: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: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: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:fstream fd( fileName.c_str(),
std::ios_base: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;
> } // main()
>


--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
Executing a static non-main operation in a jar file contained withina war file O.B. Java 0 06-24-2006 05:47 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
The file 'bin\WebProj.dll' cannot be copied to the run directory. The requested operation cannot be performed on a file with a user-mapped section open. J Gao ASP .Net 0 07-28-2003 04:18 PM
Rigorous File operation error checking Marc S. Gibian Perl 2 07-22-2003 09:32 AM



Advertisments