checking a file if it exists or not before opening it

 #include<stdio.h>

int main()
{
    FILE *ptr;
    int n1,n2;
    ptr=fopen("ab.txt","r");
    if(ptr==NULL)
    {
printf("file doesnot exist");
    }
    else{
        fscanf(ptr,"%d",&n1);
                fscanf(ptr,"%d\n",&n2);
                fclose(ptr);
        printf("n1:%d\n",n1);
                printf("n2:%d",n2);


    }
    return 0;
}

Comments