Reading file into array problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chris237
    New Member
    • Nov 2006
    • 4

    Reading file into array problems

    I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a sample program or tell me where i'm going wrong.

    i use DEV C++
    Written in C

    [code]
    #include<stdio. h>
    #include<stdlib .h>
    #include<string .h>

    char friends_name[20];
    int number_of_frien ds_i;
    int found_i;

    /* Prototype */
    int f_menu();
    void f_recall();
    void f_enter_names_a ges();
    void f_display_list( );
    void f_extend_list() ;
    void f_delete_name() ;
    void f_search();
    void f_save();


    typedef struct
    {
    int friends_age_i;
    char friends_name[20];
    }FRIENDS;

    FRIENDS friends_details[20]; /* Stes up 20 friends in an array */


    int main(void)
    {
    int Choice_d = 0;
    printf ("This is a program to store and display a list of names and ages\n");
    printf ("You can enter upto 20 names and ages, add and delete from the list, display the list and search for a name on the list\n\n");

    do
    {
    Choice_d = f_menu ();
    switch (Choice_d)
    {
    case 1: f_recall();
    break;

    case 2: f_enter_names_a ges();
    break;

    case 3: f_display_list( );
    break;

    case 4: f_extend_list() ;
    break;

    case 5: f_delete_name() ;
    break;

    case 6: f_search();
    break;

    case 7: f_save();
    break;

    case 8: printf ("Bye Bye\n\n");
    break;
    }
    }
    while (Choice_d != 8);
    system ("Pause");
    return 0;
    }

    /* *************** *************** *************** *************** ********* */
    int f_menu ()
    {
    int Selected_d = 0;


    do
    {

    printf ("(1) Load saved list\n(2) Enter names & ages\n(3) Display list\n(4) Extend list\n(5) Delete from list\n(6) Search for name\n(7) Save list\n(8) Quit\n\n");

    printf ("Enter a number that corresponds to your choice > ");
    scanf ("%d", &Selected_d) ;
    printf("\n");

    if (( Selected_d < 1 ) || ( Selected_d > 8))
    {
    printf("You have entered an invalid choice. Please try again\n\n\n");
    }
    }
    while (( Selected_d < 1) || ( Selected_d > 8));

    return Selected_d;
    }

    /* *************** *************** *************** *************** *************** * */
    void f_recall()
    {
    FILE *fp;
    int i;

    /* Set up blank array */
    for (i = 0; i < 20; i++ )
    {
    strcpy ("", friends_details[i].friends_name);
    friends_details[i].friends_age_i = 0;
    }
    number_of_frien ds_i = 0;

    if ((fp = fopen ("friends_age_i ", "rb" )) == NULL)
    {
    printf("No list saved to file\n Creating new file\n");
    }
    else
    {
    /* Read existing data from file */
    fread ( &friends_detail s[number_of_frien ds_i], sizeof (FRIENDS), 1, fp);

    while ( !(feof (fp)))
    {
    number_of_frien ds_i ++;
    fread ( &friends_detail s[number_of_frien ds_i], sizeof (FRIENDS), 1, fp);
    }
    printf("There are %d friends already stored\n", number_of_frien ds_i);
    fclose(fp);
    }
    }


    /* *************** *************** *************** *************** *************** * */
    void f_enter_names_a ges()
    {
    int count_i = 0;
    printf("Enter Names and Ages of up to 20 friends\n");
    printf("Enter number of friends you wish to enter > ");
    scanf("%d", &number_of_frie nds_i);
    fflush(stdin);
    printf("\n");


    do
    {
    printf("Enter friends name > ");
    scanf("%s", &friends_detail s[count_i].friends_name);
    printf("enter %s's age > ", friends_details[count_i].friends_name);
    scanf("%d", &friends_detail s[count_i].friends_age_i );
    fflush(stdin);
    count_i ++;
    } while ((count_i <= 19) && (count_i < number_of_frien ds_i));
    printf("\n\n");

    }


    /* *************** *************** *************** *************** *************** * */
    void f_display_list( ) /* Display list in alphabetical order */
    {
    int count_i = 0;
    int i;
    int sorted_i; /* Flag that tells if array is already sorted */

    FRIENDS temp;

    if(number_of_fr iends_i < 1)
    {
    printf("List empty\n\n");
    }
    else
    {

    do /* Sort into alphabetical order */
    {
    sorted_i = 0;

    for ( i = 0; i < number_of_frien ds_i - 1; i++)
    {
    if(strcmp (friends_detail s[i].friends_name,f riends_details[i + 1].friends_name) > 0 )
    {
    temp = friends_details[i];
    friends_details[i] = friends_details[i + 1];
    friends_details[i + 1] = temp;
    sorted_i = 1;
    }
    }
    }while (sorted_i == 1);

    printf("Friend\ n"); /* Display sorted list */
    do
    {
    printf("%d: %s age %d\n", count_i + 1, friends_details[count_i].friends_name, friends_details[count_i].friends_age_i );
    count_i ++;
    }
    while (count_i < number_of_frien ds_i);
    printf("\n\n");
    }
    }

    /* *************** *************** *************** *************** *************** * */
    void f_extend_list() /* Add a friend to the list */
    {
    int i = number_of_frien ds_i;

    if (number_of_frie nds_i < 19) /* Max 20 names on list. Can't add another if list is full */
    {
    printf("Enter friends name > ");
    scanf("%s", &friends_detail s[i].friends_name);
    printf("enter %s's age > ", friends_details[i].friends_name);
    scanf("%d", &friends_detail s[i].friends_age_i );
    fflush(stdin);

    printf("\n\n");
    number_of_frien ds_i = number_of_frien ds_i + 1; /* List increased by 1 */
    }

    else
    {
    printf("Sorry, you can't enter any more to the list");
    }
    }

    /* *************** *************** *************** *************** *************** * */
    void f_delete_name() /* Remove name from list */
    {
    char search_name [20];
    int i;


    printf("Enter the name of the friend you want to delete > ");
    scanf("%s", &search_name );
    fflush(0);
    printf("\n");


    found_i = 0;
    i = 0;

    while (( i < number_of_frien ds_i ) && ( found_i == 0))
    {

    if (strcmp (friends_detail s[i].friends_name, search_name) == 0)
    {
    found_i = 1;
    }
    i++;
    }

    if (found_i == 1)
    { /* Copy last entry on list onto name to be deleted. Reduce list by 1 */
    i --;
    printf("%s age %d has been deleted\n\n", friends_details[i].friends_name, friends_details[i].friends_age_i) ;
    strcpy(friends_ details[i].friends_name, friends_details[number_of_frien ds_i - 1].friends_name);
    friends_details[i].friends_age_i = friends_details[number_of_frien ds_i - 1].friends_age_i;
    number_of_frien ds_i = number_of_frien ds_i - 1;

    }
    else
    {
    printf("name not found\n\n");
    }
    }

    /* *************** *************** *************** *************** *************** * */
    void f_search() /* Find name on list */
    {
    char search_name [20];
    int i;

    printf("Enter the name of the friend you want to find > ");
    scanf("%s", &search_name );
    fflush(0);
    printf("\n");


    found_i = 0;
    i = 0;

    while (( i < number_of_frien ds_i ) && ( found_i == 0))
    {

    if (strcmp (friends_detail s[i].friends_name, search_name) == 0)
    {
    found_i = 1;
    }
    i++;
    }

    if (found_i == 1)
    {
    i --;
    printf("%s age %d has been found\n\n", friends_details[i].friends_name, friends_details[i].friends_age_i) ;
    }
    else
    {
    printf("name not found\n\n");
    }
    }

    /* *************** *************** *************** *************** *************** * */
    void f_save()
    {
    FILE *fp;
    int i;

    if ((fp = fopen ( "friends_age_i" , "wb" )) == NULL )
    {
    printf("Cannot open file\n");
    return;
    }

    for (i = 0; i < number_of_frien ds_i; i ++)
    {
    fwrite (&friends_detai ls[i], sizeof (FRIENDS), 1, fp);
    }

    fclose (fp);
    }

    [code]

    Thanks
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    That's very comprehensive code (use [/code] to finish your code segment. )

    What is the problem, that is how is this code failing, what unusual behaviour is it demonstrating, or what output is it giving (compared to what you expect).

    It is great that you submit what you have done so far, but could you please explain what is the problem you are experiencing (because it is difficult to wade through code and highlight a problem).

    Please Reply.....

    Comment

    • svsandeep
      New Member
      • Nov 2006
      • 15

      #3
      [QUOTE=chris237]I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a sample program or tell me where i'm going wrong.

      i use DEV C++
      Written in C

      Code:
      
      
      Hi,

      You are using f_enter_names_a ges function to read your frnds age and name. After reading why are you not calling your f_save function which will write the details in to a file. And also what abt number_of_frien ds_i variable why are you not incrementing it when ever you are reading a new friend. Do these two changes i am sure your code will start working atleast some functionality of what you have expected to do. I got more point but not now later....

      regards,
      ShaggY@FtF

      Comment

      • chris237
        New Member
        • Nov 2006
        • 4

        #4
        When i run the program there is a list of options.
        1)Load saved list
        2)Enter names and ages
        3)Display list
        4)Extend list
        5)Delete from list
        6)Save list
        7)Quit

        Options 2-5 work.
        Option 1 crashes the program
        Option 6 i have no way of checking as option 1 won't work

        I think the problem is within
        Code:
         Void f_recall
        (Option 1) function. I don't want to integrate the save function into the
        Code:
         f_enter_names_ages
        as this would remove the option of saving by choice.

        If you could help me on this it would be greatly appreciated.

        Thanks for your reply

        Chris

        Originally posted by DeMan
        That's very comprehensive code (use [/code] to finish your code segment. )

        What is the problem, that is how is this code failing, what unusual behaviour is it demonstrating, or what output is it giving (compared to what you expect).

        It is great that you submit what you have done so far, but could you please explain what is the problem you are experiencing (because it is difficult to wade through code and highlight a problem).

        Please Reply.....

        Comment

        • chris237
          New Member
          • Nov 2006
          • 4

          #5
          Originally posted by DeMan
          That's very comprehensive code (use [/code] to finish your code segment. )

          What is the problem, that is how is this code failing, what unusual behaviour is it demonstrating, or what output is it giving (compared to what you expect).

          It is great that you submit what you have done so far, but could you please explain what is the problem you are experiencing (because it is difficult to wade through code and highlight a problem).

          Please Reply.....
          Do i have to save the list as it is entered or can i have an option to save it at the end if the user wants?

          Please Reply

          Thanks

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by chris237
            Do i have to save the list as it is entered or can i have an option to save it at the end if the user wants?

            Please Reply

            Thanks
            Hi there are 2 things about f_recall that may be of interest to you.
            firstly strcpy works by putting the destination string first and the source string second. You have it the other way around.
            secondly you are miscounting the number of 'friends' stored on file by 1, and you don't have a check to ensure that you are not reading in more than 20 friends.

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              Originally posted by chris237
              Do i have to save the list as it is entered or can i have an option to save it at the end if the user wants?
              That depends on what you mean by save. You store the information in the list (in memory) while you use it. If you mean saving to disk, then there is no harm in waiting for the list to be fully populated before storing it)

              Comment

              Working...