Question 2. File I/O [6]
Suppose we are working with text files containing student numbers, where the acceptable file format is exactly one student number per line, where each student number is 9 digits long.
E.g. 3 files with valid content | E.g. 4 files with invalid content |
Write a function that takes a filename as a parameter (string or character array, your choice) and checks whether or not the file conforms to the format specified. (I.e. open the file, read it, determine if it matches the specifications, and close it.) The function should return true if the file is readable and appropriately formatted, false otherwise. The function should not produce any output, and should not modify the file contents in any way.
SAMPLE SOLUTION 1 bool checkFile(char fname[]) { // try to open the file ifstream fpin; fpin.open(fname); if (fpin.fail()) return false; // read lines one at a time until end of file while (!fpin.eof()) { // try reading the start of the next line of the file char c; fpin.get(c); // if we are at the end of the file then we // did not encounter any invalid content if (fpin.eof()) { fpin.close(); return true; } // otherwise we should get 9 digits with no errors int digitCount = 0; do { // make sure the line doesn't contain invalid characters if (isdigit(c)) digitCount++; else { fpin.close(); return false; } // get the next character fpin.get(c); // if we hit eof before 9 digits then this line is incomplete if ((digitCount < 9) && (fpin.eof()) { fpin.close(); return false; } } while (digitCount < 9); // if we are at eof then the last line was complete, // otherwise the current character should be the // newline marking the end of the line if (fpin.eof()) { fpin.close(); return true; } if (c != '\n') { fpin.close(); return false; } } } | SAMPLE SOLUTION 2 bool checkFile(string fname) { // try to open the file ifstream fpin; fpin.open(fname.c_str()); if (fpin.fail()) return false; // read lines one at a time until end of file while (!fpin.eof()) { string line; getline(fpin, line); if (!fpin.eof()) { // make sure the line has the right length if (line.length() != 9) { fpin.close(); return false; } // make sure the contents are all digits for (int i = 0; i < 9; i++) { if (!isdigit(line[i])) { fpin.close(); return false; } } } } // close and quit fpin.close(); return true; } |