Reputation: 195
i created a text file in d: drive named abc. I am unable to open it. Please tell me how to do so.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
clrscr();
fp = fopen("D:/abc.txt","r");
if(fp == NULL)
{
printf("\nCannot open");
getch();
exit(1);
}
fclose(fp);
getch();
}
Upvotes: 1
Views: 16152
Reputation: 31
If you are working with TurboC put that file in the BIN directory of TC. And specify the path as fp = fopen("abc.txt","r"); instead of any other alternate path.
Upvotes: 2
Reputation: 5836
The Next time , try to make the error more specific by using perror() function. Perror() will interpret the error code , this will help you to waste less time , trying to find the type of error.
add this in your code...
if(fp == NULL)
{
perror(fp);
}
On running i got the perror message
No such file or directory. (since i ran the program , and tried to read a file , without creating it first)
See , if this was the same problem , in your case
Upvotes: 1
Reputation: 58291
fp = fopen("D:/abc.txt","r");
should be
fp = fopen("D:\\abc.txt","r");
in use \
in path instead of /
in Windows and extra \
for escape sequence.
EDIT:
As you commented to others answers that fp = fopen("D:\\abc.txt","r");
also not working then check what is name actually. You might given probably wrong name by mistake, check whether you have error like this.
(1) open command prompt
(2) use DIR command to print name of file:
c:\Users\name> D:
D:\> DIR
Volume in drive D is FUN BOX
Volume Serial Number is B48A-3CE7
Directory of d:\
27-02-2013 19:23 0 abc.txt.txt
26-02-2013 22:05 <DIR> BOLLYWOOD MOVIES
27-02-2013 19:31 0 x
2 File(s) 0 bytes
1 Dir(s) 11,138,654,208 bytes free
file name is abc.txt.txt
but when you see this in folder extension doesn't appears and file name looks abc.txt
I am Linux user and I normally do this mistake in Windows. That's why. May be it help you!
Upvotes: 2
Reputation: 13217
You file-path looks a little bit strange. Change it to
fp = fopen("D:\\abc.txt","r");
This might work.
Apart from that, include <errno.h>
and check for it, if it has failed.
Upvotes: 3
Reputation:
You have a typo, try
fp = fopen("D:\\abc.txt","r");
instead.
Or if the file is in the same folder as the program:
fp = fopen("abc.txt","r");
Upvotes: 5