Nov 15, 2016

C Programming #90: fileio - fopen, fclose, fgetc, fputc, fgets, fputs

C Programming #90: fileio - fopen, fclose, fgetc, fputc, fgets, fputs

This article go through several API related file operation. Linux/Unix core philosophy is that everything is a file. Hence file operations becomes core of any application/system programming. Hence for a person who claims to be good at 'C' need to know the file operation really well. In this series of article it introduces all the API with example it it never tries to exhaustive, for which you can always refer man pages.


fopen

FILE *fopen(const char *path, const char *mode);

Before any operation is done on any file. It needs to be opened and fopen exactly does that. It takes two argument. You have give the path of the file (can be relative/full). Apart from it you need to also give in the mode, like read, write or append etc.. in which you want to open the file in. fopen returns a FILE pointer that has all the details of file in it. All the subsequent API's need this FILE pointer to access the file. Different modes are as follows.

Mode Description
r Reading
w Writing
a Append
r+ Reading and Writing
w+ Reading and Writing, file is created
  if it doesn't exist.
a+ Append, file is created if it does-not
  exist

Note the for some reason fopen fails, then it will return NULL pointer. And appropriate errno would be set for user for error handling. You need to include errno.h. Hence always the fopen call should be followed by check with NULL and displaying the errno in case of error.

#include <stdio.h>
#include <errno.h>
int main()
{ 
   FILE *fp;

   fp = fopen("/tmp/test_file", "w+");

   if(fp != NULL) {
      printf("file opened succesfully fp is %p\n", fp);
   }else {
      printf("fopen failed with errorno %d\n", errno);
   }

   return 0;
}
file opened succesfully fp is 0x1f85010

fclose

int fclose(FILE *stream);

All opened file should be closed once the usage of the file is done. fclose closed the file properly by flushing out any unwritten data back to file. fclose returns 0 on successful closure and returns EOF on error and setting appropriate errno. It is highly recommended the re-init the file pointer to NULL once the close is done. So that file pointer wont be accessed after it closure. Now adding fclose to above program.

#include <stdio.h>
#include <errno.h>
int main()
{ 
   FILE *fp;

   fp = fopen("/tmp/test_file", "w+");

   if(fp != NULL) {
      printf("file opened successfully fp is %p\n", fp);
   }else {
      printf("fopen failed with errorno %d\n", errno);
   }

   if(fclose(fp) != EOF) {
      printf("file closed successfully");
   }else {
      printf("fclose failed with errorno %d\n", errno);
   }
   fp = NULL;

   return 0;
}
file opened successfully fp is 0x6de010
file closed successfully

fputc

int fputc(int c, FILE *stream);

c character is put to file pointed by stream. Small example

#include <stdio.h>
#include <errno.h>
int main()
{
   FILE *fp;

   fp = fopen("/tmp/test_file", "w+");

   if(fp != NULL) {
      printf("file opened successfully fp is %p\n", fp);
   }else {
      printf("fopen failed with errorno %d\n", errno);
   }

   fputc('h', fp);
   fputc('e', fp);
   fputc('l', fp);
   fputc('l', fp);
   fputc('o', fp);

   if(fclose(fp) != EOF) {
      printf("file closed succesfully");
   }else {
      printf("fclose failed with errorno %d\n", errno);
   }
   fp = NULL;

  return 0;
}
file opened successfully fp is 0xdc3010
file closed successfully
# cat /tmp/test_file
hello

fgetc

int getc(FILE *stream);

It gets one character from the file stream. If it reaches the end of the file it returns EOF. Lets read the same file tmp_file till EOF.

#include <stdio.h>
#include <errno.h>
int main()
{
   FILE *fp;
   int c;

   fp = fopen("/tmp/test_file", "r");

   if(fp != NULL) {
      printf("file opened successfully fp is %p\n", fp);
   }else {
      printf("fopen failed with errorno %d\n", errno);
   }

   printf("Content of file is \n");

   while((c = fgetc(fp)) != EOF) {
      putchar(c);
   }
   printf("\n");

   if(fclose(fp) != EOF) {
      printf("file closed successfully");
   }else {
      printf("fclose failed with errorno %d\n", errno);
   }
   fp = NULL;

  return 0;
}
file opened successfully fp is 0x960010
Content of file is 
hello
file closed successfully

fputs

int fputs(const char *s, FILE *stream);

Same thing as fputc is extended here to strings. Here the small example that extends the same idea.

#include <stdio.h>
#include <errno.h>
int main()
{
   FILE *fp;

   fp = fopen("/tmp/test_file", "w+");

   if(fp != NULL) {
      printf("file opened successfully fp is %p\n", fp);
   }else {
      printf("fopen failed with errorno %d\n", errno);
   }

   fputs("Hello World", fp);

   if(fclose(fp) != EOF) {
      printf("file closed successfully");
   }else {
      printf("fclose failed with errorno %d\n", errno);
   }
   fp = NULL;

  return 0;
}
file opened successfully fp is 0xc60010
file closed successfully
# cat /tmp/test_file
Hello World

fgets

char *fgets(char *s, int size, FILE *stream);

Lets retrive the string from the same file here.

#include <stdio.h>
#include <errno.h>
int main()
{
   FILE *fp;
   char str[100];

   fp = fopen("/tmp/test_file", "r");

   if(fp != NULL) {
      printf("file opened succesfully fp is %p\n", fp);
   }else {
      printf("fopen failed with errorno %d\n", errno);
   }

   printf("Content of file is \n");

   fgets(str, 100, fp);
   puts(str);

   printf("\n");

   if(fclose(fp) != EOF) {
      printf("file closed succesfully");
   }else {
      printf("fclose failed with errorno %d\n", errno);
   }
   fp = NULL;

  return 0;
}
file opened succesfully fp is 0xeb8010
Content of file is 
Hello World

file closed succesfully

No comments :

Post a Comment