Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

file handling in c++

+2 votes
asked Oct 14, 2018 by NaveedAsif (160 points)
sir i have saved my file in D derive and i referenced it in code but after compilation following error has been occured. file could not open. code is given below.

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    char name[100];// used to read name of employee from file
    char sal [100];// used to read name of employee from file
    char dep[100];// used to read name of employee from file
    ifstream inFile;
    char inputFileName[]="D:\naveed/ali.txt";
    inFile.open(inputFileName);
    //checking the file is opened or not
    if(!inFile){
        cout<<"cannot open input file"<<"\t"<<inputFileName;
        exit(1);
    }
    while(!inFile.eof()){
        inFile>>name>>sal>>dep;
        cout<<name<<"\t"<<sal<<"\t"<<dep;
    }
    inFile.close();
}

1 Answer

0 votes
answered Jun 27, 2025 by Jerry Jeremiah (2,040 points)

You cannot use   D:\naveed/ali.txt    as a filename.  In a string   \n   is a linefeed.  Any time you use a   \   in a string you need to escape it with another backslash.  So the filename should be  D:\\naveed\\ali.txt

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...