Thread: Searching for a String within a file

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Question Searching for a String within a file

    I'm making a program that stores peoples info in a file. Each Person's info is a New line ex:

    John <info>
    doug <info>

    I need to be able to search for the person's name to pull the info from the file into a struct. I'm not quite sure how to compare the strings. here's a snipit of the code:

    FILE * input_file;

    struct
    {
    char name[20];
    int age; // theirs more, but not important.
    } Info_Struct;

    int main(void)
    {
    char name[20];
    char file_name[20];

    printf("What is your name: ");
    scanf("%s",name);

    input_file = fopen("d:\\test.dat","r");

    fscanf("%s",file_name);

    //here's where I need to compare the names to see if the
    //first name in the file is correct, or if I need to go to the
    // nest line. I'd like to use an if statement, but if theirs
    //something better, I think I could live.

    }


    I'm using DevC++
    Last edited by TidusBlue; 02-11-2002 at 10:29 AM.
    Don't let the Man get you Down

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You'll want something along the lines of this:
    Code:
    int main ( void )
    {
      char name[80], buffer[80];
      FILE *IN = fopen ( "filename" );
      if ( IN != NULL ) {
        printf ( "Enter your name: " );
        fgets ( name, sizeof name, stdin );
        while ( fscanf ( in, "%s", buffer ) != EOF ) {
          if ( strcmp ( buffer, name ) == 0 )
            /* Put it in the struct */
        }
      }
      else
        printf ( "Error opening file!" );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    This is the code I put in, but it's just putting garbage in the variables....


    Characters_Struct Player;

    char name[20],buffer[20];

    Character_File = fopen("d:\\character.dat","r+");

    if (Character_File != NULL)
    {
    printf("Enter your name: ");
    fgets (name, sizeof name, stdin);
    while (fscanf(Character_File,"%s",&buffer) != EOF)
    {
    if (strcmp(buffer,name) == 0)
    {
    fscanf(Character_File,"%s", &Player.name);
    fscanf(Character_File,"%d", &Player.weapon);
    fscanf(Character_File,"%d", &Player.armor);
    fscanf(Character_File,"%d", &Player.strength);
    fscanf(Character_File,"%d", &Player.def);
    fscanf(Character_File,"%d", &Player.gold);
    fscanf(Character_File,"%d", &Player.level);
    fscanf(Character_File,"%d", &Player.health);
    fscanf(Character_File,"%d", &Player.max_health);
    }
    }
    }
    else
    printf ("Error opening file");
    Don't let the Man get you Down

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    use fread() for reading structs from the disk.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >fgets (name, sizeof name, stdin);

    add this after the fgets().

    name[strlen(name)-1] = NULL;

    You need to remove the newline character before the comparison (or you could use strncmp() instead of strcmp().

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    I got it to work by using fscanf instead of fgets....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM