The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replay Attack Ashvin Gaur Security 3 05-27-2008 04:22 AM
anonymous ftp attack? dennisheazle Security 2 04-07-2008 05:11 PM
Bruteforce attack on my pc rdns UNIX for Dummies Questions & Answers 6 10-16-2007 11:37 AM
any ideas? fwabbly Shell Programming and Scripting 5 07-27-2007 06:27 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-16-2007
Registered User
 

Join Date: May 2007
Location: San Francisco bay area
Posts: 32
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

Code:
#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?
Reply With Quote
Forum Sponsor
  #2  
Old 06-16-2007
Registered User
 

Join Date: May 2006
Posts: 101
Quick Reply

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 ?'
Reply With Quote
  #3  
Old 06-16-2007
Registered User
 

Join Date: May 2007
Location: San Francisco bay area
Posts: 32
Quote:
Originally Posted by grumpf View Post
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

Code:
#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.
Reply With Quote
  #4  
Old 06-16-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,663
It sounds like "about" would need to be changed to "ab
out".

Here is my solution...
Code:
$ 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
$
Reply With Quote
  #5  
Old 06-16-2007
Registered User
 

Join Date: May 2007
Location: San Francisco bay area
Posts: 32
Quote:
Originally Posted by Perderabo View Post
It sounds like "about" would need to be changed to "ab
out".

Here is my solution...
Code:
$ 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.
Reply With Quote
  #6  
Old 06-16-2007
Registered User
 

Join Date: May 2007
Location: San Francisco bay area
Posts: 32
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


Code:
#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 ~]$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0