/* * Program to read a text file and copy the contents to another file * after printing it to the screen. Demonstrates arrays are passed by * reference and structs are passed by value. Check in gdb. */ #include #include #include using namespace std; int saveRecToFile(struct finfo rec); int saveArrToFile(struct finfo *, int); const int MAX_REC = 100; struct finfo{ string name; int codesize, dycount; }; int main() { int index, num; char ch; string str; struct finfo fRecord[MAX_REC]; /* open the input file */ ifstream ifile("tfile.txt"); if(!ifile){ cout << "File tfile.txt cannot be opened for reading\n"; exit(-1); } /* Read all file records into the structure */ index = 0; while(ifile.peek() != EOF){ ifile >> str; // reads until whitespace cout << str; ifile >> str; // read function name cout << str; fRecord[index].name = str; ifile >> str; cout << str; ifile >> num; // read codesize cout << num; fRecord[index].codesize = num; ifile >> str; cout << str; ifile >> str; cout << str; ifile >> num; // read Dy. Count cout << num; fRecord[index].dycount = num; ifile.get(ch); cout << ch; index++; } ifile.close(); /* Save the structure to another file */ for(int i=0 ; i