-2

I want store approximate pi number from "approximate" file and use it afterwards, somehow it getting deleted and i can't print it after close method. Here's how i tried to.

string pi;
ifstream piFile;
piFile.open("pi_approximate");

while(piFile){
getline(piFile,pi);

}

piFile.close();
cout << pi << endl;
urbanassa
  • 9
  • 3
  • 1
    Without knowing that file content difficult to say, looks like it has empty line at the end and that what you get in `pi` variable after the loop - last line. – Slava Nov 08 '20 at 19:31
  • File includes 100 digit approximate pi number without trims, also file type is text. @Slava – urbanassa Nov 08 '20 at 19:49
  • This is the C++ equivalent to [why is while(!feof) always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Chris Dodd Nov 08 '20 at 20:28
  • yes, should I delete the post ? – urbanassa Nov 08 '20 at 21:46

1 Answers1

0

This will keep reading from the file until it gets a failed read. If the last character read from the file was a newline then that failed read will be immedaitely after clearing the string pi. So after the loop exits, pi will be empty.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226