Thread: problem with setting of file name

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    problem with setting of file name

    well here is my code... and i will bold what is being defined as an error.
    Code:
    void MCreateFile()
    {
        ofstream File;
        ifstream testFile;
        string filename;
        
        CLEARSCREEN;
        
        cout<<"Please enter the filename: ";
        cin>>filename;
        if(testFile.open(filename.c_str()))
        {
           testFile.close();
           
           int samefile;
           
           cout<<"\nThere is a file at the current save location with the same name...";
           cout<<"\n  <1>Overwrite";
           cout<<"\n  <2>Rename Current File";
           cout<<"\n  <3>Quit";
           cout<<"\n  ->: ";
           cin>>samefile;
           switch(samefile)
           {
                           case 1:
                                testFile.open(filename.c_str(), ios::trunc);
                                testFile.close();
                                break;
                           case 2:
                                filename.clear();
                                cout<<"\nNew File Name: ";
                                cin>>filename;
                                break;
                           case 3:
                                Quit();
                                break;
                           default:
                                   cout<<"\nNot a valid entry!";
                                   cout<<"\n  ->: ";
           }
        }
                       
        if(!File.open(filename.c_str()));
        {
             cout<<"File could not be created!";
             Quit();
        }
    }
    here are my compiler errors....
    Code:
    73 C:\Dev-Cpp\My Stuff\C++\PhoneBook\PhoneBook4\definitions.h could not convert `(&testFile)->std::basic_ifstream<_CharT, _Traits>::open [with _CharT = char, _Traits = std::char_traits<char>]((&filename)->std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](), (std::_Ios_Openmode)8u)' to `bool' 
    105 C:\Dev-Cpp\My Stuff\C++\PhoneBook\PhoneBook4\definitions.h could not convert `(&File)->std::basic_ofstream<_CharT, _Traits>::open [with _CharT = char, _Traits = std::char_traits<char>]((&filename)->std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](), std::operator|((std::_Ios_Openmode)16u, (std::_Ios_Openmode)32u))' to `bool' 
    105 C:\Dev-Cpp\My Stuff\C++\PhoneBook\PhoneBook4\definitions.h in argument to unary !
    i realize that when you have a filename, it has to be a const char* type. whats goin on?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm not intirely clear on what open() returns right now, but you can use the function testFile.is_open() instead. Let the filestream try to open the file all the time.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    open() returns a void, so it can't be used as part of a conditional expression

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    try
    Code:
    testfile.open(filename.cstr())
    if (!testfile.fail())
    instead.
    There is a difference between tedious and difficult.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    ooo ok... that makes since...
    but that makes me run into another problem...
    is there a function i can use to test to see if the filename of the file that the user chooses is already in the current directory?
    like a overwrite or rename window?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In standard C++, you'll probably want to just open the file for reading, and if it fails to open, it doesn't exist. There are platform specific options, and there is also Boost::filesystem, if you want more control or a nicer interface.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It depends on the mode that you use open with. There are lots of them, and you can use them in combination if they're separated by a vertical bar.
    Code:
    ios::app append output 
    ios::ate  seek to EOF when opened 
    ios::binary open the file in binary mode 
    ios::in  open the file for reading 
    ios:: out  open the file for writing 
    ios::trunc  overwrite the existing file
    The default for ifstream is ios::in, so you don't have to really worry about overwriting. If you don't want to overwrite, just create a new file with a different name after the other one opens.
    Last edited by whiteflags; 05-11-2006 at 07:01 PM.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    ok... i got rid of the compiler errors by taking the open function out of the conditionals...
    now i get bad logic....

    Code:
    /*********************/
    /**** MCreateFile ****/
    /*********************/
    void MCreateFile()
    {
        string filename;
        
        CLEARSCREEN;
        
        cout<<"Please enter the filename: ";
        cin>>filename;
        
        ifstream testFile(filename.c_str());
        
        if(testFile.is_open())
        {
           testFile.close();
           
           int samefile;
           
           cout<<"\nThere is a file at the current save location with the same name...";
           cout<<"\n  <1>Overwrite";
           cout<<"\n  <2>Rename Current File";
           cout<<"\n  <3>Quit";
           cout<<"\n  ->: ";
           cin>>samefile;
           switch(samefile)
           {
                           case 1:
                                testFile.open(filename.c_str(), ios::trunc);
                                testFile.close();
                                break;
                           case 2:
                                filename.clear();
                                cout<<"\nNew File Name: ";
                                cin>>filename;
                                break;
                           case 3:
                                Quit();
                                break;
                           default:
                                   cout<<"\nNot a valid entry!";
                                   cout<<"\n  ->: ";
           }
        }          
        ofstream File(filename.c_str());     
        if(!File);
        {
             cout<<"File could not be created!";
             Quit();
        }
    The bolded text is where the problem lies....
    I check to see if the file isnt created... and if it isnt ... it says what it says...
    And it even outputs that if i have if(File)
    it works for both true and false...
    whats goin on?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
        ofstream File(filename.c_str());     
        if(!File);
        {
             cout<<"File could not be created!";
             Quit();
        }
    The semicolon is your problem, because now no matter what, the code block following it will execute.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    hahaha... wow.... what a newbish mistake...ahhh its late... im tired... thats my excuse...
    thanks bro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM