[prog] stream eof problem
Jennifer Davis
davi0302 at algonquinc.on.ca
Tue Mar 4 12:04:36 EST 2003
Hi:
I am working on a coding assignment for class and I am really
stumped on reading an end of file marker. When I run part of the program,
I get stuck on the last part of the read. Instead of finding an EOF
marker, the program assumes an error, clears the error and does it again.
I am not used to being stumped as I usually understand the texts, and show up
and pay attention in class.
I would so appreciate a review of the code. I will put up the
text file I used for input on my website. http://www.jenn.ca/file1.txt.
What makes this most confusing is that when I use the bloodshed compiler
for windows, it seems to work. I use the g++ compiler for Linux.
bash-2.05a$ g++ -v
Reading specs from /usr/lib/gcc-lib/i386-slackware-linux/2.95.3/specs
gcc version 2.95.3 20010315 (release)
Thanks
Jennifer Davis
davi0302 at algonquincollege.com
#include<iostream.h>
#include<fstream.h>
#include<string.h>
struct ITEM {
char item_code[8];
int quantity_on_hand;
float unit_selling_price;
};
void getFileName(char IF[] ,char OF[]);
void main(){
// variables
char inputFile[256], outputFile[256];
char exit, clearChar;
int filesInputted=0,badRead=0;
ITEM product[100];
getFileName(inputFile,outputFile);
ifstream inFile; //These two lines create
inFile.open(inputFile); //\ and open the input file.
if (inFile.good()){
while (!inFile.eof()){
inFile >> product[filesInputted].item_code;
if (!inFile.fail()){ // outer
inFile >> product[filesInputted].quantity_on_hand;
if (!inFile.fail()){ // middle
inFile >> product[filesInputted].unit_selling_price;
if (!inFile.fail()){ // inner
filesInputted++;
cout << filesInputted << " ";
} //endif - inner
}// endif - middle
} else { //endif - outer
inFile.clear();
do
clearChar=inFile.get();
while (clearChar!='\n');
} // end if - else
cout << endl;
}// end while
}// end if part of if-else
else{
cout << "Cannot open file " << inFile << endl;
cout << "Please try again!" << endl << endl;
} // end if-else
} // end main
void getFileName(char fInputFile[], char fOutputFile[]){
cout << "Please enter an input file name: ";
cin >> fInputFile;
//cout << "Please enter an output file name: ";
//cin >> fOutputFile;
} // end getFileName
More information about the Programming
mailing list