Struct size causes freezing of program?

This is a discussion on Struct size causes freezing of program? within the C Programming forums, part of the General Programming Boards category; Dear all, I ran into the following weird problem while trying to write a simple C program using structures. The ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Struct size causes freezing of program?

    Dear all,

    I ran into the following weird problem while trying to write a simple C program using structures. The source is as follows

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct filerectype { 
      char myarray [9];
    };
    
    int main() 
    {
      FILE *fp;  
      fp = fopen("file.txt", "rt");
      if (!fp) {
        printf("fopen error\n");
        exit(-1);
      }
    
      char* line;
      struct filerectype filerec;
      while( !feof(fp) ) {
        if ( fgets (line, 100, fp) != NULL)
          puts (line);
      }
      fclose(fp);
    
      exit(0);
    } //main()
    The source compiles without problems but when I try to run the .exe file, it freezes and pops the error window "blah.exe has stopped working"

    Now the weird part is, that in case I play around with the fields of struct filerectype, then the .exe runs without any problems and outputs the content of file.txt. But this is all nondeterministic, i.e. when I started to write this comment, then I wanted to illustrate with an example that it works correctly with myarray size 8 and freezes with myarray size 9, but since then I played arround with the struct fields a bit again and now it freezes with myarray size 3

    I am actually using Win7 and Bloodshed 4.9.9.2 if that has any relevance, but I rather suspect that it is some coding problem which I was not able to figure out so far. The goal would be actually to write a simple program using dynamic struct chains built from the data read from the file, and I have the whole concept designed on skeleton level, but this problem is now completely blocking the coding

    Thank you for any help in advance

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    If you have text-files, you cannot read/write structs directly.
    fgets/fputs cannot handle structs, you should use fread/fwrite.

  3. #3
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    char* line=(char*)malloc(100);

    replace this line at line number 17. and replace rt with r

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by BillyTKid View Post
    If you have text-files, you cannot read/write structs directly.
    fgets/fputs cannot handle structs, you should use fread/fwrite.
    I did not yet use the struct variable at all, that's what made the issue weird. The fgets/fputs calls were not used on the struct but on a char pointer

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by sagar474 View Post
    char* line=(char*)malloc(100);

    replace this line at line number 17. and replace rt with r
    Thanks man, this seems to work. I suppose the missing of this allocation might have messed up the memory handling
    Using r instead of rt is not completely clear though, because file.txt is a text file

  6. #6
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    I did not yet use the struct variable at all, that's what made the issue weird. The fgets/fputs calls were not used on the struct but on a char pointer
    yes i noticed that.

  7. #7
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    i never used rt
    i use r for text file and rb for bindery file. but rt is working fine i think those both are equivalent.


    can you show me how you are using structs.
    Last edited by sagar474; 11-05-2011 at 01:36 PM.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    A 't' in fopen isn't C standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct field containing size of struct
    By DL1 in forum C Programming
    Replies: 7
    Last Post: 10-10-2011, 11:37 PM
  2. Program freezing while running a function, please help
    By kuzadaman in forum C Programming
    Replies: 6
    Last Post: 07-25-2011, 07:05 PM
  3. program freezing
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-10-2007, 06:38 PM
  4. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 04:20 PM
  5. Program freezing..
    By xlnk in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-17-2003, 09:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21