vendredi 21 septembre 2018

File is not opening even though it exists

In the following program, it writes data successfully to student.txt file but when I open it and print line by line of the file, it always shows file not found. Could anyone help me with that? Thanks in advance.

    ifstream Myfile;


    Myfile.open("student.txt");


      if(!Myfile){
        cout<<"Sorry file can't be opened" <<endl;

        exit(1);
    }


        else
        { 

            // Use loop and read the names and ids from the file and display them 
        string line;
        while (getline(Myfile, line)){

        cout<<line<<endl;
    }

            // Close the file 
      Myfile.close();    

Aucun commentaire:

Enregistrer un commentaire