int main() { int sum =0; FILE * ptr; ptr = fopen("d:\\students. "," "); if (ptr ==NULL){ ("file does not exist!!"); exit(0); }
#include <stdio.h>
#include <stdlib.h>
int main() {
int sum = 0;
FILE *ptr;
ptr = fopen("d:\\students.", " ");
if (ptr == NULL) {
printf("file does not exist!!");
exit(0);
}
}
Explanation:
1. Include necessary libraries:
- #include <stdio.h>
for standard input-output functions.
- #include <stdlib.h>
for standard library functions.
Declare the
main()
function of return typeint
.Declare an integer variable
sum
and initialize it to0
.Declare a file pointer variable
ptr
.Use
fopen()
to open a file named"d:\\students."
in the current directory with mode" "
. The mode used is empty, which is invalid. It might have been intended to specify a mode like"r"
for reading or"w"
for writing. There is an extra space after the file name"d:\\students."
.Check if the file pointer
ptr
isNULL
, indicating that the file opening failed.If the file pointer is
NULL
, print a message "file does not exist!!".Exit the program using
exit(0)
to terminate the program execution.