• Navigation überspringen
  • Zur Navigation
  • Zum Seitenende
Organisationsmenü öffnen Organisationsmenü schließen
Friedrich-Alexander-Universität Lehrstuhl für Informatik 4 (Systemsoftware)
  • FAUZur zentralen FAU Website
  1. Friedrich-Alexander-Universität
  2. Technische Fakultät
  3. Department Informatik
Suche öffnen
  • English
  • Campo
  • StudOn
  • FAUdir
  • Stellenangebote
  • Lageplan
  • Hilfe im Notfall
  1. Friedrich-Alexander-Universität
  2. Technische Fakultät
  3. Department Informatik
Friedrich-Alexander-Universität Lehrstuhl für Informatik 4 (Systemsoftware)
Menu Menu schließen
  • Lehrstuhl
    • Team
    • Aktuelles
    • Kontakt und Anfahrt
    • Leitbild
    • 50-jähriges Jubiläum
    Portal Lehrstuhl
  • Forschung
    • Forschungsbereiche
      • Betriebssysteme
      • Confidential Computing
      • Eingebettete Systemsoftware
      • Verteilte Systeme
    • Projekte
      • AIMBOS
      • BALu
      • BFT2Chain
      • DOSS
      • Mirador
      • NEON
      • PAVE
      • ResPECT
      • Watwa
    • Projektkampagnen
      • maRE
    • Seminar
      • Systemsoftware
    Portal Forschung
  • Publikationen
  • Lehre
    • Sommersemester 2025
      • Applied Software Architecture
      • Ausgewählte Kapitel der Systemsoftware
      • Betriebssystemtechnik
      • Projekt angewandte Systemsoftwaretechnik
      • System-Level Programming
      • Systemnahe Programmierung in C
      • Systemprogrammierung 1
      • Verteilte Systeme
    • Wintersemester 2024/25
      • Betriebssysteme
      • Middleware – Cloud Computing
      • Systemprogrammierung 2
      • Verlässliche Echtzeitsysteme
      • Virtuelle Maschinen
      • Web-basierte Systeme
    Portal Lehre
  • Examensarbeiten
  1. Startseite
  2. Extern

Extern

Bereichsnavigation: Lehre
  • Systemnahe Programmierung in C
    • Vorlesung
      • Übung
        • Folien
        • Aufgaben
        • SPiCboard
          • Linux libc-Doku
          • Prüfung
            • Evaluation
              • Intern

              Linux libc-Doku

              Files | Functions
              Input/Output

              Files

              file  stdio.h
               

              Functions

              int printf (const char *format,...)
               Print formatted data to stdout More...
               
              int fprintf (FILE *stream, const char *format,...)
               Print formatted data to stream. More...
               
              int fgetc (FILE *stream)
               Read a character. More...
               
              char * fgets (char *s, int size, FILE *stream)
               Read a string. More...
               
              int fputc (int c, FILE *stream)
               Write a character. More...
               
              int fputs (const char *s, FILE *stream)
               Write a string. More...
               
              void perror (const char *s)
               Print an error message. More...
               
              int feof (FILE *stream)
               Test end-of-file indicator of a file stream. More...
               
              int ferror (FILE *stream)
               Test error indicator of a file stream. More...
               

              Detailed Description

              This code snippet illustrates the usage of fgetc() and fputc():

              int c;
              while ((c = fgetc(stdin)) != EOF) {
              if (fputc((unsigned char) c, stdout) == EOF) {
              perror("fputc");
              exit(EXIT_FAILURE);
              }
              }
              if (ferror(stdin)) {
              // error
              }
              // no error; end of file reached
              ferror
              int ferror(FILE *stream)
              Test error indicator of a file stream.
              fgetc
              int fgetc(FILE *stream)
              Read a character.
              perror
              void perror(const char *s)
              Print an error message.
              fputc
              int fputc(int c, FILE *stream)
              Write a character.
              exit
              void exit(int status)
              Terminate process.

              This code snippet illustrates the usage of fgets() and fputs():

              char buffer[1024];
              char *check;
              // reads at most 1023 characters per iteration
              while ((check = fgets(buffer, 1024, stdin)) != NULL) {
              if (fputs(buffer, stdout) == EOF) {
              perror("fputs");
              exit(EXIT_FAILURE);
              }
              }
              if (ferror(stdin)) {
              // error
              }
              // alternative to ferror():
              // if (feof(stdin)) {
              // // no error; end of file reached
              // } else {
              // // error
              // }
              fgets
              char * fgets(char *s, int size, FILE *stream)
              Read a string.
              fputs
              int fputs(const char *s, FILE *stream)
              Write a string.

              Function Documentation

              int printf ( const char *  format,
                ... 
              )

              The printf() (print formatted) function produces output according to a format string as specified in format and writes it stdout. The format string defines the structure of the output (i.e., how many and which additional arguments) and the additional parameter of the printf() function are used to replace the arguments in the format string with actual data. It is important that the number of additional parameter matches the number of arguments of the format string.

              An argument in the format string starts with a %. The following table shows some important arguments, however, there are several more options described in the manpage of printf() (man 3 printf):

              Type Description
              i, d integer
              u unsigned integer
              f floating point
              p pointer
              s string
              c character

              Example:

              int i = 5;
              char c = 'a';
              void *p = &i;
              printf("Hello World!\n"); // no arguments
              printf("$> i: %i\n$> c: %c\n$> f: %f\n \
              $> p: %p\n", i, c, 3.14, p); // including arguments
              printf
              int printf(const char *format,...)
              Print formatted data to stdout
              // produces:
              Hello world!
              $> i: 5
              $> c: a
              $> f: 3.140000
              $> p: 0x7ffebe5dbe34

              We do not expect error handling for printf().

              Parameters
              formatformat string
              ...arguments
              Return values
              >=0number of characters printed
              <0on error, errno is set
              int fprintf ( FILE *  stream,
              const char *  format,
                ... 
              )

              The fprintf() (file stream print formatted) function is very similar to printf(), except that it does not write to stdout, but to stream.

              For more details see printf().

              // both lines write to stdout
              fprintf(stdout, "Hello world!\n");
              printf("Hello world!\n");
              // write to stderr
              fprintf(stderr, "Error in file '%s' at line %u\n", __FILE__, __LINE__);
              fprintf
              int fprintf(FILE *stream, const char *format,...)
              Print formatted data to stream.

              We do not expect error handling for fprintf().

              Parameters
              streamfile stream to write
              formatformat string, see printf()
              ...arguments
              Return values
              >=0number of characters printed
              <0on error, errno is set
              int fgetc ( FILE *  stream)

              The fgetc() (file stream get character) function returns an unsigned char casted to an int read from a stream or returns EOF.

              fgetc() returns EOF on error or when the end of the file is reached. To distinguish these two events the feof() or ferror() function can be used. Be aware, that the return value of fgetc() must be saved in an int (and not in an unsigned char) in order to distinguish EOF and 0xFF (which is the character ÿ when interpreted as ISO-8859-1).

              Parameters
              streamfile stream to read
              Return values
              EOFon error or end of file, errno is set on error
              !=EOFunsigned char read from stream
              char * fgets ( char *  s,
              int  size,
              FILE *  stream 
              )

              The fgets() (file stream get string) function reads at most size-1 characters from stream and saves them in the buffer pointed to by s. It terminates the string in s with a null byte (\0).

              fgets() reads in characters until it finds a newline character (\n) or it has read size-1 characters. If an error occurred or the end of file was reached it returns NULL. The ferror() and feof() functions can be used to distinguish these two events.

              Parameters
              sbuffer to write to
              sizesize of the buffer
              streamfile stream to read
              Return values
              NULLon error or end of file, errno is set on error
              !=NULLpointer to s
              int fputc ( int  c,
              FILE *  stream 
              )

              The fputc() (file stream put character) function writes the character c to stream.

              We do not expect error handling when using fputc().

              Parameters
              cchar to print (casted to an int)
              streamfile stream to write
              Return values
              !=EOFprinted char
              EOFon error, errno is set
              int fputs ( const char *  s,
              FILE *  stream 
              )

              The fputs() (file stream put string) function writes the string pointed to by s to stream.

              We do not expect error handling when using fputs().

              Parameters
              sstring to be printed
              streamfile stream to write
              Return values
              >=0on success
              EOFon error, errno is set
              void perror ( const char *  s)

              The perror() (print error) function produces an error message on stderr printing the string in s followed by a human-readable description of the value in errno. This is especially helpful after a failed call to a system or library function, which sets the errno variable (e.g., malloc()).

              Parameters
              sstring printed before the actual error message
              int feof ( FILE *  stream)

              The feof() (file stream end of file) function tests the EOF indicator of stream.

              Parameters
              streamfile stream to test
              Return values
              0end-of-file indicator is not set
              !=0end-of-file indicator is set
              int ferror ( FILE *  stream)

              The ferror() (file stream error) function tests the error indicator of stream.

              Parameters
              streamfile stream to test
              Return values
              0error indicator is not set
              !=0error indicator is set
              Friedrich-Alexander-Universität
              Erlangen-Nürnberg

              Schlossplatz 4
              91054 Erlangen
              • Impressum
              • Datenschutz
              • Barrierefreiheit
              • Facebook
              • RSS Feed
              • Xing
              Nach oben