Text I/O Functions

Text files are the normal files that you can easily create using simple text editors.

The text file I/O functions in the stdio library are as follows. You can look up their manual pages to see more details. We use three examples to show how these functions work.

Mode Description
feof detects end-of-file marker in a file
fscanf reads formatted input from a file
fprintf prints formatted output to a file
fgets reads a string from a file
fputs prints a string to a file
fgetc reads a character from a file
fputc prints a character to a file

fgetc_cp.c use fgetc(-) and fputc(-) to copy files. fputc(int char, FILE *stream) writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream. Similarly, fgetc(FILE *stream) reads a character from the specified stread and advances the position indicator for the stream.

/*fgetc_cp.c*/
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    FILE * originfile, * copyfile;
    int c;

    if(argc!=2){
        printf("Useage: ./cp fileName\n");
        return 1;
    }
    originfile = fopen(argv[1], "r");

    if (originfile == NULL) {
        perror("fopen!\n");
        exit(EXIT_FAILURE);
    }

    copyfile = fopen("copyfile", "w");

    if (copyfile == NULL) {
        perror("fopen\n");
        exit(EXIT_FAILURE);
    }
    while ( (c=fgetc (originfile)) != EOF ){
        printf("%c",c);
        fputc (c, copyfile);
    }

    fclose (copyfile);
    fclose ( originfile );
    return 0;
}

fgets_cp.c use fgets(-) and fputs(-) to copy files. fputs(const char *str, FILE *stream) writes a string to the specified stream up to but not including the null character. char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.

/*fgets_cp.c*/
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char* argv[] ){
    FILE * originfile;
    FILE * copyfile;
    char line [256];

    if(argc!=2){
        printf("Useage: ./cp fileName\n");
        return 1;
    }

  originfile = fopen(argv[1], "r");
  if (originfile == NULL) {
    perror("fopen!\n");
    exit(EXIT_FAILURE);
  }

  copyfile = fopen("copyfile", "w");
  if (copyfile == NULL) {
    perror("fopen\n");
    exit(EXIT_FAILURE);
  }


    while ( fgets ( line, sizeof(line), originfile ) != NULL ){
        fputs ( line, stdout );
        fprintf (copyfile ,"%s", line);
    }

    fclose (copyfile);
    fclose ( originfile );
    return 0;
}

int fscanf(FILE *stream, const char *format, ...) reads formatted input from a stream. It returns the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. See the following example fscanf_cp.c. Please download the file inputlist before you run the code.

/*fscanf_cp.c*/
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
  FILE * originfile, * copyfile;
  char sid[11];  
  int score;

  originfile = fopen("inputlist", "r");

  if (originfile == NULL) {
    perror("fopen!\n");
    exit(EXIT_FAILURE);
  }

  copyfile = fopen("copyfile", "w");

  if (copyfile == NULL) {
    perror("fopen\n");
    exit(EXIT_FAILURE);
  }

  while (fscanf(originfile, "%s %d", sid, &score) == 2) {
    fprintf(copyfile, "%s %d\n", sid, score);
  }

  fclose(originfile);
  fclose(copyfile);

  return 0;
}