Need help with this program

Discussion in 'C Programming' started by wdandrews1@embarqmail.com, Dec 11, 2007.

  1. Guest

    /* 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
    */
    , Dec 11, 2007
    #1
    1. Advertising

  2. Guest

    In article <>,
    <> wrote:

    >**************************************(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
    , Dec 11, 2007
    #2
    1. Advertising

  3. Phoenix Guest

    On Dec 11, 2:19 pm, wrote:
    > In article <>,
    >
    >
    >
    > <> wrote:
    > >**************************************(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
    Phoenix, Dec 11, 2007
    #3
  4. Chris Dollin Guest

    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.

    --
    Trivia Hedgehog
    "Based on their behaviour so far -- I have no idea" /Sahara/
    Chris Dollin, Dec 11, 2007
    #4
  5. Groovy hepcat 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.

    --
    Dig the sig!

    ----------- Peter 'Shaggy' Haywood ------------
    Ain't I'm a dawg!!
    Peter 'Shaggy' Haywood, Dec 15, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wrecker
    Replies:
    3
    Views:
    375
    Kevin Spencer
    Jun 21, 2005
  2. pipi
    Replies:
    1
    Views:
    604
    Joseph Millar
    Jul 21, 2003
  3. Replies:
    1
    Views:
    451
    Walter Roberson
    Aug 12, 2005
  4. Replies:
    66
    Views:
    1,140
    Kenny McCormack
    Jun 18, 2006
  5. Kris Stark
    Replies:
    1
    Views:
    84
    John Bokma
    Sep 8, 2005
Loading...

Share This Page