C program to create a file and write data into file

Write a C program to create a file and write contents in it. How to create a file in C programming? C program to input a string from user and store it in a text file. Learn how to create a file and write contents to it in C programming.

Required knowledge

Basic input and output, Pointers, Structures, File handling

How to create a file in C?

C programming provides built-in support to create, read, write and append data to file. To perform any operation on file we use a built-in FILE structure. You need to create pointer to FILE type. The pointer to FILE type will hold a logical reference to our physically existed file on disk (hard disk).

In this post I will only explain how to create a file and write data into file. Step by step descriptive logic to create a file and write data into file.

  1. Declare a FILE type pointer variable to store reference of file, say FILE * fPtr = NULL;.
  2. Create or open file using fopen() function. fopen() function is used to open a file in different mode. You can open a file in basic three different mode r(read), w(write) and a(append) mode. We will use w file mode to create a file.

    fopen("file-name", "read-mode"); function accepts two parameter first file name to read/create/write/append data, next is file open mode. On success it return pointer to FILE type otherwise NULL pointer.

  3. Input data from user to write into file, store it to some variable say data.
  4. C provides several functions to perform IO operation on file. For this post to make things simple I will use fputs() function to write data to file. fputs("content-to-write", stream) function accepts two parameters. First string data to write into file, next pointer to FILE type that specifies where to write data.

    Use fputs() function to write data to fPtr i.e. perform fputs(data, fPtr);.

  5. Finally after completing all operations you must close file, to save data written on file. Use fclose(fPtr) function to close file.

I have restricted context of this post to create a file and store data into file. Hence there will be no output on console. Alternatively, you can view file contents by opening the newly created file in your favourite text editor.

Program to create a file and write data into file

/**
 * C program to create a file and write data into file.
 */

#include <stdio.h>
#include <stdlib.h>

#define DATA_SIZE 1000

int main()
{
    /* Variable to store user content */
    char data[DATA_SIZE];

    /* File pointer to hold reference to our file */
    FILE * fPtr;


    /* 
     * Open file in w (write) mode. 
     * "data/file1.txt" is complete path to create file
     */
    fPtr = fopen("data/file1.txt", "w");


    /* fopen() return NULL if last operation was unsuccessful */
    if(fPtr == NULL)
    {
        /* File not created hence exit */
        printf("Unable to create file.\n");
        exit(EXIT_FAILURE);
    }


    /* Input contents from user to store in file */
    printf("Enter contents to store in file : \n");
    fgets(data, DATA_SIZE, stdin);


    /* Write data to file */
    fputs(data, fPtr);


    /* Close file to save file data */
    fclose(fPtr);


    /* Success message */
    printf("File created and saved successfully. :) \n");


    return 0;
}

Output

Enter contents to store in file :
Hurray!!! I learned to create file in C programming. I also learned to write contents to file. Next, I will learn to read contents from file on Codeforwin. Happy coding ;)
File created and saved successfully. :)

Open newly created file in your favourite text editor to view saved changes. In next post we will learn to read file contents to C program.

Output

Create and write content to file in C

Happy coding 😉