fprintf()

Formats then writes text to a stream.

  #include <stdio.h>
  
  int fprintf(FILE *stream,
  
  const char *format, ...);    
Parameter

stream

A pointer to a file stream.

format

A pointer to a character string containing format information.

Remarks

The fprintf() function writes formatted text to stream and advances the file position indicator. Its operation is the same as printf() with the addition of the stream argument.

If the file is opened in update mode (+) the file cannot be written to and then read from unless the write operation and read operation are separated by an operation that flushes the stream's buffer. To flush a stream's buffer, use fflush() or one of the file positioning operations ( fseek(), fsetpos(), or rewind()).

This facility may not be available on configurations of the EWL that run on platforms without file systems.

The function returns the number of arguments written or a negative number if an error occurs.

Listing: Example of fprintf() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

static char filename[] = "myfoo";

int a = 56;

char c = 'M';

double x = 483.582;

// create a new file for output

if (( f = fopen(filename, "w")) == NULL) {

printf("Can't open %s.\n", filename);

exit(1);

}

// output formatted text to the file

fprintf(f, "%10s %4.4f %-10d\n%10c", filename, x, a, c);

// close the file

fclose(f);

return 0;

}

Output to file foo:

myfoo 483.5820 56

M