Thread: Load a file into a structure and output it to a screen

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    Load a file into a structure and output it to a screen

    I am trying to find a way to load my file into a series of structures and then display the results out onto the screen but i seem to hit the infinite loop as the files isnt actually being read, any thoughts on how to get my file to load to it?

    Code:
    struct area_type
    {
       int num_of_area;
       char address_of_area[200];
       char firstname[100];
       char surname[100];
       int num_of_location;
       float price;
    } area_type;
    
    
    int main()
    {
       struct area_type * book_type;
       int numCounter;
    
       char filepath[256] ="";
       printf("Please enter the full path of the file to be loaded: %s", filepath);
       scanf("%s", filepath);
       getchar();
    
       FILE * fptr;
       if (((fptr = fopen(filepath,"r"))))
         {
           printf("The file has opened sucessfully for reading\n");
           while (fread(&area_type, sizeof area_type, 1, fptr) == 1)
           book_type = (struct area_type*) malloc(sizeof(struct * area_type) *numCounter);
           int i = 0;
             {
                for (i = 0; i < numCounter; i++)
                  {
                   printf("Area:%d, Address:%s, First name:%s, Surname:%s, Loaction:%d, Price:%f\n",
                   book_type[i].num_of_properties, book_type[i].address_of_property,
                   book_type[i].estate_firstname, book_type[i].estate_surname,
                   book_type[i].num_of_bedrooms, book_type[i].price_property);
                  }
             }
         }

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    You got some serious messing with names here.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    @OP
    Kindly put down your compiler and step away from the keyboard!

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    lol fatal error i presume, yahr i tried to run it and i get a seg fault

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    You probably want to read about typedef facility and how to use dynamic memory with structures properly, and try lesser examples to get used to them. Also I'd suggest reading a sticky thread about development process.

    A couple further notes:
    char filepath[256] ="";
    printf("Please enter the full path of the file to be loaded: %s", filepath);
    Initializing string to '\0' and %s", filepath here really make no sense.

    >scanf("%s", filepath);
    Better use fgets().
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by greg677 View Post
    lol fatal error i presume, yahr i tried to run it and i get a seg fault
    Don't write more than a few lines at a time without test compiling, even if the code doesn't "do anything" and you maybe have to shim something in temporarily to take care of some loose detail. Otherwise at you will tend to wander more and more off the deep end.

    It would make more sense to make the if (fopen) condition like this:
    Code:
    if (!(fptr = fopen(filepath,"r")))  {
         puts("fopen failed");
         return -1;
    }
    so that you don't have to enclose everything after that in one huge block.

    - numCounter is never initialized to any value
    - your while(fread) loop will just overwrite the same structure everytime, which incidentally that "structure" is actually just a pointer -- either allocate it memory or make it a real struct instance (this is the probable cause of the segfault).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    if (((fptr = fopen(filepath,"r"))))
    That line needs a few more parentheses; you don't want to be struck down by the gods of operator precedence in that statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me as fast as possible
    By Xbox999 in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 06:53 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM