Sunday, December 6, 2009

My copy command

This can only handle a line of 1500 characters...

#include
#include
#define LINESIZE 1500+1

int main(int argc, char *argv[]) {
FILE *src;
FILE *dest;
src = fopen (argv[1], "r");
dest = fopen (argv[2], "w");
char sf[LINESIZE];

if (src) {
while (1 == fscanf(src, "%[^\n]\n", sf)) {
sf[strlen(sf)+1] = '\0';
fprintf(dest, "%s\n", sf);
}
fclose(src);
}
else {
printf("Cannot open source file.\n");
}
fclose(dest);
return 0;
}

Printing a file backwards

int main(){
Student S;
fstream fback("std.bin",ios::in| ios::binary);
fback.seekg(-(sizeof(Student)), ios::end);
while (!fback.read((char *)&S, sizeof(Student)).fail()) {
cout << S << endl;
fback.seekg(-2 * (sizeof(Student)), ios::cur);
}
return 0;
}