MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Input and Output using File Pointers

Input and output (I/O) using file pointers in C involve reading from and writing to files. File pointers, of type FILE*, manage the file operations and track the current position within the file. Here’s an explanation of how to perform I/O operations using file pointers:

Opening and Closing Files

Before performing any I/O operations, a file must be opened using fopen, which returns a file pointer. After operations are complete, the file should be closed using fclose to release resources.

#include <stdio.h>
FILE *fp; // Declare a file pointer // Open a file for reading or writing fp = fopen("filename.txt", "mode"); // Close the file fclose(fp);

Writing to a File

Several functions can be used to write data to a file:

  1. fprintf: Formats and writes a string to the file.

    fprintf(fp, "Formatted string: %d\n", 42);
  2. fputs: Writes a string to the file.

    fputs("This is a string.\n", fp);
  3. fputc: Writes a single character to the file.

    fputc('A', fp);

Reading from a File

Several functions can be used to read data from a file:

  1. fscanf: Reads formatted input from the file.

    int num; fscanf(fp, "%d", &num);
  2. fgets: Reads a string from the file until a newline or end-of-file is encountered.

    char buffer[100]; fgets(buffer, 100, fp);
  3. fgetc: Reads a single character from the file.

    char ch; ch = fgetc(fp);

Example: Writing and Reading a File

Here’s a complete example demonstrating writing to and reading from a file using the functions mentioned above.

#include <stdio.h>
int main() { FILE *fp; // Open a file for writing fp = fopen("example.txt", "w"); if (fp == NULL) { printf("Error opening file for writing!\n"); return 1; } // Write to the file fprintf(fp, "Hello, World!\n"); fputs("This is a file handling example.\n", fp); fputc('A', fp); // Close the file fclose(fp); // Open the file for reading fp = fopen("example.txt", "r"); if (fp == NULL) { printf("Error opening file for reading!\n"); return 1; } // Read from the file using fscanf char str[50]; fscanf(fp, "%s", str); printf("Read using fscanf: %s\n", str); // Read from the file using fgets fgets(str, 50, fp); printf("Read using fgets: %s", str); // Read from the file using fgetc char ch; ch = fgetc(fp); printf("Read using fgetc: %c\n", ch); // Close the file fclose(fp); return 0; }

File Positioning

Sometimes, you may need to control the file pointer position using functions like fseek, ftell, and rewind.

  • fseek: Sets the file position to a specific location.

    fseek(fp, offset, origin); // origin can be SEEK_SET, SEEK_CUR, or SEEK_END
  • ftell: Returns the current file position.

    long pos = ftell(fp);
  • rewind: Sets the file position to the beginning of the file.

    rewind(fp);

Error Handling

Always check the return values of file operations to handle errors gracefully.

  • ferror: Checks if a file error occurred.

    if (ferror(fp)) {
    printf("Error reading from the file.\n"); }
  • feof: Checks if the end of the file is reached.

    if (feof(fp)) {     printf("End of file reached.\n"); }

Using these functions and techniques, you can efficiently perform input and output operations using file pointers in C.

Report an issue

Reporting: Input and Output using File Pointers (topic)

Related Posts