a simple input to output c copy program

This is a discussion on a simple input to output c copy program within the C Programming forums, part of the General Programming Boards category; When i run program below on turbo c compiler,i got the output but i can't get out of the output ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    a simple input to output c copy program

    When i run program below on turbo c compiler,i got the output but i can't get out of the output screen,seems like while loop is not ending due to some reason..............
    Code:
    #include<stdio.h>       
    #include<conio.h>
    void main()
    {
     int c;
     while((c=getchar())!=EOF)
     {
      putchar(c);
     }
     getch();
    }
    Please tell me friends why is this happening and it's solution?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    You need to somehow signal an "end of file" from the keyboard to cause getchar() to return EOF, after which the loop will terminate. Under windows that is achieved by hitting CTRL-Z (on the keyboard holding the CTRL key and the Z key simultaneously). It is different with other operating systems (eg CTRL-D under most flavours of unix).

    Also, main() returns int, not void. Even if your compiler supports void main(), it is better to write int main(), since that is what the C standard specifies.

    The <conio.h> header and the getch() are also non-standard.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Thank you grumpy for clearing my doubt.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    As was mentioned CTRL-Z and CTRL-D are the most common methods of signalling EOF from the keyboard. Another way to signal EOF is to do so implicitly: run the program with input from a file using a redirection operator. If your program is called foo and you have an input file called bar, then you can run

    foo < bar

    Then the loop will terminate after the n characters in bar have been read, where n is the character count* of the file

    *technically C standard-io library does some internal conversion of newline characters, so the character count might differ slightly from the filesize on non-UNIX systems.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    I wonder why people use getch() at the end of a program. I guess its to avoid that the terminal window disappears at the end. If you want to do it using only standard functions, then here is an alternative:

    Code:
    printf("Press enter to end the program...");
    getchar();
    exit(0);
    If you want put this in a function called EndProgram or something and then re-use it in your examples

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Thank you,but please tell me how can i run foo<bar(program<input fille) in turbo c compiler.Where for running a program i click on run(or press ctrl+f9).So exactly what i'd have to do for running program<input file in this compiler.I am a novice to c language.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    If you are running from the command-line, just type
    Code:
    foo < bar
    Which means to run the program foo with bar as input.

    If you're using an IDE like Turbo C then there may be a configuration option for it. You'll have to check the menus, help files, etc.

    EDIT:
    Even if you like to use an IDE, most computers nowadays have a feature where you can open a command prompt in a separate window on the screen, so that you can switch back and forth between your IDE (Turbo C) and the command prompt window without ending either of these programs. This is referred to as "multi-tasking" and is not normally available in systems for which Turbo C was developed (DOS). Or maybe they have a newer version? I don't know...
    Last edited by c99tutorial; 12-13-2012 at 02:56 AM.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Thank you,I never used command line for running c programs....well, Would you please tell me some books for c language.I have just read a book of Yashvant Kanetkar and now studying "The C Programming Language" by Brian W.K. and Dennis Ritchie.I am novice to this language now, but want to be expert in it.Please guide me for that,...it'd be very helpful for me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple output/input program not working?
    By tmac619619 in forum C Programming
    Replies: 3
    Last Post: 10-07-2012, 10:31 PM
  2. Replies: 3
    Last Post: 04-27-2011, 09:46 PM
  3. Replies: 5
    Last Post: 04-25-2011, 01:12 PM
  4. Simple copy program hangs?
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 07-24-2004, 05:00 PM
  5. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 01:57 PM

Tags for this Thread


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