Visit Our UNIX and Linux User Community


Need ideas how to attack this problem


 
Thread Tools Search this Thread
Top Forums Programming Need ideas how to attack this problem
# 1  
Old 06-16-2007
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?
# 2  
Old 06-16-2007
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 ?'
# 3  
Old 06-16-2007
Quote:
Originally Posted by grumpf
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.
# 4  
Old 06-16-2007
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
$

# 5  
Old 06-16-2007
Quote:
Originally Posted by Perderabo
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.
# 6  
Old 06-16-2007
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 ~]$

Previous Thread | Next Thread
Test Your Knowledge in Computers #926
Difficulty: Medium
On 05:20:00 UTC on Saturday, 24 January 2065, the Unix time value will equal 4000000000 seconds.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me get some ideas

Hello, I have been given a scripting project, but have not learned any scripting. I need to get some ideas on how to start. Attached is part of the project: I have no idea how to parse the arguments. What I had in mind was to get the arguments ($1, $2, ... ) and have if statements for different... (1 Reply)
Discussion started by: facepalm
1 Replies

2. Ubuntu

Problem in Postfix server/is my server got some attack

Hi Friends, This is logs of my mail log: mail for yahoo.com.tw is using up 4001 of 6992 active queue entries : 1 Time(s) mail for yahoo.com.tw is using up 4001 of 7018 active queue entries : 1 Time(s) mail for yahoo.com.tw is using up 4001 of 7072 active queue entries : 1 Time(s) ... (1 Reply)
Discussion started by: darakas
1 Replies

3. UNIX and Linux Applications

Need ideas for graduation project based on unix or linux Need ideas for graduation project based on

Dear all, i am in last year of electronics department in engineering faculty i need suggestions for a graduation project based on unix or free bsd or linux and electronics "embedded linux " i think about embedded unix for example or device drivers please i need helps (1 Reply)
Discussion started by: MOHA-1
1 Replies

4. Shell Programming and Scripting

BASH script problem using find, ideas?

Hi, I'm trying to write a script to search through my computer and find all .jpg files and put them all in a directory. So far I have this: for i in `find /home -name '*.jpg' ` ; do mv $i home/allen/Pictures/PicturesFound ; done When I run it, I get this error (this is only part of it, it... (2 Replies)
Discussion started by: FortressPTH
2 Replies

5. UNIX for Advanced & Expert Users

Sendmail - Any Ideas?

I have solaris 9 and am using sendmail to pickup requests and forward them to a bulk mail server on a different port. Now for the fun...In sendmail, I need to find a way to place a default address in the Mail From: and Rcpt To: or remove them as required - These will be picked up by the bulk mail... (3 Replies)
Discussion started by: mikey2003ma
3 Replies

6. Shell Programming and Scripting

Problem with Script that writes max lines of a file - Any ideas how to fix?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (4 Replies)
Discussion started by: mmiller99
4 Replies

7. Shell Programming and Scripting

any ideas?

i need to compare to dates/times given in the format MMDDhhmmYY. That is month, day, hour, minute, year. It is a 24 hour clock. I need to compare two dates to check that they are, say, less than 900 seconds apart. I have got to a point where it checks the time, turns the values into seconds and... (5 Replies)
Discussion started by: fwabbly
5 Replies

8. Cybersecurity

Any Ideas !!!!!!!!!!

Hi, I installed sybase server on a LINUX server. I assigen port 2025 whilst installation for sybase , later i uninstalled sybase and when i try to reinstall sybase and use port 2025 it throw up error saying that it is already in use, use other port number. How can I re-use the same port number... (2 Replies)
Discussion started by: suda
2 Replies

9. Shell Programming and Scripting

Any Ideas?

I have several staging directories on my UNIX server. /usr2/data1 /usr2/data2 /usr2/data3 /usr2/data4 /usr2/data5 /usr2/data6 In these directories a file is transferred from different PC's connected to the network via TCP/IP. The File name is constant for all directories. Transfers... (1 Reply)
Discussion started by: Docboyeee
1 Replies

Featured Tech Videos