C program to remove empty lines from a file

Quick links

Write a C program to remove all empty lines from a file. How to remove all empty lines from a given file in C programming. Logic to remove empty lines from file in C program.

Required knowledge

Basic Input Output, String, Pointers, File Handling

In previous post we learned to remove specific line from a file. In this post we will continue ahead with same logic and will learn to remove empty lines from a given text file.

Logic to remove empty lines from a file

Step by step descriptive logic to remove empty lines from a file.

  1. Open source file to remove empty lines, store its reference to a FILE pointer srcFile.
  2. Create a temporary file remove-blanks.txt and store its reference to tempFile.
  3. Read single line from srcFile and store to some variable say buffer.
  4. Write line buffer to tempFile if it is not an empty line. A line is said to be empty if it contains only whitespace characters.
  5. Repeat step 3-4 till end of source file.
  6. Close both files to save changes.
  7. Delete source file and rename tempFile with same name as of source file.

Program to remove empty lines from file

/**
 * C program to remove empty lines from a file.
 */

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

#define BUFFER_SIZE 1000

/* Function declarations */
int  isEmpty(const char *str);
void removeEmptyLines(FILE *srcFile, FILE *tempFile);
void printFile(FILE *fptr);


int main()
{
    FILE *srcFile;
    FILE *tempFile;

    char path[100];


    /* Input file path */
    printf("Enter file path: ");
    scanf("%s", path);


    /* Try to open file */
    srcFile  = fopen(path, "r");
    tempFile = fopen("remove-blanks.tmp", "w");


    /* Exit if file not opened successfully */
    if (srcFile == NULL || tempFile == NULL)
    {
        printf("Unable to open file.\n");
        printf("Please check you have read/write previleges.\n");

        exit(EXIT_FAILURE);
    }



    printf("\nFile contents before removing all empty lines.\n\n");
    printFile(srcFile);


    // Move src file pointer to beginning
    rewind(srcFile);

    // Remove empty lines from file.
    removeEmptyLines(srcFile, tempFile);


    /* Close all open files */
    fclose(srcFile);
    fclose(tempFile);


    /* Delete src file and rename temp file as src */
    remove(path);
    rename("remove-blanks.tmp", path);


    printf("\n\n\nFile contents after removing all empty line.\n\n");

    // Open source file and print its contents
    srcFile = fopen(path, "r");
    printFile(srcFile);
    fclose(srcFile);

    return 0;
}


/**
 * Print contents of a file.
 */
void printFile(FILE *fptr)
{
    char ch;

    while((ch = fgetc(fptr)) != EOF)
        putchar(ch);
}



/**
 * Checks, whether a given string is empty or not.
 * A string is empty if it only contains white space
 * characters.
 * 
 * Returns 1 if given string is empty otherwise 0.
 */
int isEmpty(const char *str)
{
    char ch;

    do
    {
        ch = *(str++);

        // Check non whitespace character
        if(ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r' && ch != '\0')
            return 0;

    } while (ch != '\0');

    return 1;
}



/**
 * Function to remove empty lines from a file.
 */
void removeEmptyLines(FILE *srcFile, FILE *tempFile)
{
    char buffer[BUFFER_SIZE];

    while ((fgets(buffer, BUFFER_SIZE, srcFile)) != NULL)
    {
        /* If current line is not empty then write to temporary file */
        if(!isEmpty(buffer))
            fputs(buffer, tempFile);
    }
}

Output

Enter file path: data/blanks.txt

File contents before removing all empty lines.

I love programming.



I am learning C programming at Codeforwin.

Programming with files is fun.

Learning C programming at Codeforwin is simple and easy.


File contents after removing all empty line.

I love programming.
I am learning C programming at Codeforwin.
Programming with files is fun.
Learning C programming at Codeforwin is simple and easy.

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>