fileCopy.c
#include <stdio.h>
#include <string.h>
#define bufSize 1024
int main(int argc, char *argv[])
{
FILE *ifp, *ofp;
char buf[bufSize];
if (argc != 3)
{
fprintf(stderr,
"Usage: %s <soure-file> <target-file>\n", argv[0]);
return 1;
}
if ((ifp = fopen(argv[1], "rb")) == NULL)
{ /* Open source file. */
perror("fopen source-file");
return 1;
}
if ((ofp = fopen(argv[2], "wb")) == NULL)
{ /* Open target file. */
perror("fopen target-file");
return 1;
}
while (fgets(buf, sizeof(buf), ifp) != NULL)
{ /* While we don't reach the end of source. */
/* Read characters from source file to fill buffer. */
/* Write characters read to target file. */
fwrite(buf, sizeof(char), strlen(buf), ofp);
}
fclose(ifp);
fclose(ofp);
return 0;
}
This code was developed by me, G. Samaras.
Same code for C++.
#include <iostream>
#include <fstream>
#include <cstdlib>
int main ( int argc, char *argv[] )
{
if ( argc != 3 ) {
std::cerr<<"Usage: %s <readfile1> <writefile2>\n";
std::exit ( EXIT_FAILURE );
}
std::ifstream in ( argv[1] );
if ( !in.is_open() ) {
std::cerr<<"Error opening input file\n";
std::exit ( EXIT_FAILURE );
}
std::ofstream out ( argv[2] );
if ( !out.is_open() ) {
std::cerr<<"Error opening output file\n";
std::exit ( EXIT_FAILURE );
}
char ch;
while ( in.get ( ch ) )
out.put ( ch );
}
Have questions about this code? Comments? Did you find a bug? Let me know! ![]()
Page created by G. (George) Samaras (DIT)
Pingback: programme, prime factorisation