I/O operation, file operation behaviou

Discussion in 'C++' started by raan, Aug 16, 2007.

  1. raan

    raan Guest

    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");
    }
    raan, Aug 16, 2007
    #1
    1. Advertising

  2. 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
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 16, 2007
    #2
    1. Advertising

  3. raan

    BobR Guest

    raan <> 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::eek: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::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;
    > } // main()
    >


    --
    Bob R
    POVrookie
    BobR, Aug 16, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc S. Gibian

    Rigorous File operation error checking

    Marc S. Gibian, Jul 15, 2003, in forum: Perl
    Replies:
    2
    Views:
    1,177
    Andrew Savige
    Jul 22, 2003
  2. J Gao
    Replies:
    0
    Views:
    1,210
    J Gao
    Jul 28, 2003
  3. O.B.
    Replies:
    0
    Views:
    474
  4. david ullua
    Replies:
    13
    Views:
    663
  5. Buzz Lightyear
    Replies:
    10
    Views:
    1,119
    Alexander Bartolich
    Aug 12, 2009
Loading...

Share This Page