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?