Torbj�rn Kristoffersen wrote:

> I want a function that reads a line from a file, separates
> the two variables (one string and one integer).
> (I am going to use those variables later)..
> 
> The code so far:
>  
> 
> void output_db()
> {
>    for(;;)
>    {
>       char name[60];
>       int  number;
>                   
>       if(!fgets(line, sizeof(line), fp))
>          break;
>                    
>       if(sscanf(line, "%s %d", name, &number) != 2)
>       {
>          fprintf(stderr, "error !\n");
>          continue;
>       }
>                               
>       fprintf(stdout, "%s %d", name, &number);
>    }
>                                       
>    fclose(fp);
> }
> 
> (*fp is global)

... and `line' isn't defined anywhere.

> But the result is : "error ! error !". 
> The file film.db is exactly two lines by the way.
> 
> The file format looks like this:
> 
> First blood 1982
> The shining 1980

As the sscanf() manpage says (regarding `%s'):

       s      Matches  a  sequence of non-white-space characters;

You would need to use %[...] to match the name.

A question. How will your file format cope with titles containing
numerals?

sscanf() doesn't perform backtracking. If you specify a %[...] 
conversion specifier which allows digits, the entire line will be
considered part of the title, and the %d will fail. If you specify one
which doesn't allow digits, you won't be able to have numerals in
titles.

You will need to either seperate the title from the year using some
character which never occurs in a title (e.g. a tab), or use something
other than sscanf() to parse the data (e.g. regexps).

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to