How do I only read in unique lines using command-line arguments?

47 Views Asked by At

Currently, my C program, alter.c, successfully creates a char array, a, from text files invoked using command-line arguments.

However, I want to alter it so that instead of reading in every line, it will only read in lines that are not consecutive repeats of the previous line. If a line is a repeat but does not follow its identical line, it shouldn't be deleted.

For example, say the contents of testfile.txt are:

Hi there

Hi there

Hi there

Hello.

If invoked as 'alter testfile.txt', the char array created should be:

Hi there.

Hello.

Here's the code I currently have:

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

#define MAX_NAME_SZ 100
#define caseChange 32
#define MAXCHARS 79
int main (int argc, char *argv[])
 {
  char ch, *a;
  static char changed = 'f';

  /* if there is more than one argument */
  if (argc > 1)
    {
      int i = 1, j = 0;
      FILE *fp;
      while (i < argc)
    {
      a = malloc (MAX_NAME_SZ * sizeof (char));
      j = 0;
      fp = fopen (argv[i], "r");
      if (fp == NULL)
        {
          /*Error statement in case an error is encountered /*
             fprintf (stderr, "Error encountered for File %s : %s\n",
             argv[i], strerror (errno));
             return 3;            /* I chose to return 3 if an error was encountered */
        }
      else
        {
          while (((ch = fgetc (fp)) != EOF) && (j < 99))
        {
          a[j++] = ch;
        }
          a[j] = '\0';

          fclose (fp);
          changeCase (a, changed);
          noNums (a, changed);
          identLines (a, changed);
          spaces (a, changed);
          printf ("\n");
        }
      i++;

    }
      if (changed == 'f')
    return 1;
      else
    return 0;

    }
  else
    {
      a = malloc (MAX_NAME_SZ * sizeof (char));
      fgets (a, MAX_NAME_SZ, stdin);
      changeCase (a, changed);
      noNums (a, changed);
      identLines (a, changed);
      spaces (a, changed);
      printf ("\n");
    }
}

I assume I need another while statement when doing my

while (((ch = fgetc (fp)) != EOF) && (j < 99))

but I'm not sure. Any help is appreciated.

1

There are 1 best solutions below

1
Askar Safin On

I didn't read all your code. (Well, questions in style "this is big bunch of my code with 100 lines, please, help me find errors" and not good questions on this site.)

But I want to point that this code:

char ch;
((ch = fgetc (fp)) != EOF)

...is typical beginner's error. "fgetc" returns "int". And you must NOT convert this "int" into "char" before you compare it with EOF. Put "fgetc" result into "int" variable, then compare it with EOF and only after that convert this "int" into "char". And after this do any other processing with it.

Maybe will help. Maybe not. It is possible your code contains other problems. Make your code smaller and probably we will read it.

Also, this:

while (((ch = fgetc (fp)) != EOF) && (j < 99))

...is bad style. I mean practice of putting a lot of things inside "while" condition. This makes it hard to read. while ((ch = fgetc (fp)) != EOF) is OK, assuming ch is int. Also, your code has broken identation. You should make your code as easy-to-read as possible if you want us to comment it