#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0

    Post how to search data from the text file


    Dear programmers, i would like to know on how to read data from a text file (data.txt). The text file is constructed as following,

    College
    Mandy

    University
    Richard

    College
    John

    When user type-in "College", the program should only display the student name from college and count how many records.

    Please choose: College
    Mandy
    John
    2 records found

    Please help me to solve my problem, thank you in advance.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    2
    Rep Power
    0
    In addition, i'm using C language
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    hmmm, either my bacon is badly burned or that's homework i smell :P here is some pseudo code to get you going:

    read a line from the file
    compare that line to college
    if it matches, then read in the next line and save it into an array of names
    increment the counter for number of names found
    loop to read line
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    12
    Just in case this is homework, here's a solution that is a little more complex than a teacher would expect, and ugly enough to merit a low grade. Works fine though! ;)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int search(const char *file, const char *str, char ***itemv)
    {
      FILE *in;
      char buff[BUFSIZ];
      int  sz;
      int  i;
    
      if ((in = fopen(file, "r")) == NULL)
        return -1;
      sz = 50;
      *itemv = malloc(sz * sizeof *itemv);
      i = 0;
      while (fgets(buff, sizeof buff, in) != NULL) {
        char *newline;
    
        if ((newline = strchr(buff, '\n')) != NULL)
          *newline = '\0';
        if (i == sz) {
          char **save;
    
          sz *= 2;
          if ((save = realloc(*itemv, sz)) == NULL) {
            free(*itemv);
            return -1;
          }
          *itemv = save;
        }
        if (strcmp(buff, str) == 0) {
          if (fgets(buff, sizeof buff, in) == NULL)
            return -1;
          if ((newline = strchr(buff, '\n')) != NULL)
            *newline = '\0';
          if (((*itemv)[i] = malloc(strlen(buff) + 1)) == NULL) {
            while (i >= 0)
              free((*itemv)[i]);
            free(*itemv);
            return -1;
          }
          strcpy((*itemv)[i++], buff);
        }
      }
    
      return i;
    }
    
    int main(void)
    {
      char **it;
      char **itemv;
      char *newline;
      char buff[BUFSIZ];
      int  items;
    
      printf("Enter a string to search for: ");
      fflush(stdout);
      if (fgets(buff, sizeof buff, stdin) == NULL) {
        perror("fgets");
        return EXIT_FAILURE;
      }
      if ((newline = strchr(buff, '\n')) != NULL)
        *newline = '\0';
      itemv = NULL;
      if ((items = search("input.txt", buff, &itemv)) == -1) {
        perror("search");
        return EXIT_FAILURE;
      }
      for (it = itemv; *it != NULL; it++)
        puts(*it);
      printf("%d records found\n", items);
    
      return EXIT_SUCCESS;
    }
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    LOL his teacher is gonna see the pointer to pointer to pointer and be like "WTF!"

IMN logo majestic logo threadwatch logo seochat tools logo