// this program copies n first columns of a matrix, sitting in in file // to out file // usage : brit #include #include #include #include "escanf.h" #include #include void panic (char* str); main (int argc, char* argv[]) { if (argc != 4) panic ("brit <# of columns to leave") ; int number = atoi (argv[3]); ifstream in (argv[1]); ofstream out (argv[2]); if (!in) panic ("No input file. Stop"); // counting columns char *command = new char[100]; sprintf (command, "wc %s > dummy", argv[1]); system (command); ifstream dumm ("dummy"); double nlines , nwords; dumm >> nlines >> nwords; int ncolumns = int (nwords / nlines + 0.5); // cout << "ncolumns = " << ncolumns << endl; // checking if enough columns if (ncolumns < number) panic ("Too few columns. Exit."); int cnt1 ; double variable ; // brit-ting for (int counter = 0 ; counter < int (nlines+0.5) ; counter++ ) { for (cnt1 = 1; cnt1 <= number; cnt1++) { in >> variable ; out << variable << " " ; } while (cnt1++ <= ncolumns) in >> variable; out << endl; } in.close (); out.close(); system ("rm dummy"); } void panic (char* str) { cerr << str << endl ; exit (0); }