c++ - ostream << istream and testing EOF on an empty file, alternatives for getting characters from istream -


this first post on stackoverflow, if i'm doing wrong, please let me know. have final exam intro c++ programming on wednesday next week, , have no way of checking answers prof's practice questions. i'm concerned checking see if input file empty before copying it's contents output file. also, grabbing characters input file. here's question , code below:

suppose have following enumerated type list possible file i/o errors:

enum fileerror {    nofileerror,       // no error detected    openinputerror,    // error opening file input    openoutputerror,   // error opening file output    unexpectedfileend, // reached end-of-file @ unexpected spot in program    emptyfileerror,    // file contained no data }; 

provide appropriate implementations 3 following file handling routines:

fileerror openinputfile(ifstream& infile, char *filename); // open named file input, , return opening status  fileerror openoutputfile(ofstream& outfile, char *filename); // open named file output, , return opening status  fileerror copynchars(ifstream& infile, ofstream& outfile, int numchars); // check ensure 2 files open, //    copy numchars characters input file output file 

now i'm concerned last function listed here. here's code:

fileerror copynchars(ifstream& infile, ofstream& outfile, int numchars){     char c;     if (!infile.is_open()) return 1;     if (!outfile.is_open()) return 2;     if ((infile.peek()) == -1) return 4; //this right? (i'm using linux g++ compiler.     // also, can return ints enum types?     (int = 0; < numchars; i++){         if (infile.eof()) return 3;         else {             infile.get(c); //is way this?  or there recommendation?             outfile << c;         }     } } 

i've looked @ various methods checking eof before reading, haven't found specific answer -1 or eof being valid check (similar null???). think it's unfamiliarity terminology, because i've looked on documentation , can't find example of kind of check. doing empty file check correctly here? don't have driver written test code out. also, concerned method i'm using. there alternatives in situation, , what's best way 1 character @ time. lastly, allowed ask speculative questions (like "what various methods getting , what's best in situation?") on stack overflow. thank time , consideration.

check cplusplus.com. has examples of using ifstream: http://www.cplusplus.com/reference/fstream/ifstream/

particularly, might want check out get() function has no parameters. returns eof if eof hit. also, ifstream has eof() function tell if eof bit set. also, don't know if check of return value of peek() guaranteed. cstdio defines eof macro, -1, don't think guaranteed language.

also, rather returning integer values, return enum literals. there for.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -