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.