'Im using putty for a class assignment and I am getting a segmentation fault

Write a C program that counts the number of non white-space characters in an input text file. Program takes as command argument(s) the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writes the output to an output file. (25 points)

The command format is as follows:

command -f inputfile outputfile

or,

command -s inputfile

-f indicates writing to an output file;
-s indicates displaying the output on the screen.

I am receiving the segmentation fault on the -s command

#include <stdio.h>
#define BLANK ' '
#define NEWLINE '\n'

int main(int argc, char *argv[])
{
        FILE *infile;
        char c;
        int char_count=0;

        FILE *outfile;
        outfile = fopen(argv[3], "w");

        //checks for the argc length to be within the correct perameteres

        if ((argc < 3) || (argc > 4))
        {
                printf("Incorrect format, please try gain\n");
                exit(1);
        }


        //checks the input file to see if it is empty

        if ( (infile= fopen(argv[2], "r")) == NULL)
        {
                fprintf(stderr,"%s: cannot open %s \n", argv[0], argv[1]);
                exit(1);
        }

        // count the number of charecters in infile

        while ( (c = getc(infile)) != EOF)
                if ((c != BLANK) && (c != NEWLINE) )
                        char_count++;

        //checks to see if the command is -s or not, outputing the corrct message to the  desired location

        if (argv[1] == "-s")
        {
                printf("%d characters\n", char_count);
        }
        else
        {
                fprintf(outfile, "%s contains %d characters\n", argv[2],  char_count);
        }

        return 0;
}


Solution 1:[1]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BLANK ' '
#define NEWLINE '\n'

int main(int argc, char *argv[])
{
    FILE *infile;
    FILE *outfile;
        char c;
        int char_count=0;

        //checks for the argc length to be within the correct perameteres
        if ((argc < 3) || (argc > 4))
        {
                printf("Incorrect format, please try gain\n");
                exit(1);
        }

    //checks if passed second argument is valid (-f or -s)
    if (strcmp(argv[1], "-f") == 0) {
        if (argc < 4) {
            fprintf(stderr, "Please inform output file. Usage: -f inputfile outputfile");
            exit(1);
        }
        outfile = fopen(argv[3], "w");
    }
    else if (strcmp(argv[1], "-s") != 0) {
        fprintf(stderr, "Parameter not recognized: %s\n", argv[1]);
        exit(1);
    }

        //checks the input file to see if it is empty
        if ( (infile= fopen(argv[2], "r")) == NULL)
        {
                fprintf(stderr,"%s: cannot open %s \n", argv[0], argv[1]);
                exit(1);
        }

        // count the number of charecters in infile
        while ( (c = getc(infile)) != EOF)
                if ((c != BLANK) && (c != NEWLINE) )
                        char_count++;

        //checks to see if the command is -s or not, outputing the corrct message to the  desired location
        if (strcmp(argv[1], "-s") == 0)
        {
                printf("%d characters\n", char_count);
        }
        else
        {
                fprintf(outfile, "%s contains %d characters\n", argv[2],  char_count);
        printf("Result written to %s\n", argv[3]);
        }

        return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rodrigo Kravetz