Revisited an old problem

This is a discussion on Revisited an old problem within the C++ Programming forums, part of the General Programming Boards category; Sorry to revisit this subject, but I have another question with I/O files. Do I need to have both *ifstream* ...

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

    Revisited an old problem

    Sorry to revisit this subject, but I have another question with I/O files. Do I need to have both *ifstream* & *ofstream* statements in order to reopen a file? The first part worked just fine, but the second part did not reopen the file to write the lowest numbers.

    If I change from *b_file* to *e_file* in the second part, I get an *e_file is undefined* error. Is it the *ifstream* statement that DEFINES the file & opens it for reading?

    My first part is:
    Code:
    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
    
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    
    d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
    d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
    d_file << endl << endl;
    
    d_file.close();   // Close the file stream explicitly
    
    cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
    cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;
    My second part is:
    Code:
        
    ofstream out1;
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
    
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    
    d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
    d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
    d_file << endl << endl;
    
    d_file.close();   // Close the file stream explicitly

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,690
    In the snippet below:
    Code:
    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" ); 
     
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    The first line creates an instance of an ofstream and opens the file for output the 'o' of ofstream stands for output. Because you did not use any open mode modifiers this will erase the file. So that means your if statement is not correct. You did not try to open the file for input.

    In the following snippet:
    Code:
    ofstream out1;
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
     
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    The first line creates an instance of an ofstream. The second line is opening a file for output and erasing the file when opening.

    These are two different streams d_file and out1 both trying to use the same file for output. This will cause problems.


    You need to study the following tutorials: Basic file input and output, and the documentation for ifstream and ofstream, both the ifstream and ofstream documentation pages have multiple pages, make sure you look at several of those pages, especially the pages for the constructors.

    You may also need to study these links: Classes I and Classes II, because in my opinion you don't have much understanding as to how classes work.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    The following code worked just fine.

    Code:
    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
    
     
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
     
    d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
    d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
    d_file << endl << endl;
     
    d_file.close();   // Close the file stream explicitly
     
    cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
    cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;
    If I stop the program here with a *cin.get()* and check the file, It was written into just fine. However, If I remove the *cin.get()* to let the program continue,

    Code:
    ofstream out1;
    
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
     
    if (! d_file )
    
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
     
    d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
    d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
    d_file << endl << endl;
     
    d_file.close();   // Close the file stream explicitly
    not only does not update the file with new data, it erases the file, leaving it empty. I thought that
    Code:
    ofstream out1;
    
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
    was supposed to open the file, write to it without erasing anything that was in it.

    As far as your reading assignments, I thank you, however I have already read everything that I could fine on the subject. Much of it is so tech, that it leaves me confused. Please remember that I'm too new to C++ to be comfortable with the readings... but, I'm learning. . . . anyway, I'm greatful for your help!!!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,690
    I thought that
    Code:
    ofstream out1;
     
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
    was supposed to open the file, write to it without erasing anything that was in it.
    No that will erase the file every time. If you don't want to erase the file you need to use the "optional" open mode parameter. You need to use either the app, or ate open modes if you don't want to erase the files.

    Why are you opening and closing this file so often? Why not just open it once?

    Jim

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    you'll need
    Code:
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" , ios_base::app);
    to append it to the file.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Thanks. I am opening the file to write the numbers with the highest frequencies, then reopening the file to write the numbers having the lowest frequencies. I was tole that I have to close the file if I wanted to be available to another part of the program.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Thanks nimitzhunter http://im.cprogramming.com/images/st...ser-online.png, That's what I needed. I forgot that.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Sorry, that did not work. It is true that the addition of * std::ios::app* caused the program to stop erasing the content of the file, but the file is still not up-dated with the new data. What am I still doing wrong? The new data:

    Code:
      cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
            cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;    
            
        
        
            ofstream out1;
            out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app);
            
    
            if (! d_file )
            {
            cout << "Can't open input file " << endl << endl;
            cin.get();
            exit(1);
            }
    
            d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
            d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third    << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
            d_file << endl << endl;
    
            d_file.close();   // Close the file stream explicitly
    is not being up-dated in the file.

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    not sure if this's relevant but
    Code:
    cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
          cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;    
           
       
       
          ofstream out1;
          out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app);
           
     
          if (! d_file )
          {
          cout << "Can't open input file " << endl << endl;
          cin.get();
          exit(1);
          }
     
          d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
          d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third    << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
          d_file << endl << endl;
     
          d_file.close();   // Close the file stream explicitly
    I don't see how out1 and d_file are related. How did open the file for d_file?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  10. #10
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    I'm sorry about being so thick here, but I do not understand your question? The code that you quoted is the new data that I wish to write/append into the file. Are you implying that I should use *out_2* instead of *out_1* because the file was opened once before?. . . and would using *out_1* stop the program from writing the new data into it? Thanks for your help.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    I was about to ask the same question as nimitzhunter.

    You are opening out1 as output.
    If d_file is false you are outputting a messages about some input file not being opened.
    Then you try and output to d_file.

    Seriously, make up your mind about what variable you're using and whether you're doing input or output with it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    I understand that

    Code:
     if (! d_file )       {       cout << "Can't open input file " << endl << endl;       cin.get();       exit(1);       }
    only checks to see if the file is opened by:
    Code:
    ofstream out1; out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app);
    If the file was opened, then the *if* code will not even be seen. As I understand it, the *if* statement will
    only be seen if the file is NOT opened, am I correct? After the *if* statement sees that the file is indeed open,
    then the program should continue with the next task, which is writing/updating the file with
    ;code]
    d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl; d_file << indexoffirst+1 << " = " << first << ", " << indexofsecond+1 << " = " << second << ", " << indexofthird+1 << " = " << third << ", " << indexoffourth+1 << " = " << fourth << endl <<endl; d_file << endl << endl; d_file.close(); // Close the file stream explicitly
    [/code]
    which is the new data that I want appended to the file.

  13. #13
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Code:
    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt" );
    that that one in your original post. did you add the ios_base option to that one as well?
    Code:
    d_file.close();   // Close the file stream explicitly
    if it's closed, have you tried to reopen the file somewhere?
    Then again, you don't have to come up with new variable every single time you want to open a new file. You can just reopen it.
    Code:
         if (!d_file.is_open())
          d_file.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app);
         
          d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
          d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third    << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
          d_file << endl << endl;
     
          d_file.close();   // Close the file stream explicitly
    BTW, if multiple input and output from the same file is desired, you can always use 'fstream' for both since closing and opening the same file is a bit boring.
    Code:
     fstream file;
     file.open("bleh blhe bleh", ios_base::in | ios_base::out | ios_base::app);
    Last edited by nimitzhunter; 04-21-2012 at 04:20 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    2,690
    NO!
    Code:
    if (! d_file )
    Only checks if d_file opened correctly. It has nothing to do with out1.

    You don't seem to understand that ofstream is a type, just like an int or a double. You create an instance of an ofstream using a variable just like you create an instance of an int.

    Code:
     int a; // Create an int variable wiht the name a.
    ofstream output; // Create an ofstream variable with the name output.
    output.open("SOMESTIPIDFILE"); // Open a file named SOMESTUPIDFILE using the ofstream variable named output.
    ofstream test("SOMEFILENAMETEST"); // Create an ofstream variable with the name of test, open the file named SOMEFILENAMETEST.
    Jim

  15. #15
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    OK, I commented out the troubling
    Code:
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    and reran the program. Unfortunately, The new data still is not being updated/appended into the file.

    [code]
    // Prints to the screen
    cout << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
    cout << indexoffirst+1 << " = " << first << ", " << indexofsecond+1 << " = " << second << ", " << indexofthird+1 << " = " << third << ", " << indexoffourth+1 << " = " << fourth << endl <<endl;
    cout << endl << endl;

    ofstream d_file ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Fre q.txt" );

    /*
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    */

    d_file << "The 4 numbers having the Highest Frequencies are:" << endl << endl;
    d_file << indexoffirst+1 << " = " << first << ", " << indexofsecond+1 << " = " << second << ", " << indexofthird+1 << " = " << third << ", " << indexoffourth+1 << " = " << fourth << endl <<endl;
    d_file << endl << endl;

    d_file.close(); // Close the file stream explicitly
    Code:
     
    is already written into the file "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Fre q.txt". Up to this point everything is just fine.
    Now I want to update the same file by appending:
    Code:
     cout << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
    cout << indexoflowest+1 << " = " << lowest << ", " << indexofsecondlowest+1 << " = " << secondlowest << ", " << indexofthirdlowest+1 << " = " << thirdlowest << ", " << indexoffourthlowest+1 << " = " << fourthlowest << endl <<endl;    
            
    ofstream out1;
    out1.open ( "D:\\1_LotteryPrograms\\1_Fla_Mega_Money\\High_Freq.txt", std::ios::app);
            
     /*
    if (! d_file )
    {
    cout << "Can't open input file " << endl << endl;
    cin.get();
    exit(1);
    }
    */
    
    d_file << "The 4 numbers having the Lowest Frequencies are:" << endl << endl;
    d_file << indexoffirst+1 << " = " << first << ",  " << indexofsecond+1 << " = " << second << ",  " << indexofthird+1 << " = " << third << ",  " << indexoffourth+1 << " = " << fourth << endl <<endl;
    d_file << endl << endl;
    
    d_file.close();   // Close the file stream explicitly
    which is the new data, but it is not being written/appended to the file.
    What am I doing wrong?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration revisited
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 09-05-2007, 09:38 AM
  2. C language revisited
    By oloc in forum C Programming
    Replies: 2
    Last Post: 05-12-2005, 04:34 PM
  3. nop - revisited
    By oioi in forum Tech Board
    Replies: 4
    Last Post: 12-28-2004, 09:18 AM
  4. VSD revisited
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 05-01-2004, 04:27 PM

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