Richard Ivanowich wrote:

> > > As a newbie programmer i am now looking make a simple database program.
> > > This proggy reads file number one then file number two and matches the two
> > > records with the same username then outputs to a file.  What i need to know
> > > is how to read the file into a structure.  How do i do this???
> > 
> > It depends entirely upon the format of the file.
> 
> The file is just a plain test file like so
> 
> ---
> joe           30
> larry         20
> moe           50
> ---

The simplest way is to use fgets + sscanf, e.g.

        for (;;)
        {
                char buff[LINE_MAX];
                char name[NAME_MAX];
                int number;

                if (!fgets(buff, sizeof(buff), fp))
                        break;

                if (sscanf(buff, "%s %d", name, &number) != 2)
                {
                        fprintf(stderr, "syntax error: %s", buff);
                        continue;
                }

                /* do something with `name' and `number' */
        }

A custom scanner built using lex will be more efficient, but it's
unlikely to be worth the effort in this case.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to