You
openfilechk
function does not change fp to indicate the closed status. As a result, if it opened sucessfully, it always leaves it indicating the file block you opened.
#include<stdio.h>
#include "file.h"
void openfilechk()
{
char ch;
fp = fopen("abc.txt","r");
while( ( ch = fgetc(fp) ) != EOF )
{
printf("%c",ch);
}
printf("file readed \n");
if (fclose(fp) == 0)
{
fp = NULL;
printf("\nFILE SUCCESSFULLY CLOSED\n");
}
}
int main()
{
fp = NULL;
openfilechk();
if (fp == NULL)
{
printf("\nFILE SUCCESSFULLY CLOSED\n");
}
return 0;
}
BTW: It's a style thing, but I wouldn't declare variables in a ".h" file - constants, and structure definitions only.
I'd also return
fp
from the
openfilechk
method and store it in a local variable rather than using a global anyway - that way you can use it for two different files in the future. In the case of a function defined to check if it was possible to open teh file, I'd return a
bool
rather than a FILE* anyway - it makes your code a lot more readable.