Why cant I open a file by its explicit path?
Submitted by: AdministratorWhy can't I open a file by its explicit path? The call
fopen("c:newdirfile.dat", "r")
is failing.
The file you actually requested--with the characters n and f in its name--probably doesn't exist, and isn't what you thought you were trying to open.
In character constants and string literals, the backslash is an escape character, giving special meaning to the character following it. In order for literal backslashes in a pathname to be passed through to fopen (or any other function) correctly, they have to be doubled, so that the first backslash in each pair quotes the second one:
fopen("c:newdirfile.dat", "r")
Alternatively, under MS-DOS, it turns out that forward slashes are also accepted as directory separators, so you could use
fopen("c:/newdir/file.dat", "r")
(Note, by the way, that header file names mentioned in preprocessor #include directives are not string literals, so you may not have to worry about backslashes there.)
Submitted by: Administrator
fopen("c:newdirfile.dat", "r")
is failing.
The file you actually requested--with the characters n and f in its name--probably doesn't exist, and isn't what you thought you were trying to open.
In character constants and string literals, the backslash is an escape character, giving special meaning to the character following it. In order for literal backslashes in a pathname to be passed through to fopen (or any other function) correctly, they have to be doubled, so that the first backslash in each pair quotes the second one:
fopen("c:newdirfile.dat", "r")
Alternatively, under MS-DOS, it turns out that forward slashes are also accepted as directory separators, so you could use
fopen("c:/newdir/file.dat", "r")
(Note, by the way, that header file names mentioned in preprocessor #include directives are not string literals, so you may not have to worry about backslashes there.)
Submitted by: Administrator
Read Online C Programming Job Interview Questions And Answers
Top C Programming Questions
☺ | Why does this code crash? |
☺ | What is wrong with this declaration? |
☺ | How can I read/write structures from/to data files? |
☺ | What does static variable mean in c? |
☺ | How can I increase the allowable number of simultaneously open files? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |