Where to put ios::app?

This is a discussion on Where to put ios::app? within the C++ Programming forums, part of the General Programming Boards category; Sorry to be such a dummy, but exactly where would you put: Code: ofstream out1; out1.open("D:\1_LotteryPrograms\1_Fla_Mega_Money\Highest_Frequencies.txt", std::ios::app); to append my ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    Where to put ios::app?

    Sorry to be such a dummy, but exactly where would you put:

    Code:
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    to append my file? My code is:

    ifstream e_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_ Frequencies.txt" );

    Code:
    if (!e_file )
    {
    cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
    cin.get();
    exit(1);
    }
            
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        
    if (! f_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    
    cout << "Mega_Money's 4 highest numbers are:" << endl << endl;
    f_file << indexoffirst << " = " << first << "  " << indexofsecond << " = " << second << "  " << indexofthird << " = " << third << "  " << indexoffourth << " = " << fourth;
    Right now, the file gets erased instead of being up-dated/appended.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" ); 
        // this opens and erases the contents of the output file
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        // now you open it again and append 
        // will propably fail because the file is already open
    if (! f_file )
    {
    cout << "Can't open input file " << endl << endl;
        // f_file is the output file 
    cin.get();
    exit(1);
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    // will probably fail because the file is already open

    It's does me no good it it fails, how can I do it so that is does not fail?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    How about only opening the file once. You are trying to open the same file twice. The first time you erase the file because you don't change the open mode. I suggest you remove lines 10 and 11 and change your ofstream on line 8 to include the proper open mode.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Sorry, this newbie is really slow when it comes to C++.

    Quote Originally Posted by jimblumberg View Post
    How about only opening the file once. You are trying to open the same file twice. The first time you erase the file because you don't change the open mode. I suggest you remove lines 10 and 11 and change your ofstream on line 8 to include the proper open mode.

    Jim
    Are you suggesting that I replace line 8 with lines 10 & 11?

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    two possibilities
    either remove lines 10 & 11 and add the openmode to the constructor in line 8
    or remove line 8
    Kurt

  7. #7
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Quote Originally Posted by ZuK View Post
    two possibilities
    either remove lines 10 & 11 and add the openmode to the constructor in line 8
    or remove line 8
    Kurt
    My code is:
    Code:
    ifstream e_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    
    if (!e_file )
    {
    cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
    cin.get();
    exit(1);
    }
            
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        
    if (! f_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
                }
    //cout << "Mega_Money's 4 lowest numbers are:" << endl << endl;
    f_file << indexofone << " = " << one << "  " << indexoftwo << " = " << two << "  " << indexofthree << " = " << three << "  " << indexoffour << " = " << four;

    If I:

    Code:
            
    // ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
       ofstream out1;
       out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
       ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    I get the following errors:
    Code:
    Error    1    error C2065: 'f_file' : undeclared identifier    249
    Error    2    error C2065: 'f_file' : undeclared identifier    257
    Error    3    IntelliSense: identifier "f_file" is undefined    249

    If I:

    Code:
    // ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    // ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);

    I still get the same errors. I'm confused. Where exactly in my code do I put the append code?

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Therry View Post
    My code is:
    If I:

    Code:
            
    // ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
       ofstream out1;
       out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
       ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    I get the following errors:
    Code:
    Error    1    error C2065: 'f_file' : undeclared identifier    249
    Error    2    error C2065: 'f_file' : undeclared identifier    257
    Error    3    IntelliSense: identifier "f_file" is undefined    249

    If I:

    Code:
    // ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    // ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);

    I still get the same errors. I'm confused. Where exactly in my code do I put the append code?
    you removed the declaration of f_file in both versions
    Code:
     // ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    and later on you still use it
    Code:
    if (! f_file )
    { ....
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    I took your suggestion and replaced both:

    Code:
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    but the file "Highest_Frequencies.txt still is not appended.

    Is it because I opened that file earlier in the program to write the Highest Frequencies in:

    Code:
    ifstream c_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    if (!c_file )
    {
    cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
    cin.get();
    exit(1);
    }
            
    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    //ofstream out1;
    //out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    //ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    Then I try to open it again in:

    Code:
    ifstream e_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    
    if (!e_file )
    {
    cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
    cin.get();
    exit(1);
    }
            
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        
    if (! f_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    Is this my problem? Should I use the append code each and every time I try to open the file? or just the first time?

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Sorry, I really don't understand what you're trying to do.
    It seems alwais to be the same file ("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest _Frequencies.txt") that you're opening for reading and writing. But you never close it.
    Kurt

  11. #11
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Are you saying that I must close the file "Highest_Frequencies.txt" each time I've finished writing to it?
    Is that the only safe way to recall/reopen the file to append it?
    I did not know this. I think I understand. My code should be:

    Code:
    // ==================
    ifstream c_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    
    if (!c_file )
    {
    cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
    cin.get();
    exit(1);
    }
            
    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    
    d_file << "This is for b_file" << endl << endl;
    d_file << "Mega_Money's 4 highest numbers are:" << endl << endl;
    d_file << indexoffirst << " = " << first << "  " << indexofsecond << " = " << second << "  " << indexofthird << " = " << third << "  "     <<    indexoffourth << " = " << fourth << endl << endl;
    d_file.close();   // Close the file stream explicitly
    // ======================
    before I can reopen it in:

    Code:
    ifstream e_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    
    if (!e_file )
    {
    cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
    cin.get();
    exit(1);
    }
            
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
        
     if (! f_file )
     {
     cout << "Can't open input file " << endl << endl;
     cin.get();
     exit(1);
     }
    
     f_file << "This is for f_file" << endl << endl;
     f_file << "Mega_Money's 4 lowest numbers are:" << endl << endl;
     f_file << indexofone << " = " << one << "  " << indexoftwo << " = " << two << "  " << indexofthree << " = " << three << "  " << indexoffour << " = " << four;
     f_file.close();   // Close the file stream explicitly
    // =========================
    Am I correct so far?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    No you are not correct. How many times do you need to try to open your file? You are opening the same file at least four times. One for reading and three for writing. Why are you trying to open the file three times for writing?

    Code:
    // open file for reading.
    ifstream e_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" ); 
             
    // open file for writing. This will erase your file contents.
    ofstream f_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt" );
    
    // open same file again for writing. If there was anything left, this call will not erase the file.
    ofstream out1;
    out1.open("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    
    //open same file a third time for writing. This call would not erase the file.
    ofstream out2("D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\Highest_Frequencies.txt", std::ios::app);
    You really should open the file for reading, read the contents into memory, then close the file. Then open the file for writing, preserve the contents if so desired. Write your information to the file, close the file.

    Jim
    Last edited by jimblumberg; 04-19-2012 at 09:29 PM.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    WOW!. . . . Thanks for your patience!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21