How to open the output file?

Discussion in 'C++' started by hei, Mar 9, 2009.

  1. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
        int x ;
        char y[1000] ;
        char str[10];
        char op [5] ;
    
    
        cout << "Wait for input: ";
        // get input, if the input is not "open", wait for another input
       
    
        
        //Creates an instance of ofstream, and opens example.txt
        ofstream a_file ( "example.txt" );
        // Outputs to example.txt through a_file
        cout<<"type some numbers :";
        cin>> x ;
        a_file<<"number :" <<x ;
        cout<<"type some alphabet :";
        cin>> y ;
        a_file<<"alphabet: "<<y ;
        cin.ignore();
         while (true) {
            cin >> op;
            if(strcmp(op, "open")== 0) break;
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
        
        // Close the file stream explicitly
        
        a_file.close();
        
        //Opens for reading the file
        ifstream b_file ( "example.txt" );
        //Reads one string from the file
        b_file>> str;
        //Should output 'this'
        cout<< str <<"\n";
        FILE *fp;ss
        fp=fopen("G:\\ccc\\example.txt", "r");
        system("pause");
    }
    the program in ms-dos type, it will ask the user to type some number and alphabet and save those things that typed by user into a certain folder(for my situation, the file location: G:\ccc. and the name is example.txt). The problems is i want the program open(for write or read) the file if the user type "open" and pressed enter. But it did not open anythings. Can somebody help me.

    Thanks in advance.:pleased::nice:;):D
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Code:
    ofstream a_file ( "example.txt" );
    Try specifying the location.
     
  3. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    I edit it :
    Code:
    cin.ignore();
         while (true) {
            cin >> op;
            if(strcmp(op, "open")== 0) fopen("g:\ccc\example.txt","r"); break;
            
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
    2 error came out:
    1.36 G:\ccc\dllmain.cpp expected primary-expression before "else"
    2.36 G:\ccc\dllmain.cpp expected `;' before "else"

    I don't get its points, where should i put the primary bracket?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Let's just indent that correctly and see if the problem becomes obvious:
    Code:
    cin.ignore();
    while (true)
    {
        cin >> op;
        if(strcmp(op, "open")== 0)
            fopen("g:\ccc\example.txt","r");
        break;
        else
            cout << endl << "Hint: open" << endl << "type again:" ;
    }
    
    So you have an unconditional break and an else without an if. The if is the primary expression (note it says EXPRESSION not BRACKET - read the messages!)

    Don't forget whitespace is completely irrelevant to C; if you want an if to execute two statements, you MUST enclose them in braces:
    Code:
        if(strcmp(op, "open")== 0)
        {
            fopen("g:\ccc\example.txt","r");
            break;
        }
    
    Otherwise you end up confusing yourself and not being able to see what is wrong with stuff like
    Code:
        if(strcmp(op, "open")== 0) fopen("g:\ccc\example.txt","r"); break;
    
    - the break here is NOT conditional.

    Indentation lesson learned?
     
  5. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    After i edit the code, it become like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
        int x ;
        char y[1000] ;
        char str[10];
        char op [5] ;
    
    
        cout << "Wait for input: ";
        // get input, if the input is not "open", wait for another input
       
    
        
        //Creates an instance of ofstream, and opens example.txt
        ofstream a_file ( "G:\\ccc\\example.txt" );
        // Outputs to example.txt through a_file
        cout<<"type some numbers :";
        cin>> x ;
        a_file<<"number :" <<x ;
        cout<<"type some alphabet :";
        cin>> y ;
        a_file<<"alphabet: "<<y ;
        
        cin.ignore();
         while (true) {
            cin >> op;
            if(strcmp(op, "open")== 0){
                          fopen("g:\\ccc\\example.txt","r"); break;
                          }
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
        
        // Close the file stream explicitly
        
        a_file.close();
        
        //Opens for reading the file
        ifstream b_file ( "g:\\ccc\\example.txt" );
        //Reads one string from the file
        b_file>> str;
        //Should output 'this'
        cout<< str <<"\n";
        FILE *fp;
        fp=fopen("g:\\ccc\\example.txt", "r");
        system("pause");
    }
    
    
    I am sure that the example.txt with this location:G:\ccc is exists.
    But weird why no file open?
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How do you know it doesn't open the file?
    Which line of code do you think fails?
     
  7. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    I think every line that use to open the file fails, but i don't know the culprit.
    Its is better if this part can be fix:
    Code:
    if(strcmp(op, "open")== 0){
                          fopen("g:\\ccc\\example.txt","r"); break;
                          }
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
        
        // Close the file stream explicitly
        
        a_file.close();// i also tried delete this line but it also did not work
    
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, have you RTFM on fopen? Specifically have you read about fopen's return value?
     
  9. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    You mean if the fopen successfully, it will open the file but if wrong it will do nothings?
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, I mean that fopen returns a pointer to a FILE structure that you can use to manipulate the file, but you just drop this rather essential value by not assigning it to anything. So you have no way of knowing whether or not the file was opened successfully.

    Actually you almost do it correctly at the end of the program, but I can't see the point of opening a file just before you exit. What exactly do you think fopen does?
     
  11. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    fopen is the code that return the pointer of a file structure, but i don't know very know how to return the pointer correctly that make the file open.
    ofstream is a code use to open the file mentioned.
    Code:
    ofstream  a_file ( "test.txt", ios::app )
    I tried this but still not work as I want(open the out put file/a_file/file with location:G:\ccc\example.txt).

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    #include <cstdio>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
        int x ;
        char y[1000] ;
        char str[10];
        char op [5] ;
    
    FILE *fp;
        fp=fopen("g:\\ccc\\example.txt", "r");
    
        cout << "Wait for input: ";
        // get input, if the input is not "open", wait for another input
       
    
        
        //Creates an instance of ofstream, and opens example.txt
        ofstream a_file ( "G:\\ccc\\example.txt" );
        // Outputs to example.txt through a_file
        cout<<"type some numbers :";
        cin>> x ;
        a_file<<"number :" <<x ;
        cout<<"type some alphabet :";
        cin>> y ;
        a_file<<"alphabet: "<<y ;
        
        cin.ignore();
         while (true) {
            cin >> op;
            if(strcmp(op, "open")== 0){
                          ofstream a_file ( "g:\\ccc\\example.txt", ios::app ); break;
                          }
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
        
        // Close the file stream explicitly
        
        a_file.close();
        
        //Opens for reading the file
        ifstream b_file ( "g:\\ccc\\example.txt" );
        //Reads one string from the file
        b_file>> str;
        //Should output 'this'
        cout<< str <<"\n";
        
        system("pause");
    }
    
    
     
    Last edited by a moderator: Mar 13, 2009
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You are linking to your locale image file. Upload in sites like imageshack and then use that link to display it
     
  13. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Opps!:embarasse sorry about that, i don't where that things come from and i did not it notice.:disappoin
     
  14. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    This is where you do it correctly:
    Code:
        FILE *fp;
        fp=fopen("g:\\ccc\\example.txt", "r");
    
    You can then use fp in functions like fseek and fread to manipulate example.txt, which due to the 2nd parameter "r" has been opened in read only mode.

    But I'm still not sure what you are intending. Your code also uses ifstream and ofstream on the same file and these on their own should be sufficient for the operations you want to perform. So my question is: what exactly do you think that fopen is adding to the mix? Do you think [i/o]fstream aren't opening the files properly and need extra help? (Doesn't work that way.) If you want to use fopen to open the file then you need to use fopen-related functions to access the file; you can't fopen a file then modify it with [i/o]fstream.

    So here's my suggestion. Remove the FILE and fopen stuff altogether on the grounds that it's completely unnecessary.

    What is your while loop doing? a_file is already open at this stage. The flow here is: if the user types open, open the already open file, break the loop, then close the file. This I don't get at all. What are you trying to achieve here?

    One step at a time. If you close the file and end the program after "a_file<<"alphabet: "<<y ;", does the file contain what you expect? Open the file with your favourite text editor to find out.
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Removed from the OP
     
  16. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    You mean that break; closed the file after open it?

    Actually what am i want to achieve is a simple things, but i don't know how to make it.
    1. It will ask the user type numbers then some alphabets.
    2. The things the user typed will save the file named example.txt that i mentioned.
    3. I want it open when the user typed open and pressed enter.
     
  17. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, not the break, that doesn't affect files at all. I was referring to this code:
    Code:
        // Close the file stream explicitly
        
        a_file.close();
    
     
  18. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > 3. I want it open when the user typed open and pressed enter.

    You need to open a file before you can do anything with it. You cannot write data to a file that is not open (for writing).
    So if the 1.2.3 above indicates the sequence of events the program should follow, then you need to keep the entered data in memory until the user types "open" to get the file open.
    Only when the file is open can you enter data. So you need to move the a_file<<"number :" <<x ;
    and the alphabets one to AFTER you've opened the file.

    Because contrary to your design what you've actually got is:
    1. open the file
    2. get some numbers and write them to the file
    3. get some alphabets and write them to the file
    4. prompt for "open"
    5. ...and now your code gets really confused (and I think you do too) because somehow you've forgotten that you've already opened the file and written the data to it.
    The loop loops until the user enters "open".
    If they don't then it keeps looping.
    If they do it opens the file (WHICH IS ALREADY OPEN), and the break ends the loop.
    After exiting the loop you then close the file (so I'm uncertain of the point of the loop - it seems to achieve absolutely nothing)
     
    Last edited: Mar 14, 2009
  19. hei

    hei New Member

    Joined:
    Mar 9, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Oh! I finally understand it, thanks everyone
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice