Hi,
This is some simple project I made in C that removes blank lines from text files. I needed this tool as I was working with a source files that contained a lot of blank lines So I made this code to remove them hope you like it.
Compile:
gcc stripblanks.c - o stripblanks
Code:
// File : stripblanks.c
// By : Ben a.k.a DreamVB
// Date : 20:53 15/06/2020
// Info : A small project that removes any blank lines from a text file.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define MAX_LINE 4048
typedef int BOOL;
BOOL IsBlankLine(char *s){
int x = 0;
int y = 0;
char temp[MAX_LINE] = {0};
while(x < strlen(s)){
//Skip first write spaces
if(s[x] != ' ' && s[x] != '\t' && s[x] != '\0' && s[x] != '\r'){
break;
}
x++;
}
//Copy from were the start white spaces were found to the end of the string.
while(x < strlen(s)){
temp[y] = s[x];
x++;
y++;
}
//Zap temp
temp[y] = '\0';
if(strlen(temp) == 0){
memset(temp,0,sizeof temp);
return TRUE;
}
memset(temp,0,sizeof temp);
return FALSE;
}
int main(int argc, char *argv[])
{
char lzBuffer[MAX_LINE] = {0};
FILE *fp = NULL;
if(argc != 2){
printf("Strip Blank Lines.\n");
printf("USE %s InputFile\n",argv[0]);
exit(0);
}
//Open the source file.
fp = fopen(argv[1],"r");
if(fp == NULL){
printf("Error Reading Input File: %s",argv[1]);
exit(0);
}
while(fgets(lzBuffer,sizeof lzBuffer,fp) != NULL){
const int len = strlen(lzBuffer);
if((lzBuffer[len-1] = '\r')){
lzBuffer[len-1] = '\0';
}
//Check if the line is empty.
if(!IsBlankLine(lzBuffer)){
//Print the line to the console.
printf("%s\n",lzBuffer);
}
}
//Close the file.
fclose(fp);
return 0;
}
Example
stripblanks example.c > fixed.c