I am new to C and here I have C program that will take in an X amount of files from the command line and output the files to stdout but deletes the blank lines. If the user doesn't input any files it will read directly from stdin.
Everything functions smoothly except for the removing of the lines.
Here is my program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUMCHARS 1024
int main (int argc, char** argv)
{
int good_file = 0;
if(argc <= 1){
good_file++;
test(stdin);
}
FILE* files[argc - 1];
int i;
for(i = 0; i < argc - 1; i++){
if ((files[i]=fopen(argv[i+1], "r"))==NULL){
continue;
}
else{
good_file++;
test(files[i]);
}
}
if(!good_file){
fprintf(stderr, "ERROR!\n");
}
}
int test (FILE *file)
{
char buffer[NUMCHARS];
while (fgets(buffer, NUMCHARS, file) != NULL)
part2(buffer);
fclose(file);
}
int part2 (char *buffer)
{
if(*buffer != '\n' || *buffer != '\t' || *buffer != ' '){ //if(*buffer != '\n') successfully deletes (skips) plain newlines
fputs(buffer, stdout);
}
}
In function part 2, as you can see by my comment the program will successfully delete (skips) just newlines if I remove the ||
to the '\t'
and the ' '
in that if statement. But a lot of blank lines are not necessarily "blank". i.e they will have tabs or white space on them. Using the logic I had with removing newlines I applied it to tabs and white space as well.
But with this new implementation it won't even skip the newlines. And it doesn't work what so ever.
Appreciate the feedback!