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 - Getting Started

C Programming

In this tutorial we will learn to create files in C programming language.

So far we have been using the printf() and scanf() functions to print data in the console and take user input from the console respectively. And the data we were working with were all temporary and lasted till our program executed.

Now we will learn to work with files by using I/O (Input/Output) library functions in C programming langauge.

FILE data type

To work with files in C programming language we have to declare them as type FILE which is defined in the standard library of I/O functions.

Syntax:

FILE *fptr;

The fopen function

We use the fopen() function to create a new file and use an existing file.

Syntax:

FILE *fptr;
fptr = fopen("filename", "mode");

Where fptr is the file pointer. "filename" is the name of the file that we want to create or use.

filename can be like example or with extension like sample.txt or full path /path/to/file.ext.

"mode" is how we are intending to use the file. And it takes the following values.

  • r open the file only for reading, provided the file exists.
  • w open the file only for writing.
  • a open the file for appending data.

We can also use the following in new compilers.

  • r+ open the file for reading and writing, provided the file exists.
  • w+ open/create the file for reading and writing.
  • a+ open the file for reading and appending data.

Please Note!

  • Opening a file in r reading mode: if the file exists then it is opened for reading otherwise, it gives an error.
  • Opening a file in w writing mode: if the file already exists then its content is overridden. Otherwise, a new file is created.
  • Opening a file in a appending mode: if the file exists then it is opened for appending. Otherwise, a new file is created.

On success the fopen() function returns a FILE pointer. Otherwise, NULL is returned.

The fclose function

It is important to close the file connection after use and for this we use the fclose() function.

Syntax.

fclose(file_pointer);

Where, file_pointer is the file pointer created for the file.

Write a program in C to create a new file by the name entered by the user

For this we will first create a variable to hold the path of the file. Then using the fopen() function we will create the file. And then we will close the connection using the fclose() function.

#include <stdio.h>

int main(void) {
  // creating a FILE variable
  FILE *fptr;
  
  // creating a variable to store filename
  char filename[255];
  
  // get file path from the user
  printf("Enter the filename: ");
  scanf("%s", filename);
  
  // open the file in write mode
  fptr = fopen(filename, "w");
  
  if (fptr != NULL) {
    printf("File created successfully!\n");
  
    // close the file
    fclose(fptr);
  }
  else {
    printf("Failed to create the file.\n");
  }
  
  return 0;
}

Output:

Enter the filename: /Users/yusufshakeel/Desktop/sample/helloworld.txt
File created successfully!

For this example I am entering the complete path of the file. We can also enter just the filename like helloworld.txt and it will create the file in the directory where our executable C program file resides.

Folder content before:

YUSUF-MBP:sample yusufshakeel$ pwd
/Users/yusufshakeel/Desktop/sample
YUSUF-MBP:sample yusufshakeel$ ls -la
total 0
drwxr-xr-x   2 yusufshakeel  staff   64 Jan 01 16:34 .
drwx------+ 10 yusufshakeel  staff  320 Jan 01 16:34 ..

Folder content after:

YUSUF-MBP:sample yusufshakeel$ ls -la
total 0
drwxr-xr-x   3 yusufshakeel  staff   96 Jan 01 16:37 .
drwx------+ 10 yusufshakeel  staff  320 Jan 01 16:34 ..
-rw-r--r--   1 yusufshakeel  staff    0 Jan 01 16:37 helloworld.txt