Good programming pracctice is to also test the return value of sscanf. sscanf returns the number of succeeful reads
 
ie                                                 if( fscanf( fd, "%d" "%d", &a, &b) != 2)
                                                                   #error statement & exit

Kind Regards
        Renato Parletta........
 
To reply send mail to:   [EMAIL PROTECTED]
                    ICQ No:   13078952
 
         "l can only assume that a "Do Not File" document
          is filed in a "Do Not File" file."
 
Senator Frank Church
Senate Intelligence Subcommittee Hearing, 1975
 
 
 
-----Original Message-----
From: holotko <[EMAIL PROTECTED]>
To: Linux C Programming <[EMAIL PROTECTED]>
Date: Wednesday, 7 October 1998 16:57
Subject: Re: Separate two integers

Torbjørn Kristoffersen wrote:
I'm wondering how it is possible to read from a file looking like this:

0001 1
0002 0
0003 0
0004 1

The program should read the file and understand 0001 as an integer and 1 or
0 as an integer. The two numbers should be saved in variables such as a, b.

Can anyone help?

T. Kristoffersen <[EMAIL PROTECTED]>


Yes, you can first open the file for reading via "fopen()" and then read the data into separate integer variables using "fscanf()" from the file stream. For example, the following example should set you in the right direction:

/* readvals.c  */

#include <stdio.h>
#include <stdlib.h>

int main()
{
   FILE fd;
   int a, b;

   if ( !(fd = fopen( "numbers.dat", "r")) ) {
     fprintf( stderr, "file error...\b");
     exit(1);
   }
 
   do  {
      fscanf( fd, "%d" "%d", &a, &b);
      printf("a = 000%d, b = %d \n", a, b);  /* if you want to see the data displayed to stdout */
   } while ( feof( fd ) == 0);

 exit(0);

}
 

You could also write fscanf() without the 2 sets of quotation marks as:

  fscanf( fd, "%d  %d", &a, &b);
 
 
 

-- 
email: [EMAIL PROTECTED]
Local mailserver  , remote 

OOP means programming
 

Reply via email to