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