Standard copy in C and C++
A standard simple program in a Unix environment is to copy
the standard input to the standard output.
Here is a standard simple version in the C language:
Copy Program in C |
Copy Program in C++ |
/* copy.c: simplest C copy program
* copies stdin to stdout
*/
#include <stdio.h>
int main(void) {
int ch;
while ((ch = getchar()) != EOF)
putchar(ch);
}
-------------------------------------------------
% lint copy.c # try out lint
(9) warning:Function has no return statement:main
function falls off bottom without returning value
(9) main
function returns value which is always ignored
putchar
% cc -o copy copy.c
% copy # copy: terminal to terminal
Now is the time # input echoed on next line
Now is the time
for all good men ... # ditto
for all good men ...
# ctrl-d entered for end-of-file
% copy < copy.c > copy2.c # use redirection
% cat copy2.c # result of copy
/* copy.c: simplest C copy program
* copies stdin to stdout
*/
#include
int main(void) {
int ch;
while ((ch = getchar()) != EOF)
putchar(ch);
}
% gcc -o copy_gcc copy.c # C compiler
% CC -o copy_CC copy.c # C++ complier
% g++ -o copy_gpp copy.c # C++ complier
# All 3 compilers produce above results
| // copy.cc: simplest C++ copy program
// copies cin to cout
//
#include <iostream.h>
int main() {
char ch;
while (cin.get(ch))
cout.put(ch);
}
-------------------------------------
% CC -o copy_cc copy.cc
% copy_cc
Now is the time
Now is the time
for all good men ...
for all good men ...
# ctrl-d entered for end-of-file
% copy_cc < copy.cc > copy2.cc
% cat copy2.cc
// copy.cc: simplest C++ copy program
// copies cin to cout
//
#include
int main() {
char ch;
while (cin.get(ch))
cout.put(ch);
}
|
The C copy program on the left above also compiles and runs under C++,
but C++ provides a different style, shown on the right above. Note that the C++
function cin.get(ch); uses a C++ reference
parameter, not available in C or in Java, so that after the
call, the value of the next character "pops into" the variable
ch. (In C, one would have to pass the address of
ch, that is &ch, and in Java this
style is not possible.)
Simple copy in Java
The shortest corresponding program in Java is a little more complex.
Sending a character to the standard output is simple and
similar: System.out.print(ch);, but
input requires that one takes an I/O exception condition
into account. Also, the function read()
returns an int. Without the cast
(char)ch below, the program would print the
Ascii integer value, rather than the character.
(This and other programs here were adapted from the Gosling
Java text.)
Copy Program in Java |
// CopyFirst.java: copy standard input to standard output
import java.io.*;
class CopyFirst {
public static void main(String[] args) throws IOException {
int ch;
while ((ch = System.in.read()) != -1)
System.out.print((char)ch);
}
}
--------------------------------------------------------------
% javac CopyFirst.java
% java CopyFirst
Now is the time
Now is the time
for all good men ...
for all good men ...
# ctrl-d entered for end-of-file
% java CopyFirst < CopyFirst.java > CopyFirst2.java
% cat CopyFirst2.java
// CopyFirst.java: copy standard input to standard output
import java.io.*;
class CopyFirst {
public static void main(String[] args) throws IOException {
int ch;
while ((ch = System.in.read()) != -1)
System.out.print((char)ch);
}
}
|
The above program is not good style for input in
Java, since the extra "throws" clause would be
needed anywhere this input code is called from.
For a through discussion of copying in Java, see:
Copy programs in Java.
Line-by-line copy in C:
Here is a C copy program that copies each line separately.
In the Unix world, a text file has to behave as if there were
a single newline character at the end of each record, but nothing
else between. (This is simulated even if it is not the case.)
One complication is that it is legal to leave off the newline
at the end. In order to test for end-of-file, one needs
to read past the final character in the file, and test for
EOF.
Line-by-line Copy Program in C |
/* copy3.c: copy line-by-line
*/
#include
int main(void) {
int ch;
int linenum = 0;
while ((ch = getchar()) != EOF) {
printf("Line %2i:", ++linenum);
while (ch != '\n' && ch != EOF) {
putchar(ch);
ch = getchar();
}
if (ch == EOF) break; /* in case no newline at end */
putchar(ch); /* write the newline */
}
}
--------------------------------------------------------------
% cc -o copy3 copy3.c
% copy3 < copy3.c # direct copy3.c in; output to terminal
Line 1:/* copy3.c: copy line-by-line
Line 2: */
Line 3:#include
Line 4:int main(void) {
Line 5: int ch;
Line 6: int linenum = 0;
Line 7: while ((ch = getchar()) != EOF) {
Line 8: printf("Line %2i:", ++linenum);
Line 9: while (ch != '\n' && ch != EOF) {
Line 10: putchar(ch);
Line 11: ch = getchar();
Line 12: }
Line 13: if (ch == EOF) break; /* in case no newline at end */
Line 14: putchar(ch); /* write the newline */
Line 15: }
Line 16:}
% vi copy3.c (delete newline at end of file copy3.c)
"copy3.c" [Incomplete last line] 16 lines, 381 characters
% copy3 < copy3.c
Line 1:/* copy3.c: copy line-by-line
Line 2: */
Line 3:#include
Line 4:int main(void) {
Line 5: int ch;
Line 6: int linenum = 0;
Line 7: while ((ch = getchar()) != EOF) {
Line 8: printf("Line %2i:", ++linenum);
Line 9: while (ch != '\n' && ch != EOF) {
Line 10: putchar(ch);
Line 11: ch = getchar();
Line 12: }
Line 13: if (ch == EOF) break; /* in case no newline at end */
Line 14: putchar(ch); /* write the newline */
Line 15: }
Line 16:}% # Unix prompt at end; no newline
|
Copyright © 2011, Neal R. Wagner. Permission is granted
to access, download, share, and distribute, as long as this notice remains.