Opearation on file in C language ,Open,read,write,close

1. Opening an Existing File:

To open an existing file in C we must use the fopen() function in the stdio.h header file. The exact type you open depends on the function you want.
The Opening operation creates a connection between our program and an existing file, allowing us to read,write, append, or add operations with different types of file modes.
File Modes:
File mode are the permissions that determines how the file is opened for different purposes for eg. read,write,append etc.
"r" (read):"r" is the read mode that is used to Opens the file for reading existing data from a file.
"w" (write): is the write mode that Opens the file for writing, or for overwriting any existing contents available in the file.
"a" (append): append mode opens a file in append mode for writing, but data is appended or written to the end of existing file content.
"r+" (read/write): r+ is the mode that is used to Opens the file in both read and write mode for reading from and writing to the file.

Example: c program to open the file to read its contents
#include <stdio.h>
int main()
  {
  FILE *fptr = fopen("my_file.txt", "r"); // "r" mode for reading
  if (fptr == NULL)
  {
   printf("File not available or Wrong File Name!\n");
   return 1;
  }
  printf("File has been opened successfully!\n");
  fclose(fptr);
   return 0;
 }


2. Creating a New File:
Creating new file operation in C creates a new file on the storage device, allowing us to store the data permanently. The new file is created with the help of fopen() function available in stdio.h header file.

Create new file in C using fopen()
#include <stdio.h>
int main()
 {
 FILE *fptr = fopen("my_file.txt", "w"); // "w" is writing mode
  if (fptr == NULL)
  {
  printf("Error while creating file!\n");
  return 1;
 }
 printf("File has been created successfully!\n");
 fclose(fptr);
 return 0;
 }


3. Reading from a File:
• This operation reads or retrieves data stored within an opened file.
To read the data from the file once successfully opened the fread(fptr, size, count, fp) function is used.
• fptr: is file Pointer to the memory location where the read data will be stored.
• size: is the total size in bytes of each element to be read.
• count: is the total Number of elements to be read at a time.
• fp: is the File pointer associated with the opened file.


C program to read content of a file.

#include <stdio.h>
int main()
 {
  FILE *fp = fopen("my_file.txt", "r");
  if (fp == NULL)
   {
   printf("Error opening file!\n");
   return 1;
  }
 char buff[100]; // Buffer to store read data
 int bytes_read = fread(buff, 1, sizeof(buff), fp); // Read up to 100 bytes
 if (bytes_read > 0)
 {
  printf("Read %d bytes from file: %s\n", bytes_read, buff);
 }
 else
 {
  printf("Error reading from file!\n");
 }
  fclose(fp);
 return 0;
}


4. Writing to a File:
• You can write data to or adds new data to a file in C using fopen() to open the file (choose "w" for overwrite, "a" for append, or "r+" for both read/write).
This operation adds new data or modifies existing content in an opened file.
• The function fwrite(ptr, size, count, fp) is used to write contents to a file.
Example: C program to write in file usinf fwrite()

#include <stdio.h>
int main()
  {
  FILE *fptr = fopen("my_file.txt", "a"); // "a" file opens in append mode
  if (fptr == NULL)
   {
   printf("Error while opening file!\n");
   return 1;
  }
  char data[] = "Welcpme to C Languae.\n";
  int bytes_written = fwrite(data, sizeof(data[0]), sizeof(data) / sizeof(data[0]), fptr);
  if (bytes_written > 0)
  {
   printf("%d bytes has Successfully wrote to file.\n", bytes_written);
  }
  else
  {
    printf("Error writing to file! Try again…….\n");
  }
 fclose(fptr);
 return 0;
 }


5. Closing a File:
fclose(fp):This function always close files. Closing a file frees the resources associated with the file, preventing resource wastage and potential program crashes. This ensures that any written data that flows from program memory to the actual storage device ensures data persistence.


Previous Topic:-->> Types of files in C. || Next topic:-->>example on file.