C program to create a file and write contents

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

Required knowledge

Basic input and output, Pointers, Structures, File handling

How to create a file?

C programming provides built-in support to 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 store contents to it. Step by step descriptive logic to create a file and store contents in it.

  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 string from user to save in file, store it to some variable say data.
  4. There are several function to perform IO operation on file. For this post to make things simple I will use fputs() to write contents to file. fputs("content-to-write", stream) function accepts two parameters. First string content to write to 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 all changes. Use fclose(fPtr) function to close file.

I have restricted context of this post to create and store contents to 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 and write contents to a file

/**
 * C program to create and write contents to a 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 contents to file */
    fputs(data, fPtr);


    /* Close file to save contents */
    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 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>