Need ideas how to attack this problem

I'm at a total loss how to attack this problem.

I have a file that contains
ab

What I need to do is if
1)if the string "ab" doesn't contain a newline, I need to insert one back into the buffer.
2)If the file contains two consecutive blank lines, skip over it.

Here is what I started

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

#define LINE_LENGTH 80

main(void)
{
   FILE* fp = NULL;
   static char line[LINE_LENGTH];

   fp=fopen("/home/cda/file.txt","r");
   while ( fgets(line, LINE_LENGTH, fp) != NULL) {
     if((strlen(line)) < LINE_LENGTH)
        printf("File contains %s", line);
     else
       printf("line is too long \n");
   }

   fclose(fp);
   return 0;
}

For the first half of the problem, I was thinking about checking 'line' for '\n'. However, how would I push '\n', so that the buffer of line would go from
'a' 'b' '\0' '\0'

to
'a' 'b' '\n' '\0'

Is there some kind of ANSI C function that would do this? Otherwise, I was thinking about maybe using some kind of stack.

For the second half of the problem, the only thing I could possibly think of would be to test if each line like

line[0]=='\n'

However, I have NO idea how to track if the previous line also had a blank line. Ideas?

hi frequency8,
i am not sure if i understand you problem totaly

  1. add an \n when missing
    ask your self when can this happen ? perhaps at the last line in file ?

  2. Skip empty lines, i asume that empty means 'contains no char execpt \n'
    why not compare this line with '\n' ?

  3. to get extra points use gnu- getline() function. It saves you thinking about 'did i read the whole line or only part of it because my buffer is to small ?'

Regarding point 1. I would assume '\n' would be missing if it was the last line in the file. I still have no idea how to solve it in this case.

Regarding point 2. I compared the line with '\n'. It appears to work correctly. Here is what I have
[cda@localhost ~]$ more file.txt
a

b

c
d

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

#define LINE_LENGTH 80

main(void)
{
   FILE* fp = NULL;
   static char line[LINE_LENGTH];
   char key[] = "\n";

   fp=fopen("/home/cda/file.txt","r");
   while ( fgets(line, LINE_LENGTH, fp) != NULL) {
     if((strcmp (line,key) == 0)) {
       continue;
     }
     if((strlen(line)) < LINE_LENGTH)
       printf("File contains %s", line);
     else {
       printf("The input line is too long\n");
     }
   }

   fclose(fp);
   return 0;
}

Regarding point 3. I have getline() on my Linux box. However, the target systems that I would be running this under use FreeBSD and OpenBSD. I'm not sure if getline() is available on these systems.

It sounds like "about" would need to be changed to "ab
out".

Here is my solution...

$ cat abba.c
#include <stdio.h>
main(void)
{
        FILE *fp;
        int suppress, state, c;
        fp=fopen("file.txt","r");
        state=0;
        while ( (c=getc(fp)) != EOF ) {
                suppress=0;
                if (state == 2 && c != '\n') {
                        putchar('\n');
                }
                switch (c)
                {
                case 'a':
                        state=1;
                        break;
                case 'b':
                        if (state == 1) state=2;
                        break;
                case '\n':
                        if (state == 3) {
                                suppress=1;
                        } else {
                                state=3;
                        }
                        break;
                default:
                        state=0;
                        break;
                }
                if (!suppress) putchar(c);
        }
        fclose(fp);
        exit(0);
}
$
$
$
$
$
$ cat file.txt
about time



jjjjj
about face
ssss
$
$
$
$
$ ./abba
ab
out time
jjjjj
ab
out face
ssss
$

If my input file was
about

I would need
ab
out

in my input file.

I just tried using fwrite() and it didn't work.

Okay, no before anyone starts to think "Hey, I might be doing this kids homework", I would like to point out that it's been over a decade since I've been in school. Anyhow, here is what I've attempted.

[cda@localhost ~]$ more file.txt
ab

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

#define LINE_LENGTH 80

main(void)
{
   FILE* fp = NULL;
   static char line[LINE_LENGTH];
   char key[] = "\n";
   int value = 0;

   fp=fopen("/home/cda/file.txt","rw");
   while ( fgets(line, LINE_LENGTH, fp) != NULL) {
     value=strlen(line);
     if((strcmp (line,key) == 0)) {
       continue;
     }
     if(line[value] != '\n')
       putchar('\n');
     if(value < LINE_LENGTH)
       printf("File contains %s", line);
     else {
       printf("The input line is too long\n");
     }
   }

   fclose(fp);
   return 0;
}

And the output
[cda@localhost ~]$ ./get
File contains ab[cda@localhost ~]$