I'm learning to program in C. I've been asked to write a program that, asking the user for the name of a file, calculates its size in bytes. I'm on linux mint cinnamon, for the record. I attach the code and the commands used in the terminal to run the program, thanks in advance!
#include <stdio.h>
int main()
{
FILE* f;
char* nombre;
int cont;
printf("Introduce el nombre del archivo cuyo tamanio quieres conocer: ");
scanf("%s", nombre);
f=fopen(nombre,"r");
cont=0;
while(!feof(f)){
cont=cont+1;
}
fclose(f);
printf("El archivo pesa %d bytes",cont);
return 0;
}
Ok, now I open the linux terminal to create the executable and run it.
~/Escritorio$ gcc -o longFile longFile.c
~/Escritorio$ ./longFile
Introduce el nombre del archivo cuyo nombre quieres conocer: longFile.c
Well, it stays with the cursor blinking and does not return any results. Searching through forums I saw that you have to compile it with
~/Escritorio$ cc longFile.c -o longFile
But then putting back
~/Escritorio$ ./longFile
The same thing that I mentioned before keeps happening to me, which does not return anything.
It was missing to add this inside the while so as not to be in an infinite loop