reading data from file and storing into array of structs

This is a discussion on reading data from file and storing into array of structs within the C Programming forums, part of the General Programming Boards category; hi im trying to read the movie title, rating, date and length from a file and tryingto store it into ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    reading data from file and storing into array of structs

    hi im trying to read the movie title, rating, date and length from a file and tryingto store it into an array of structs here is how the data file is
    Code:
    Meet the Parents,PG,03/06/2001,108
    Meet the Fockers,PG-13,04/19/2005,116
    Fantastic Four,PG-13,07/10/2005,99
    Harry Potter: Prisoner of Azkaban,Not-Rated,11/23/2004,142
    Hitch,PG-13,07/02/2005,103
    and the code ive written so far
    Code:
    #define SIZE 10
    struct{
    char title[30];
    char rating[40];
    char date[40];
    int length;
    } movies[SIZE];
    int main()
    {
        
        int i;
        char           name[100];  /* name of the file to use  */
        FILE           *in_file;    /* file for input */
        printf("Name? ");
        fgets(name, sizeof(name), stdin);
        name[strlen(name)-1] = '\0';
    
        in_file = fopen(name, "r");
        if (in_file == NULL) {
            fprintf(stderr, "Could not open file\n");
            exit(8);
        }
    
              fscanf(in_file, "%[^,]s,%[^,]s,%[^,]s,%d", &movies[1].title, &movies[1].rating,&movies[1].date,&movies[1].length);              
        fclose(in_file);
              printf("Name of Movie:%s - Rating:%s - Date:%s - Length:%d",movies[1].title, movies[1].rating,movies[1].date,movies[1].length); 
        getch();
        return (0);
    }
    i compiled it and this is what is displayed
    Name? input.txt
    Name of Movie:Meet the Parents - Rating:= - Date: - Length:0
    also how would i go about doing it for the nextline as this is a test for only one line
    Last edited by loso44x; 10-01-2005 at 04:54 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    movies[1]
    That should be movies[0]. movies[1] isn't defined.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you have an array
    Code:
    int array[10];
    Then the addressable elements are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. So the last addressable element is 9, which is one less than the array's size. Since you declared the array as
    Code:
    #define SIZE 1
    movies[SIZE];
    then the last, and first, element is 0. 1 doesn't exist.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    Quote Originally Posted by dwks
    If you have an array
    Code:
    int array[10];
    Then the addressable elements are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. So the last addressable element is 9, which is one less than the array's size. Since you declared the array as
    Code:
    #define SIZE 1
    movies[SIZE];
    then the last, and first, element is 0. 1 doesn't exist.
    sry i edited the code what i compiled was size as 10, still having trouble displaying the correct results

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    new updated code
    Code:
    #include <stdio.h>
    #include <stdlib.h>	
    #include <conio.h>
    #define SIZE 4
    struct{
    char title[36];
    char rating[11];
    char date[11]; 
    char length[3];
    } movies[SIZE];
    int main()
    {
        
        int i;
        char           name[100];  /* name of the file to use  */
        FILE           *in_file;    /* file for input */
        printf("Name? ");
        fgets(name, sizeof(name), stdin);
        name[strlen(name)-1] = '\0';
    
        in_file = fopen(name, "r");
        if (in_file == NULL) {
            fprintf(stderr, "Could not open file\n");
            exit(8);
        }
        for (i=0;i<=SIZE;i++)
        {
              fscanf(in_file, "%35[^,],%10[^,],%10[^,],%s",movies[i].title, movies[0].rating,movies[i].date,&movies[i].length);              
              printf("Name of Movie:%s - Rating:%s - Date:%s - Length:%s\n",movies[i].title, movies[i].rating,movies[i].date,movies[i].length);   
              }
        fclose(in_file);
     
                  
        getch();
        return (0);
    }
    OUTPUT:
    Name? input.txt
    Name of Movie:Meet the Fockers - Rating:PG-13 - Date:04/19/2005 - Length:116
    Name of Movie:
    Meet the Parents - Rating: - Date:03/06/2001 - Length:108
    Name of Movie:
    Fantastic Four - Rating: - Date:07/10/2005 - Length:99
    Name of Movie:
    Harry Potter: Prisoner of Azkaban - Rating: - Date:11/23/2004 - Length:142
    Name of Movie:
    Hitch - Rating: - Date:07/02/2005 - Length:103
    any1 know why the rating is not being displayed after the first one
    nm got it stupid mistake =)))

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > #include <conio.h>
    This suggests to me that your compiler is very old, as does your use of getch() at the end of the program.

    > char length[3];
    Is too short to hold say
    Hitch,PG-13,07/02/2005,103

    > for (i=0;i<=SIZE;i++)
    For array[n], the subscripts are 0 to n-1, not 0 to n
    So it's for (i=0;i<SIZE;i++)

    > fscanf(in_file, "%35[^,], ..
    You know you can fold long lines like this to make the code more readable.
    Code:
    for (i=0;i<SIZE;i++)
    {
      fscanf ( in_file, "%35[^,],%10[^,],%10[^,],%2s",
               movies[i].title, movies[i].rating,
               movies[i].date, movies[i].length );
      printf ( "Name of Movie:%s - Rating:%s - Date:%s - Length:%s\n",
               movies[i].title, movies[i].rating,
               movies[i].date,movies[i].length );
    }
    You specified sizes for everything else, why not for the final field?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Info From file into Array of structs
    By bigparker in forum C Programming
    Replies: 16
    Last Post: 06-27-2009, 02:21 AM
  2. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  3. Replies: 3
    Last Post: 02-26-2008, 01:12 PM
  4. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  5. Reading strings from a file and storing into an array
    By Rizage in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2002, 03:04 AM

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