1

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();
}
rippy
  • 195
  • 3
  • 5
  • 14
  • 1
    check the `errno` or use `GetLastError` – Zang MingJie Feb 27 '13 at 13:43
  • 1) conio.h is a non-standard header. 2) main() should return int 3) MS-dos uses backslashes instead of slashes. 4) diagnostic output should go to stderr, 5) and adding a \n to it will cause it to be visible. – wildplasser Feb 27 '13 at 13:45
  • you need to be more specific on your platform (OS) and compiler. I just verified with Visual Studio on Windows 7, and it also works with '/' (even though that was my first thought also). Are you sure that the file exists and is readable? – Andreas Fester Feb 27 '13 at 13:48
  • @rippy please review my answer. – Grijesh Chauhan Feb 27 '13 at 14:07
  • thanks everyone. i think problem is with my compiler (dosbox). – rippy Feb 27 '13 at 14:31

6 Answers6

5

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");
4

correct the path, it should be "D:\\abc.txt"

rahul.deshmukhpatil
  • 977
  • 1
  • 16
  • 31
3

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.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • The do `#include ` and when `fp == NULL` check the value in `errno`. See [here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms737828(v=vs.85).aspx) – bash.d Feb 27 '13 at 13:54
2
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!

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
2

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.

ishaunak
  • 31
  • 1
  • I tried this, but giving just "abc.txt" doesn't work when I placed the file at C:\\TC\\BIN location inside DOS. I am running Turbo C++ v1.01 (tcpp101) inside DOSBOX 0.74-2 (latest revision). – Shubham Deshmukh Apr 06 '19 at 12:55
  • Placing the file inside DOS' root folder, i.e. C:\\ worked for me, if you insist that the file should be detected based on only its name and without giving it any path, like "abc.txt". However, do note that DOS' root folder is NOT the same as your Windows C drive, but instead the folder DOSBOX recognizes as root. – Shubham Deshmukh Apr 06 '19 at 13:14
1

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

Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
  • i am working on dosbox. I think may be that is the reason why my file opens in the same folder(bin) and not on a different one. – rippy Feb 27 '13 at 14:28
  • it also gives the error No such file or directory. but in cmd using dir command it displays the file(abc.txt). – rippy Feb 27 '13 at 14:30
  • 2
    Yes there may be the file present in D:\ But is the D:\ accessible to DosBOX ? I am no sure about that... So instead of opening "D:\\abc.txt" , open "abc.txt"... The file will be read in same directory as the executable.. But do create a file first , before reading it – Barath Ravikumar Feb 27 '13 at 14:39