Open file c as safe as possible

I would write a program as follows

int main ()
{
      FILE *fp = fopen("test.txt", "r")

      if (fp == NULL)
      {
           printf("Sorry, file doesn't exist.");
           return 0;     
      }

   return 0;
} 

Is there any other check that I will need to check before or after opening the file?

What to do if opening a file can damage the system (virus)? Is there a check for this?

What if the file is not a .txt file, the user simply renamed the mp3 file to a txt file?

+4
source share
3 answers

No other verification is required.

I always also do calling practice fclose, however, when it is done with the * file.

+3
source

r . , . , DOS/Windows \r\n , UNIX- \n, MAC \r .

-, , - , rb.

, , , struct , ..

+2

, , , . , .

+1

All Articles