can't find file in c++ file handling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can't find file in c++ file handling

i was creating a program in turbo c but one part of my code is not running properly can you help me to solve this problem? code: #include<iostream.h> #include<conio.h> #include<fstream.h> void main() { clrscr(); ifstream account; char str1[50]; account.open("C:\Users\abhis\OneDrive\Documents\C practice\input.txt"); if(!account) { cout<<"error"; } else { while(!account.eof()) { account.getline(str1,50); cout<<"\n"<<str1; } } account.close(); getch(); } the program is not giving the info from file it's just showing the text "error" pls check and tell me is there a logical error or anything else

25th Dec 2021, 7:59 AM
Abhishek Shelar
Abhishek Shelar - avatar
2 Answers
+ 2
The part of the code that prints "error" is the part that checks whether the <account> was valid. if( !account ) { // prints "error" } You must make sure the file was there, and your user account has the access rights needed to access the file and the directory where the file is.
25th Dec 2021, 8:28 AM
Ipang
+ 2
The file path string contains backslashes ('\') which get interpreted as escape characters for metacharacters. You have to use double backslashes ("\\") to tell the interpreter that the backslashes are to be used literally as '\'.
26th Dec 2021, 7:14 AM
Brian
Brian - avatar