-1

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!

collinskewl2
  • 75
  • 1
  • 3

4 Answers4

2

Your condition here is always true, so does precisely nothing:

if (*buffer != '\n' || *buffer != '\t' || *buffer != ' ') {
    fputs(buffer, stdout);
}

You're asking "Is the first character not a newline or not a tab or not a space?" Every character can say that: a space is not a newline, and a newline is not a tab. You need to check all the characters in the line:

int is_all_white(char *s) {
    while (*s) {
        if (!('\n' == *s || '\t' == *s || ' ' == *s)) return 0;
        s += 1;
    }
    return 1;
}

... 

if (! is_all_white(buffer)) {
    fputs(buffer, stdout);
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
0

The conditional of the following if statement

if(*buffer != '\n' || *buffer != '\t' || *buffer != ' ')

is true for every line.

You should change the logic to use:

int isBlankLine(char const* line)
{
   for ( ; *line != '\0'; ++line )
   {
      if ( !isspace(*line) )
      {
         return 0;
      }
   }
   return 1;
}

int part2 (char *buffer)
{
   if (!isBlankLine(buffer) )
   {
      fputs(buffer, stdout);
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Your if condition is always satisfied - you check if the first character of buffer is not a newline, and if it is a newline, you check that if it's not a tab.

You could replace all the || in the condition with && - that would mean that every line whose first character is not a newline, space or tab gets printed.

What you really want to do is check every character of the line with the [isspace]1 function. If there is a non-whitespace character, you can print it.

Teyras
  • 1,262
  • 11
  • 22
-2
if(*buffer != '\n' || *buffer != '\t' || *buffer != ' '){
    fputs(buffer, stdout);
}

replace to

char c;
if(1==sscanf(buffer, " %c", &c)){
    fputs(buffer, stdout);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70