How to open a file by fopen or other open file command?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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");
}


After the user type in the number and alphabets, if the user type open and pressed enter, i want it open the example.txt(the output file. Its location is:
G:\ccc .
Last edited on
i haven't learnt enough to help you with your problem but i did notice that on line 32:
if(strcmp(op, "open")== 0) break;

is it better to have what you have put or to have:
if(!(strcmp(op, "open")) break;

and sorry if this is being ignorant but isnt fp meant to be op? ... line 46/47
Last edited on
Now my main problems is how to open a the file that i mentioned? And yes the fp and fopen is use to open the file, but i don't know why every times i try the program didn't open it and it also didn't gave me any error message.
Last edited on
Topic archived. No new replies allowed.