copying input to output

This is a discussion on copying input to output within the C Programming forums, part of the General Programming Boards category; I was fiddling around with a simple program in the first chapter of K&R that looks something like this: Code: ...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    copying input to output

    I was fiddling around with a simple program in the first chapter of K&R that looks something like this:

    Code:
    #include <stdio.h>
    
    /* copy input to output; 1st version */
    
    int main(void)
    {
         int c;
    
         c = getchar();
         while ( c != EOF )
         {
              putchar( c );
              c = getchar();
         }
         return 0;
    }
    I wanted to modify the program, so that it would take the input, and instead of basically just copying it to the output once, have it copy it out, say like ten times or whatever. I made a few attempts at this modification, but I am not sure of how to get what I want. I added a for loop, to make it look like this:

    Code:
    #include <stdio.h>
    
    int c,i;
    
    int main(void)
    {
    
    /* copy the input to the output ten times */
    
    	  while(( c = getchar()) != EOF )
    	  {
    	       for ( i = 0; i <= 10; i++ )
    		    putchar ( c );     
    	  }
    
    
         return 0;
    }
    This works, but it does not do what I want it to do. If I type in the word 'hello', instead of displaying hello ten times, it gives me this:

    hhhhhhhhhhheeeeeeeeeeelllllllllllllllllllllloooooo ooooo

    Not exactly what I was after. Even though this is a trivial modification, I would still like to know how to make it work the way I want it to.

    kermit

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're gonna have to store everything they type in, and then you can print it out 10 times.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Yep!. Get the string and display it ten times.
    or else getchar and store it in array and display it ten times.
    I hope you need only string to display ten times not char.
    Saravanan.T.S.
    Beginner.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Ok, so in the original program, if you type in 'hello' and hit enter, it displays it back on the screen, like this

    hello
    hello

    I just thought I could make it print a bunch of times. So I am to understand that I am using the wrong functions for what I want to do here (ie, getchar() and putchar())? I can see I have some more reading to do, but now I have some ideas to think about, and where to start looking.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    You can use getchar() and putchar(), but you will need to separate your input and output.

    As XSquared implied, when you input the characters, instead of putchar() you must load them into a buffer. You must count each character as it is placed in the buffer.

    Then start your output loop as you tried, but instead of putchar() you need a second loop to get each character from the buffer.

    So, Loop 1 is for the number of times you want to output the sentence. Within loop 1 is Loop 2 which outputs each character from the buffer.

    HTH
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    i don't know if this exactly what you want but it takes a input and prints it 10 times --you can replace gets with fgets() if ya want

    Code:
    #include <stdio.h>
    int main()
    {
    int word,i;
    printf("Enter: ");
    gets(word);
    for(i =0; i <= 10; i++)
    printf("%s\n",word);
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >you can replace gets with fgets() if ya want
    Translation: Use fgets because gets stinks. By the way, the above code is incorrect. gets only accepts a char pointer, not an int. This is what you want:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char word[BUFSIZ];
        int  i;
    
        printf("Enter: ");
        fflush(stdout);
    
        if (fgets(word, sizeof word, stdin) != NULL) {
            for (i = 0; i < 10; i++)
                printf("%s\n",word);
        }
    
        return 0;
    }
    Note that you were printing the word 11 times since you started at 0 and iterated until i was greater than 10.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    when i try and compile your code it says
    c:\program files\miracle c\tax calculator.c: line 5: Parse Error, expecting `']'' or `NUMBER'
    'char word[BUFSIZ]'
    aborting compile
    but my code works perfectly when i compile it , that's odd

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You have to #define BUFSIZ first.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    haha, yea Miracle C is no Miracle, the debugger doesn't help me at all , well i used to have linux on my system so i used GCC, what would you reccomend for xp?

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    this may be my compiler again but

    if (fgets(word, sizeof word, stdin) != NULL) {
    should be
    Code:
     if(fgets(word, sizeof(word), stdin, != NULL) {
    i believe

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by AsmLover
    haha, yea Miracle C is no Miracle, the debugger doesn't help me at all , well i used to have linux on my system so i used GCC, what would you reccomend for xp?
    Borland 5.5. It's debugger works well.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    9
    > if (fgets(word, sizeof word, stdin) != NULL)
    > if(fgets(word, sizeof(word), stdin, != NULL)

    compiles great both ways.

    can someone explain why sizeof(word) and sizeof word are the same here?

    uh, what is BUFSIZ , it works great, but i dont understand it , it's not #defined...

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    i think he was just trying to imply feel in that for w/e the buffer size should be.....my guess, i was messing around just now with the weakness's of gets() and i used this code

    Code:
    #include<stdio.h>
    int main()
    {
    char x[6];
    printf("Enter a 5 letter word: ");
    gets(x);
    printf("%s",x);
    sleep(3);
    return 0;
    }
    and i had mixed results, one time it said an error occured at the location of bla bla and the program had to be shut down another time i typed in "alkdjflksdsj" and it printed the input request again but like this

    "alkdjflksdsj a 5 letter word: ");"

    and then i tried using this code
    Code:
    #include<stdio.h>
    int main()
    {
    char x[6];
    printf("Enter a 5 letter word: ");
    fgets(x, sizeof(x), stdin);
    printf("%s",x);
    sleep(3);
    return 0;
    }
    and it prints it fine just cutting off the first 5 characters of the 20 or w/e characters you enter, where does the rest of the characters go? so if you planned a buffer overflow to execute arbitrary code would you just use the input as a code? like

    input: lskdfgoto exec(bla bla);

    Just curious how buffers work

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    11

    > if (fgets(word, sizeof word, stdin) != NULL)
    > if(fgets(word, sizeof(word), stdin, != NULL)

    compiles great both ways.

    can someone explain why sizeof(word) and sizeof word are the same here?


    It shouldn't work. fgets is a function that takes three arguments, not four with a relational operator thrown in. Your compiler blows. The parentheses around sizeof aren't required for an expression, they are required for a type. So

    sizeof(int)

    needs parentheses, but

    sizeof word

    doesn't because word is a variable used as an expression.


    and it prints it fine just cutting off the first 5 characters of the 20 or w/e characters you enter, where does the rest of the characters go?

    They stay in the stream. fgets reads up to a newline unless the buffer is filled first, anything not read stays where it is until you do read it. So this'll work
    Code:
    fgets(word, 5, stdin); // Type "Hoobajoob", word has Hoob
    fgets(word, 5, stdin); // word has bajo

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input and Output files
    By matrixhelp in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 02:07 AM
  2. Replies: 5
    Last Post: 02-25-2008, 05:35 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Input / Output help (arrays)
    By fp123 in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2006, 11:30 AM
  5. output as input
    By DDC in forum Linux Programming
    Replies: 3
    Last Post: 02-23-2003, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21