I am a beginner in C programming and trying to use struct to store the related variables and later use them in the main program. However, when I run the same program without using struct, its running fine.
The code is presented below, which doesn't show any compilation errors but no output except segmentation fault.
#include<stdio.h>
struct test
{
char string1[10000];
char string2[10000];
char string3[10000];
char string4[10000];
}parts;
int main()
{
FILE *int_file;
struct test parts[100000];
int_file=fopen("intact_test.txt", "r");
if(int_file == NULL)
{
perror("Error while opening the file.\n");
}
else
{
while(fscanf(int_file,"%[^\t]\t%[^\t]\t%[^\t]\t%[^\n]",parts->string1,parts->string2,parts->string3,parts->string4) == 4)
{
printf ("%s\n",parts->string3);
}
}
fclose(int_file);
return 0;
}
The input file "intact_test.txt" has the following line: AAAA\tBBBB\tCCCC\tDDDD\n
fscanf(int_file,"%[^\t]...tochar buf[MAXLINESIZE]; while (fgets(buf, sizeof buf, int_file) != NULL) { ...and then parsebufintoparts.