Thread: error while using struct in C code to read from file

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    10

    error while using struct in C code to read from file

    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

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try replacing this "parts->string3" with "parts[0].string3" NOTE: parts-> is wrong everywhere you used it.

    Note: That is NOT likely what you need because the 0 likely should be a variable.
    But, its less likely to crash.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    struct test
    {
        char string1[10000];
        char string2[10000];
        char string3[10000];
        char string4[10000];
    }parts;
    
    // ...
    
    struct test parts[100000];
    That's an awful lot of space you're using there.

    "parts" is an array, so why aren't you using it as such? (e.g. "parts[i]")

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    10
    Can solve the present problem by reducing the variable sizes. But I want to generalize my program for a large text file as input. What should be my variable size??

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Look into "dynamic memory allocation". This way, you can request memory as you need it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-04-2012, 07:36 AM
  2. struct, enum and binary file read in
    By csharp100 in forum C Programming
    Replies: 4
    Last Post: 03-29-2012, 05:43 AM
  3. Replies: 6
    Last Post: 02-14-2012, 06:46 AM
  4. read contents of a text file into a struct
    By lemonwaffles in forum C++ Programming
    Replies: 2
    Last Post: 08-03-2009, 02:20 PM
  5. Read struct-like file/print to stdout (again)
    By inakappeh in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 04:33 AM

Tags for this Thread