Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   How to open the output file? (http://www.go4expert.com/forums/output-file-t16479/)

hei 9Mar2009 16:52

How to open the output file?
 
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

xpi0t0s 9Mar2009 18:49

Re: How to open the output file?
 
Code:

ofstream a_file ( "example.txt" );
Try specifying the location.

hei 9Mar2009 21:10

Re: How to open the output file?
 
Quote:

Originally Posted by xpi0t0s (Post 44097)
Code:

ofstream a_file ( "example.txt" );
Try specifying the location.

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?

xpi0t0s 9Mar2009 21:23

Re: How to open the output file?
 
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?

hei 9Mar2009 21:32

Re: How to open the output file?
 
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?

xpi0t0s 9Mar2009 21:37

Re: How to open the output file?
 
How do you know it doesn't open the file?
Which line of code do you think fails?

hei 10Mar2009 08:03

Re: How to open the output file?
 
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


xpi0t0s 10Mar2009 14:00

Re: How to open the output file?
 
Well, have you RTFM on fopen? Specifically have you read about fopen's return value?

hei 10Mar2009 19:24

Re: How to open the output file?
 
Quote:

Specifically have you read about fopen's return value?
You mean if the fopen successfully, it will open the file but if wrong it will do nothings?

xpi0t0s 10Mar2009 22:36

Re: How to open the output file?
 
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?


All times are GMT +5.5. The time now is 02:26.