Hi all,
I am very new to C programming and am trying to use struct to store variables from a text file and use them in the main program. I have first tried running the program without using struct and declaring the variables within the main program and it runs fine. But after using struct, it gives no compilation error and a segmentation fault as output. Can you please tell where I am getting wrong?? Also, if the size of file/variable size is unknown can I declare variables as char string[]??
The code is as below:
Code:
#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 inputfile "intact_test.txt" has the following line:
AAAA\tBBBB\tCCCC\tDDDD\n