C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - File Handling - Read and Write Characters

C Programming

In this tutorial we will learn to read and write characters in files in C programming language.

We have already learned how to create files in C in the previous tutorial. Feel free to check that out.

The getc and putc I/O functions

We use the getc() and putc() I/O functions to read a character from a file and write a character to a file respectively.

Syntax of getc:

char ch = getc(fptr);

Where, fptr is a file pointer.

Note! EOF marks the End-Of-File. And when we encounter EOF character it means we have reached the end of the file.

Syntax of putc:

putc(ch, fptr);

Where, ch is the character to write and fptr is a file pointer.

Write a program in C to create a new file and save user name taken as input from the user

For this we will first create a FILE pointer and create a file by the name username.txt. Feel free to use any other filename that you like.

Then we will use the putc() function to write the characters in the file.

Once that is done we can then use the getc() function to read the file data till we hit the EOF and show it in the console.

Complete code:

#include <stdio.h>

int main(void) {
  // creating a FILE variable
  FILE *fptr;
  
  // creating a character variable
  char ch;
  
  // open the file in write mode
  fptr = fopen("username.txt", "w");
  
  // take user input
  printf("Enter your name: ");
  
  // keep reading the user input from the terminal
  // till Return (Enter) key is pressed
  while( (ch = getchar()) != '\n' ) {

    // write character ch in file
    putc(ch, fptr);

  }
  
  // close the file
  fclose(fptr);
  
  // open the file in read mode
  fopen("username.txt", "r");
  
  // display the content of the file
  printf("\nFile content:\n");
  while( (ch = getc(fptr)) != EOF ) {
    printf("%c", ch);
  }

  printf("\nEnd of file\n");
  
  // close file
  fclose(fptr);
  
  return 0;
}

Output:

Enter your name: Yusuf Shakeel

File content:
Yusuf Shakeel
End of file

Content of the file:



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT