I have a simple file that has a list of 100 file names and their corresponding sizes like this:
file1.txt, 4000
file2.txt, 5000
etc.. How do I read the file line by line and then store the list of filenames into a char array and then the list of sizes into an int array? I am trying to use sscanf like this but this is not working. I am getting a seg fault:
main(){
char line[30];
char names[100][20];
int sizes[100];
FILE *fp;
fp = fopen("filelist.txt", "rt");
if(fp == NULL){
printf("Cannot open filelist.txt\n");
return;
}
while(fgets(line, sizeof(line), fp) != NULL){
sscanf(line, "%s, %d", names[i][0], sizes[i]);
printf("%d", sizes[i]);
i++;
}
}