0

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

3
  • Side issue: Suggest changing fscanf(int_file,"%[^\t]... to char buf[MAXLINESIZE]; while (fgets(buf, sizeof buf, int_file) != NULL) { ... and then parse buf into parts. Commented Jan 31, 2014 at 21:39
  • Sorry could not understand your suggestion. Will be glad if you kindly clarify. I want to use struct since there can be almost 10 or more variables and their sizes may vary from as small as 5 characters to 1000 characters or more, depending on the input text file. Commented Jan 31, 2014 at 22:17
  • Is the file data 1 line per structure? Commented Feb 1, 2014 at 0:41

2 Answers 2

2

Each instance of struct test is 40k so

struct test parts[100000];

is trying to allocate 4GB on the stack. This will fail, leading to your seg fault.

You should try to reduce the size of each struct test instance, give parts fewer elements and move it off the stack. You can do this last point most easily by giving it static storage duration

static struct test parts[SMALLER_VALUE];
Sign up to request clarification or add additional context in comments.

5 Comments

Plus, you would appear to have both an external "parts" (size 40kB) and a parts array of size 4GB within main(). Since everything is within main(), it is presumably ignoring the external small parts.
I didnt know about the capacity of an instance of struct and so have randomly used a value. Sorry about it..
What should I actually use if my input file size changes?? I mean, I want to write a general program for running many text files that may have variable sizes and unknown to the program.
Reducing the variable size did indeed solve my problem and %[^\t] did work as well. However, I need to confirm the variable size that varies according to different input files.
Should %[^\t] be %[^\t]s - No. The %[ format specifier ends with ].
0

A single struct takes up 40,000 bytes, and you have 100,000 of these. That comes to 4,000,000,000 bytes, or about 4GB. I'm not surprised you are seg faulting

Please rethink what you are doing. Are you seriously trying to read in 4 strings of 10,000 characters each?

2 Comments

I had used a random number not realizing the capacity of a single struct. What I wanted was to use the highest capacity because my input file size may change and I dont want to re-write my programs again and again. Please suggest how to solve it.
Reducing the size did solve my problem but actually my strings can be very very large or very small (varies according to different input files), so what should be my variable size??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.