#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char name[100];
long size;
char mode[8];
char uid[8];
char ouid[8];
char size1[12];
char time[12];
char checksum[8];
char type[1];
char linkname[100];
}HEADER;
int main(int argc, char **argv)
{
for(int i = 1; i <= argc; i++) argv[0,1,2] are program name, c, tar output file
{
HEADER header;
strcpy(header.name, argv[i]); // saving the name of the file
FILE *fin = fopen(argv[i], "rb");
if(fin == NULL)
{
fprintf(stderr, "File not found.\n");
exit(1);
}
fseek(fin, 0L, SEEK_END);
header.size = ftell(fin);
printf("%s is %ld bytes long\n", header.name, header.size);
fseek(fin, 0L, SEEK_SET);
fclose(fin);
}
return 0;
}
I want to store the number of bytes and the name of the files provided as command line arguments, but I'm getting a segmentation fault. I ran a debugger and strcpy is the problem, name has fixed size so I don't need to allocate memory. Can you help me?