C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Point Out Errors (Q.No. 4)
4.
Point out the error in the program?
#include<stdio.h>

/* Assume there is a file called 'file.c' in c:\tc directory. */
int main()
{
    FILE *fp;
    fp=fopen("c:\tc\file.c", "r");    
    if(!fp) 
      printf("Unable to open file.");        

    fclose(fp);
    return 0;
}
No error, No output.
Program crashes at run time.
Output: Unable to open file.
None of above
Answer: Option
Explanation:
The path of file name must be given as "c:\\tc\file.c"
Discussion:
7 comments Page 1 of 1.

Vishal said:   1 decade ago
It crashes at run time. As the fp is NULL and its being passed to fclose.

Pravin said:   1 decade ago
If we write the path as given in explanation then option A will be the ans?

Bhushan said:   10 years ago
The program running successfully. The explanation was given wrong.

Vijay said:   10 years ago
Why should we give path name as c:\\tc\file.c?

Rohan said:   9 years ago
Program running successfully. There is no problem with the syntax of given path and extension but I don't logic of this if (! fp) ;.

Apoorva said:   9 years ago
c:\ tc\file.c is incorrect way of reading because when drive is specified like c,d etc .file path should be c:\\tc\file.c.
So, fp contain 0 value. if(!fp) means complementing fp content .if (condition) if statement falls only for 0.

Here if(!fp) becomes true so "Unable to open file" will be printed.

Shree said:   8 years ago
The Application will crash as mentioned by Visual.

int main()
{
FILE *fp;
fp = fopen("c:\tc\file.c", "r");
if(!fp)
printf("Unable to open file.");
else
fclose(fp);
return 0;
}

Post your comments here:

Your comments will be displayed after verification.