fstream

why is this not loading details from file


//container implementation
void itemcontainer::loadsItemDetailsFromFile(char* file_name){
ifstream myin(file_name);
long id;
string name;
float cost;
int quantity;

if(myin.is_open()){
for(;;){
myin>>id;
myin.ignore(1);
getline(myin>>ws, name,',');
myin>>cost;
myin>>quantity;
if(myin.good()){
item temp(id,name,cost,quantity);
itemlist[count]= temp;
++count;
}else if(myin.eof()){
//reached end of file
break;
}else{
cout<<"some error occurred while reading, probably file is corrupted"<<endl;
break;
}
}

}else{
cout<<"ERROR: failed to load information from file!!!"<<endl;
}
myin.close();
}
How do you know it's not?
Hello Ruchina,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


In addition to Repeater's question: How do you know the file stream is good?

There is not enough code there to compile and test. Also you may think the problem is in the function, but it could have started somewhere else.

With code that can compile and be run you also need to include the input file of a fair sample to work with.

Hope that helps,

Andy
Topic archived. No new replies allowed.