I/O
Home

 

I/O is one of C's strengths.  It is very close to the system so it is easy to interact with details of the environment. 

Stream versus line I/O

Fortran does line i/o, due to its roots with input on Hollerith cards and output on line printers.  This is a fundamental conceptual difference in the two languages.   In Fortran some results might be computed and printed as follows:

do i=1,10
    results(i) = compute(xx)
end do
write(6,*) 'the results are', results

This cannot be done in C since an implicit array write cannot be done.  The natural way to do it in C is beginning writing to the stream, and put a new line on it at the end.

printf(" the results are ");
for( i=0; i<10; i++)
{
    printf(" %f", compute(xx) );
}
printf("\n");

Printing n numbers per line

This is done with the remainder operator %.  The expression a%b gives the remainder of a/b and is only zero (and so false) when a is divisible by b.  To throw a new line every 8 numbers you would add the following code to the loop above:

if( !(i%8) ) printf("\n");

Standard I/O streams and buffered output

stdin - standard input stream (fort.5 in Fortran)

stdout - standard output stream (fort.6 in Fortran)

stderr - standard error output (nothing similar in Fortran)

stdout is buffered output, so that the results are saved in a buffer until it is full, and the write commit occurs.  If the code crashes before this happens the output that is still in the buffer will disappear. 

stderr is not buffered, and output to here will certainly appear, but will be very slow because of the nature of unbuffered output.  

A write commit can be forced by calling fflush on the unit. 

structure of output routines

printf

This does a formatted print to stdout.  The first argument is a string that includes any text, and also formatting descriptors.  Descriptors tell how numbers or strings should be output, they begin with '%', and are within double quotes.  Th string should end with the new line character ('\n') if a carriage return is needed.   The (optional) quantities to be printed are in the fields after the string. 
printf(" this is the format, number=%i \n", i);

sprintf, fprintf

The same as printf, but the first parameter is an additional parameter, the pointer to the string (sprintf) or output stream (fprintf) where the output goes.

io formats

These specify how numbers or strings are to be converted, and are fairly analogous to Fortran.  C converts float to double when passing by value to printf, so printing a float with a double specification will work.

format Fortran C
integer i4 %4i
exponential e10.3 %10.3e
floating f6.3 %6.3f
general double   %6g

Strings  %s for best fit.  %80s will be exactly 80 characters long, right justified with space fill if the string has less than 80 characters.

Character %c for a single character.  This does not have a precision.

Integer %i for something declared int, and %li for a long integer (although %i will probably still work).

Exponential notation %e for double variables.  %9.2e by analogy with Fortran.

Best representation.  %g will print a double as either a float or exponential format depending on which is shorter.

Floating %f will produce a number like 5.6.

opening, writing to, closing files

fopen, fprintf, fclose

    /* declare the pointers to the file - basically the io units */
    FILE *ioData , *ioCloudy ;/* the two output files */

    /*this will be the output, open with write permissions  */
    /* doubled backslash needed for win32 */
    ioData = fopen( "c:\\projects\\cloudy\\cgene\\data.txt","w" );
    /*this checks if error, pointer is null then */
    if( ioData == NULL )
    {
        printf("error opening data.txt\n");
        exit(-1);
    }

  /* now write to the file with fprintf */
  fprintf( ioData, 'Some output to the file .\n");

  /* now close the file */
  fclose( ioData );

Reading and printing entire file

while( fgets( chLine , sizeof(chLine) , stdin )!=NULL )
    printf("%s\n", chLine);

Reading numbers

char chLine[100];
double a;
/* read in a line image*/
fgets( chLine , sizeof(chLine), stdin);
sscanf( chLine , "%f", &a);