Same things as mentioned here.
- Post in English to allow more people to respond to your post
- gets is deprecated. Use fgets instead.
- FILENAME_MAX (in stdio.h) defines the maximum length for file names. You can use this macro.
- Add files to your project so you can open them from your code.
Some further notes specific to this code
- File size is calculated wrong. The reason is that with getc(p) you read the EOF (end of file) symbol too, which, unless I'm mistaken, is not part of the file. Your program for an empty file for instance returns size 1, instead of 0. You can use ftell to calculate file size.
- You use exit(1) but instead you could just return (and make your main have a return type int).
- fgets reads the new line character too. It's easier to use scanf instead.
Here's the fixed code
#include <stdio.h>
int main()
{
char fileName[FILENAME_MAX] = {};
printf("Enter a file name: ");
scanf("%s", fileName);
FILE * file = fopen(fileName, "r");
if (!file)
{
printf("Error! Cannot open file: %s!\n", fileName);
return 1;
}
fseek(file, 0, SEEK_END);
long int size = ftell(file);
fclose(file);
printf("Size of %s: %ld\n", fileName, size);
return 0;
}