Need help with this program

W

wdandrews1

/* Remove.c
** Usage: remove < In_file > Out_file (using redirection)
*/
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char c;
int space = 0;

for(c = fgetc(stdin); !feof(stdin); c = fgetc(stdin)){
if((c !=' ')&&(space == 0)) /* start from beginning of line reading
in characters */
{
continue; /* we wan't to discard 'ii ' from beginning of line */
}
if(c ==' '){ /* space found, so increment counter */
space++;
}
if((space == 1)&&(space != 2)){ /* one space found so far... */
fputc(c, stdout); /* start printing characters until we reach
the second space */
}
if(space == 2){ /* second space found */
fputc('\r', stdout); /* mark as end of line */
space = 0; /* reset space counter */
}
if((space >= 2)&&(c !='\r')){ /* we don't need rest of line to the
'\r' mark */
continue; /* get rid of versions, descriptions, ect. */
}
}
return 0; /* we've read the entire file at this point, so we're at
the end */
}

/
**************************************(CUT)*******************************************
I'm using the linux GCC 4.2.1 Compiler. I also have the Borland
V5.02.
This is only a very short sample of the KNOPPIX LINUX LIVE DVD
packages description.
I need to strip the 'ii ' at the beginning, the version, and package
description.
What's needed is the package name ONLY, so that I can edit the list
to create
a 'KICK LIST file.
The output file should be like:
-------------------------------------------------------------------------------------------
Own
aalib-bin
aalib1
ace-of-penguins
acpi
adduser
........
........
........
and so on.
----------------------------------------------------------------------------------------------
Any Suggestions would be welcomed.
Thanks in advance,
Wayne Andrews

packages.txt
||=NAME======VERSION============PACKAGE
DESCRIPTION==================||
ii 0wn 0.1-1 0wn - Knoppix
installer
ii aalib-bin 1.4p5-22 sample programs
using aalib
ii aalib1 1.4p5-28 ascii art
library - transitional package
ii ace-of-penguins 1.2-8 Solitaire-games
with penguin-look
ii acpi 0.09-1 displays
information on ACPI devices
ii adduser 3.101 Add and remove
users and groups
ii adtool 1.2-3 command line
utility for Active Directory administration
ii aespipe 2.3b-4 AES-encryption
tool with loop-AES support
ii afio 2.5-4 archive file
manipulation program
ii airsnort 0.2.7e-2 WLAN sniffer
ii alevt 1.6.1-7.1 X11 Teletext/
Videotext browser
ii alien 8.64 install non-
native packages with dpkg
ii alsa-base 1.0.13-2 ALSA driver
configuration files
ii alsa-utils 1.0.13-1 ALSA utilities
ii amanda-client 2.5.1p1-2.1 Advanced
Maryland Automatic Network Disk Archiver (Client)
ii amanda-common 2.5.1p1-2.1 Advanced
Maryland Automatic Network Disk
*/
 
D

dj3vande

**************************************(CUT)*******************************************
I'm using the linux GCC 4.2.1 Compiler. I also have the Borland
V5.02.
This is only a very short sample of the KNOPPIX LINUX LIVE DVD
packages description.
I need to strip the 'ii ' at the beginning, the version, and package
description.
What's needed is the package name ONLY, so that I can edit the list
to create
a 'KICK LIST file.
The output file should be like:
-------------------------------------------------------------------------------------------
Own
aalib-bin
aalib1
[snip]

packages.txt
||=NAME======VERSION============PACKAGE
DESCRIPTION==================||
ii 0wn 0.1-1 0wn - Knoppix
installer
ii aalib-bin 1.4p5-22 sample programs
using aalib

[snip]

Use The Right Tool For The Job.
"awk '{print $2}'" should work quite nicely, since you seem to be
working on some kind of *nix.


dave
 
P

Phoenix

**************************************(CUT)*******************************************
I'm using the linux GCC 4.2.1 Compiler. I also have the Borland
V5.02.
This is only a very short sample of the KNOPPIX LINUX LIVE DVD
packages description.
I need to strip the 'ii ' at the beginning, the version, and package
description.
What's needed is the package name ONLY, so that I can edit the list
to create
a 'KICK LIST file.
The output file should be like:
-------------------------------------------------------------------------------------------
Own
aalib-bin
aalib1
[snip]

packages.txt
||=NAME======VERSION============PACKAGE
DESCRIPTION==================||
ii 0wn 0.1-1 0wn - Knoppix
installer
ii aalib-bin 1.4p5-22 sample programs
using aalib

[snip]

Use The Right Tool For The Job.
"awk '{print $2}'" should work quite nicely, since you seem to be
working on some kind of *nix.

dave

Agree, awk is meant for text processing. It can give you a more
elegant and simple solution
 
C

Chris Dollin

(e-mail address removed) wrote:

I'll avoid commenting on what I (but perhaps not everyone here)
would regard as horrible style, but:
if((space == 1)&&(space != 2)){ /* one space found so far... */

If space == 1, then it's definitely != 2; the second test is
completely unnecessary.
 
P

Peter 'Shaggy' Haywood

Groovy hepcat (e-mail address removed) was jivin' in comp.lang.c on
Wed, 12 Dec 2007 6:12 am. It's a cool scene! Dig it.
/* Remove.c
** Usage: remove < In_file > Out_file (using redirection)
*/

[Snip.]

To paraphrase the problem, as I understand it; you have a (presumably
text) file containing lines consisting of the literal text "ii "
followed by a name field, a version field and a description field
(separated by white space, presumably), and you want to redirect this
file to the standard input of your program. The program should extract
only the name field of each line and send this to standard output,
which you will redirect to another file. Oh yeah; and the description
field is allowed to have embedded spaces, whereas the other fields are
not. Have I stated that correctly?
So something like this may be in order:

char name[100];

while(1 == scanf("ii %99s %*s %*[^\n]", name))
{
puts(name);
}

Remember to test for read errors and such. Use ferror() for this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top