Thread: Searching structs...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Searching structs...

    Can anyone give me a general idea on searching structs in files?

    For example:

    Let's say I have a struct-type "flower".

    PHP Code:
    struct flower{
    char common_name[100];
    char scientific_name[100];
    char color[50];
                        }; 
    Now suppose I want to search a file filled with these structs by "common_name" so that I may obtain the "scientific_name" of that flower. Or perhaps search all structs of a certain "color" in order to print out the "common_name"s of all matching flowers.

    First of all, I do not understand how, if at all, these structs are distinguished at all from simply plain data, since opening a plain-text file of a group of these structs simply shows data, without any "markings" indicating they ARE indeed structs...? Very weird.

    So I am guessing I will have to master "fseek()", "ftell()", et al, in order to "impose" a distinguishability between plain data and structs stored in files, but all the books I have just gleen over this information very quickly.

    Anyway, that's a long-winded explanation of this newbies current difficulties.

    All, advice, examples, and dissertations are welcome, no matter how abstract!

    Thanks
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I have time to make a qucik reply to this. I'm not exactly sure how you would go about applying the program logic because I have some difficulty understanding the purpose of your program however I can point out a few things, maybe one of which would be helpful.

    A structure is a convienient way to group data in the form of a record. So there is nothing wrong with this code of yours:
    Code:
    struct flower
    {
      char common_name[100];
      char scientific_name[100];
      char color[50];
    };
    But what you may also want to do is create a table of names:
    char common_name[][50] = { "tulup","rose","blaa blaa"};
    Now ofcourse your table would be much nicer.

    Anyway a table can serve as a reference. If you had one or two pices of the record you could reference a table of names and identify say the common name or else the equivalent scientific name. Notice I used the common name for a reason!

    Note: names can follow an alphabetical ordering such that: common_name[10] could be determined to be a "rose" for example, or common_name[20] could be a "tulup". Anyway you would be able to apply a number and get a known result. Get it? Think about this.
    I'm not sure if that is the intention of your program. As far as reading records into the structure, this can be done in a number of ways. You can create a structure of fixed length such as:
    Code:
    //structure array for 30 records
    struct flower Myflowers[30];
    or else you can build a linked list of structures which would allow you to join a list of records that would grow as large or as small as it needed to be based on the number of records in the datafile. It is called a self referencial structure because it requires each record to be able to form a link with another record. Another name for the records are nodes.
    Code:
    typedef struct node_s
    {
      char common_name[100];
      char scientific_name[100];
      char color[50];
      struct node_s *linkp;
    }node_t;
    As far as reading from a file. If it is a binary file than use fread and fwrite. If it is a text file use fscanf and fprintf. You can also use fgets.
    Code:
      struct flower Myflowers[10];
      int counter = 0;
      int status;
    
      FILE *fptr = fopen("a://file.txt","r");
      if (fptr == NULL) exit(1);
      
      while(counter < 10)
      {
         status = fscanf(fptr,"%s%s", 
                  Myflowers[counter].common_name, 
                  Myflowers[counter].color);
         
         //validates records read on each loop pass
          if (status != 2) break;
          ++counter;
       }
       ...
    This is the way you could go about it using a structure array.
    Last edited by Witch_King; 08-25-2001 at 12:46 PM.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM