[plug] C, bad syntax etc.

Anthony J. Breeds-Taurima tony at cantech.net.au
Wed Nov 20 11:22:48 WST 2002


On Wed, 20 Nov 2002, Cameron Patrick wrote:

> On Wed, 20 Nov 2002 10:37:09 +0800, John Knight wrote:
> 
> | I have a file called 'temp.dat'. The file consists of something like
> | this:
> | Monday 23
> | Tuesday 28
> | Wednesday 31
> | (etc.)
> | 
> | I have to grab all of those temperatures in the file and display the
> | average  (it'll be a fixed 7 days, don't worry), so I need to ignore the
> | names and just get the temperatures, using only C.
> 
> In that case, fscanf is your friend.
> 
> Try something like this:
> 
> char day[80];
> int temp;
> 
> for(;;)
> {
>   items_read = fscanf(f, "%s %d\n", day, &temp);
>   if (items_read != 2) break;
>   do_stuff(temp);
> }

You can avoid the char *stuff like:
int temp;

for(;;)
{
  items_read = fscanf(f, "%*s %d\n", &temp);
  if (items_read != 1) break;
  do_stuff(temp);
}


the "*" in the scanf sayd this will be there but ignore it.



Yours Tony

   Jan 22-25 2003           Linux.Conf.AU            http://linux.conf.au/
		  The Australian Linux Technical Conference!



More information about the plug mailing list