How to find a string in a struct..

This is a discussion on How to find a string in a struct.. within the C Programming forums, part of the General Programming Boards category; I'm making a database for fx a school soccerteam . When I have loaded all the file info. into the ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    How to find a string in a struct..

    I'm making a database for fx a school soccerteam .
    When I have loaded all the file info. into the struct, I need to make it possible to seek for players..
    If i make a routine like: please input the players you want fx sorted after shirtnumbers..input: 10.
    Then the program should printf out the info about shirt no. 10.
    fx:
    10, David Madsen, Birthday: 12.02.1978 etc.
    Can anybody help me with this. that would mean alot to me..tx..
    I trying something with read(handle,...,...), but it don't seems to work prober..Also strstr(..) has been around my desktop..
    Tx in advance
    !G!
    ------------my kode so far------------cut..

    #define FILENAME "datafile.txt"
    #define ENTER_PLAYERSx
    #define GET_PLAYERS

    #ifdef ENTER_PLAYERS
    /* Record of data */
    struct data
    {
    char fname[30]; /* First name */
    char lname[30]; /* Last name */
    char shirtnr[3]; /* Shirtnumber */
    int birthday[12];
    }rec[30];

    /*----Input Players to the Database.----*/
    void input_players(void)
    {
    FILE *input;
    int entries = 0;
    int loop;

    printf("How many Players will you type in?: ");
    scanf("%d", &entries);
    fflush(stdin);

    for(loop=0; loop<entries;++loop)
    {
    printf("\nEntry: %d\n", loop);
    printf("Enter the players Shirtnumber: ");
    scanf("%s", rec[loop].shirtnr);
    fflush(stdin);

    printf("\nEnter the player's first and last namename, \n");
    printf("separated by a space: ");
    scanf("%s %s", rec[loop].fname, rec[loop].lname);
    fflush(stdin);

    printf("Enter the players birthday <ex. 15.03.1974>: ");
    scanf("%s", rec[loop].birthday);
    fflush(stdin);
    }
    /* White the data to a file */
    input = fopen (FILENAME, "a+");
    for(loop=0; loop<entries;++loop)
    {
    fprintf(input, "%2s %2s, Shirtnumber: %2s, Birthday: %2s\n",
    rec[loop].fname,
    rec[loop].lname,
    rec[loop].shirtnr,
    rec[loop].birthday);
    }
    /* display the information */
    for(loop=0; loop<entries;++loop)
    {
    printf("\n");
    printf("%2s %2s Shirtnumber: %2s Birthday: %2s",
    /* loop + 1,*/
    rec[loop].fname,
    rec[loop].lname,
    rec[loop].shirtnr,
    rec[loop].birthday);
    }
    }
    #endif
    void input_players(void);
    int main()
    {
    /* Buffer to hold one single line from the file, the size is declared as
    BUFSIZE, because one line of the file will rarely if even go above 256 ch. */
    char buffer[BUFSIZ];
    FILE *output;

    struct indata
    {
    char name[BUFSIZ]; /* name */
    int shirtnr; /* Shirtnumber */
    int birthday[12];
    }record;

    /*input_players();*/
    if ((output = fopen(FILENAME, "r")) !=NULL)
    {
    while(fgets(buffer,(int)sizeof buffer, output) !=NULL)
    {
    if(sscanf(buffer, "%d,%[^,],%[^,]", &record.shirtnr, &record.name,
    &record.birthday) == 3)

    printf("Shirtnr: %2d, Name: %20s, Birthday: %10s\n", record.shirtnr,
    record.name,
    record.birthday);

    else
    {
    printf("\n\nERROR - Invalid Input\n\n");
    getch();
    return 0;
    }
    }
    }

    getch();
    return 0;
    }
    ----

    The datafile.txt might be as follows:

    10, Gustaf Norgaard, 15.03.1974
    9, Sara Kistrup, 03.06.1977
    8, Mikkel jensen, 25.03.1974
    7, Kenneth Mikkelsen, 12.06.1977
    6, Hans Niel, 06.03.1973
    5, Kasper Hansen, 17.06.1980e.t.c
    Last edited by Gugge; 03-09-2002 at 08:59 AM.
    !G!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >fflush(stdin);
    This is not guaranteed to work on all systems, and worse yet, the result is undefined by the standard. Use while ( getchar() != '\n' ); instead.

    >getch();
    getch is nonstandard, to make your program more portable you can use getchar since you don't need the single keystroke of getch in this case, it's simply to keep the program from continuing before the user can read the messages.

    For your search routine you'll need to iterate through each item in the array and test to see if the input from the user matches. If it does, print the record otherwise print an error saying the item was not found.
    Code:
    /* pseudocode */
    int searchRecords ( char *input )
    {
      int index = 0;
      while ( index != ARRAYSIZE ) {
        if ( strcmp ( rec[index].shirtnr, input ) == 0 ) {
          printRecord ( rec[index] );
          return EXIT_SUCCESS;
        }
      }
      printf ( "Record not found\n" );
      return EXIT_FAILURE;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    while ( index != ARRAYSIZE ) {
    if ( strcmp ( rec[index].shirtnr, input ) == 0 ) {
    printRecord ( rec[index] );

    tx for you answer, but can I ask:
    ARRAYSIZE = "how do one measure struct size"?
    printfRecord = is that a func I should write, or should it be printf.?
    !G!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    struct data
    {
    char fname[30]; /* First name */
    char lname[30]; /* Last name */
    char shirtnr[3]; /* Shirtnumber */
    int birthday[12];
    }rec[30];
    rec[30] defines the array of structs but magic numbers are usually frowned on because they make code harder to maintain. I used a constant name ARRAYSIZE which should be defined as
    #define ARRAYSIZE 30

    >printfRecord = is that a func I should write
    I assumed that printRecord would be a function that you write, it saved me the trouble of writing a way to print a record

    >ARRAYSIZE = "how do one measure struct size"?
    Struct size and array size are two different things, the array size is the number of elements in the array of structs and struct size is the size of a single element. But to get the size of a struct, simply say
    sizeof ( struct structName )

    How you write the actual program is up to you, I wrote that pseudocode to be as generic as possible so that it could be used however you needed it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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