c - error using struct in reading from a text file -


i beginner in c programming , trying use struct store related variables , later use them in main program. however, when run same program without using struct, running fine.

the code presented below, doesn't show compilation errors 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 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 following line: aaaa\tbbbb\tcccc\tdddd\n

each instance of struct test 40k so

struct test parts[100000]; 

is trying allocate 4gb on stack. fail, leading seg fault.

you should try reduce size of each struct test instance, give parts fewer elements , move off stack. can last point giving static storage duration

static struct test parts[smaller_value]; 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -