C Input Output Tips And Tricks

A compilation of useful IO commands.

Opening and closing file

1
2
3
  FILE *fp;
  fp = fopen("data.txt", "r"); // reading mode
  fclose(fp);

Reading from file char by char until EOF:

1
2
3
4
5
6
7
  FILE *fp;
  fp = fopen("data.txt", "r"); // reading mode
  char c;
  while ((c = fgetc(fp)) != EOF) {
    printf("%c", c);
  }
  fclose(fp);

Write to file char by char:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  char *str = "cibureh";
  FILE *fp;
  fp = fopen("data.txt", "w"); // write mode file is created or truncated to zero length on write

  int len = strlen(str);
  int i;
  char c;

  for (i = 0; i < len; i++) {
    c = fputc(str[i], fp);
    printf("wrote \"%c\" to file\n", c);
  }

  fclose(fp);

Read from file line by line:

1
2
3
4
5
6
  FILE *fp; 
  fp = fopen("data.txt", "r");
  while (fgets(buffer, BUFFERSIZE, fp) != NULL) {
    printf("%s", buffer);
  }
  fclose(fp);

Method signatures for convenience

int fgetc(FILE *stream);

  • Read one (ASCII) character (8-bits) at a time (slow for large files):
  • Returns EOF when at the end of file or on error.
  • Note: to read from a file pass a file pointer, to read from stdin pass stdin

int fputc(int c, FILE *stream);

  • Write one (ASCII) character (8-bits) at a time (slow for large files):
  • Returns EOF on error.
  • Note: to write to a file pass a file pointer, to write to stdout pass stdout

char *fgets(char *buf, int size, FILE *in);

  • Reads the next line from in into buffer buf
  • Halts at \n or at size-1 and adds \0 (the C string termination character called NUL)
  • Returns pointer to buf if successful or NULL

int fputs(const char *str, FILE *out);

  • Writes str to out stopping at \0
  • Returns number of characters written or EOF

Another option is fread and fwrite:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <string.h>
  #include <stdio.h>

  #define BUFFERSIZE 100

  int main(int argc, char *argv[]) {
    FILE *fp;
    char buffer[BUFFERSIZE];
    char c[] = "this is cibureh";

    fp = fopen("data.txt", "w+");

    fwrite(c, strlen(c)+1, sizeof(char), fp);

    fseek(fp, 0L, SEEK_SET); // set file pointer to beggining of file

    fread(buffer, BUFFERSIZE, sizeof(char), fp);

    printf("%s\n", buffer);

    fclose(fp);

    return 0;
  }
Written on November 29, 2018