![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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;
}
'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? |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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;
}
|
|
#4
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
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;
}
[cda@localhost ~]$ ./get File contains ab[cda@localhost ~]$ |
|||
| Google The UNIX and Linux Forums |