Hello everyone
I am new to the forum
I have a question. I created a method foo(ifstream &myfile)
and works only if executed ones. In a loop or consequtively calls make the cerr call

int foo( ifstream &infile){
    
    
     if (!infile) {
     cerr << "Unable to open file ";
     return -2;
    // exit(1);   // call system to stop
     } else{
       while(infile>>x){  
                   do something
                 
                   }
        
       }
      
       return matrix[row][column];
     }

Recommended Answers

All 2 Replies

if I do foo(myfile) then it will returned a value otherwise successive calls will make the cerr call

When you call the function a second time, without having reset the filestream you pass to it, your filestream is in a bad state. You read it to its end in the first iteration of the function, so there's nothing more you can do with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.