Thread: having problems with creating a file

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    having problems with creating a file

    I can't seem to figure out why I cannot create a file, when I specify a nonexistent file, it fails, but I can reopen files that already exist. Also I am not sure if deleting the filestream is neccessary

    Code:
    #include <fstream>
    
    class Record{
          private:
                  char* file_name;      //the location of the record on the disk
                  fstream* pfile;       //pointer to the file stream
                                        //it is null if the stream is closed
          public:
                 Record();
                 ~Record();
                 
                 void open_file(char* input_string);
                 void close_file();
    };
    
    Record::Record()
    {
        pfile=NULL;
    }
                    
    void Record::open_file(char* input_string)
    {
        close_file();
        file_name = new char[strlen(input_string)+1];
        strcpy(file_name, input_string);
        pfile = new fstream(file_name, ios::binary | ios::in | ios::out);
        if(!*pfile)
            throw file_name;
    }
    
    void Record::close_file()
    {
        if(pfile!=NULL)
        {
            pfile->close();
            delete pfile;//Is this neccesary?
            pfile=NULL;
        }
    }
    
    Record::~Record()
    {
        delete [] file_name;
        close_file();
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Perhaps specifying ios::in makes the open fail if the file doesn't exist?

    >> //Is this neccesary?
    With that code, yes, you allocated pFile with new, so you must delete it. Although I'm not sure why you need to make the fstream a pointer, why not just a regular member, and then use the open() function in your open_file function?

    Also, why not use a C++ string instead of C style character array to avoid having to manage the memory?

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    int main(void)
    {
    	fstream in("C:\\doesnotexist.txt", ios::in);
    	if(!in)
    	{
    		cout << "Error opening doesnotexist.txt for reading\n";
    	}
    
    	fstream both("C:\\doesnotexist.txt", ios::in | ios::out);
    	if(!both)
    	{
    		cout << "Error opening doesnotexist.txt for both\n";
    	}
    
    	fstream out("C:\\doesnotexist.txt", ios::out);
    	if(!out)
    	{
    		cout << "Error opeaning doesnotexist.txt for writing\n";
    	}	
    }
    First two fail. Last one creates. Dinkumware provides a nice little chart for openmodes: https://www.dinkumware.com/manuals/?...#filebuf::open And yeah, that deletion is necessary. Anything allocated with new needs a corresponding delete (likewise with new[] and delete[])

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    thankyou, that seemed to do the trick, but presents a new problem of having to close the filestream and reopen a new one to change from reading to writing and so on, I should be able to have both specified, as i've done it before. Thankyou for the help, I wanted the fstream as a pointer because I'm stupid and don't know how to check and see if the stream is open or not. If you know how to do that it would be helpfull, or point me to a tutorial please so i can stop badgering you.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Call is_open()?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What behavior do you want from your filestream?

    Reading + writing + append if exists? Reading + writing + trunc if it doesn't?

    Also, basically, if it wasn't a pointer, you would just change a few things. Like.

    Code:
    pfile = new fstream(file_name, yropenmodeflags);
    pfile.open(file_name, yropenmodeflags);
    Code:
    if(!*pfile)
    if(!pfile)
    Code:
    void Record::close_file()
    {
        if(pfile!=NULL)
        {
            pfile->close();
            delete pfile;//Is this neccesary?
            pfile=NULL;
        }
    }
    
    void Record::close_file()
    {
            pfile.close();
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You might also want to call clear() when you close the file just in case there were any errors during reading or writing.

    You could attempt to open with ios::in, and if that fails, create a new file first, then open it with ios::in or just remember that the file is new and can't be read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM