C/C++ Programming Pearls


copies one file to another

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void error(const char* p, const char* p2 = " ") { cerr << p << ' ' << p2 << endl; exit(1); } int main(int argc, char **argv) { if(argc != 3) error("wrong number of arguments"); ifstream from(argv[1]); if(!from) error("cannot open input file", argv[1]);
ofstream to(argv[2]); if(!to) error("cannot open output file",argv[2]);
char c; while( from.get(c)) to.put(c); if(!from.eof() || !to) error ("something is not correct"); }
  • cp.cc: C++ stream copy
  • freq.cc: reading a file and using map
  • cp3.c: C very fast file to file copy
  • iovector.c:C++ common read into a vector
  • sio.cc:C++ read/write with streams and files
  Easy way to read in a file.

#include <string>
#include <fstream>
#include <iterator>
#include <sstream>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

map < string, int >freq;

record (const string & s)

print (const pair < const string, int >&r)
  cout << r.first << ' ' << r.second << '\n';

main (int argc, char **argv)
  string infile;

  if (argc == 2)
      infile = argv[1];
      cout << "need filename" << endl;
      return 1;

  ifstream is (infile.c_str ());
  istream_iterator < string > ii (is);
  istream_iterator < string > eos;

  for_each (ii, eos, record);
  for_each (freq.begin (), freq.end (), print);

  cp3.c has the fastest file to file copy time. Below are copy times using the time command on a 257648872 byte text file.
./time ./cp access_log access_log2

real    1m1.090s
user    0m58.678s
sys     0m2.234s

This is the standard bash cp commnad
./time cp access_log access_log3

real    0m9.251s
user    0m0.039s
sys     0m2.473s

./time ./cp3 access_log access_log4

real    0m7.659s
user    0m0.020s
sys     0m2.463s

This had the fastest copy ... note times varied slightly. 
Note, if the same copy was done twice, so that after the 
second copy the file already existed, then, the second
copy from a little slower...by about 6s.

time ./cp3 access_log access_log2

real    0m6.226s
user    0m0.016s
sys     0m2.252s

#include <stdio.h>
#include <fcntl.h>
#define PERMS 0666

void error(char *p)
main(int argc, char **argv)
   int f1,f2;
  register int n;
  char buf[BUFSIZ];

  if (argc != 3)
    error("Usage: cp from to ");

  if((f1 = open(argv[1], O_RDONLY, 0)) == -1)
              error("cp <arg1> <arg2> error: can't open <arg1>");
  if((f2 = creat(argv[2], PERMS)) == -1)
              error("cp <arg1> <arg2> error: can't open <arg2>");
  while((n = read(f1, buf, BUFSIZ)) > 0 )
         if (write(f2,buf, n) != n)
                 error("cp: write error on file ");

  return 0;



#include <iostream>
#include <fstream>
using namespace std;

int main()
  // open file ``example.dat'' for reading and writing
  filebuf buffer;
  ostream output(&buffer);
  istream input(&buffer);
  buffer.open ("example.dat", ios::in | ios::out | ios::trunc);

  for (int i=1; i<=4; i++) {
    // write one line
    output << i << ". line" << endl;

    // print all file contents
    input.seekg(0);          // seek to the beginning
    char c;
    while (input.get(c)) {
    cout << endl;
    input.clear();           // clear  eofbit and  failbit

SourceForge.net Logo