Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

reading a txt file

hello i am looking for some code to open a txt file and read the contents up to a : char and write the resut to another txt file, continue reading the next chars in the line up to another : char, then proceed to the following line and the the same till the end of the txt file, i want to compile this using gcc on linux, please advise and mabe offer some code. thanks deus

Comments

  • ActorActor Member Posts: 447 ✭✭
    : hello i am looking for some code to open a txt file and read the
    : contents up to a : char and write the resut to another txt file,
    : continue reading the next chars in the line up to another : char,
    : then proceed to the following line and the the same till the end of
    : the txt file, i want to compile this using gcc on linux, please
    : advise and mabe offer some code. thanks deus
    :
    [blue]
    Seems to me that what you're wanting to do is copy one file to another, replacing ':' with NEWLINES in the process. Here's a solution in C.
    [/blue]
    [code]
    /*
    program replace -- copy input to output replacing : with

    */
    #include

    int main()
    {
    char c ;

    while ((c = getchar()) != EOF)
    if (c == ':')
    putchar('
    ') ;
    else
    putchar(c) ;
    return 0 ;
    }
    [/code]
    [blue]
    This copies standard input to standard output. You'll need to use redirection to copy from one file to another.

    If you want to keep the ':' and add a NEWLINE after it, use this program.
    [/blue]
    [code]
    /*
    program replace -- copy input to output replacing : with

    */
    #include

    int main()
    {
    char c ;

    while ((c = getchar()) != EOF) {
    putchar(c) ;
    if (c == ':')
    putchar('
    ') ;
    }
    return 0 ;
    }
    [/code]
    [blue]
    C++ versions should not be difficult to code.
    [/blue]
Sign In or Register to comment.