Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,208 of 33,346    |
|    =?UTF-8?B?RGFuaWVsIEtyw7xnbGVy?= to Helmut Jarausch    |
|    Re: Recommend method to read from a poss    |
|    27 Apr 12 11:28:50    |
   
   From: daniel.kruegler@googlemail.com   
      
   On 2012-04-26 23:24, Helmut Jarausch wrote:   
   > is there a recommend way to read from an ifstream if this is possibly   
   > empty?   
   >   
   > The simple way   
   >   
   > std::ifstream Inp("Empty");   
   >   
   > if ( ! Inp ) {   
   > cerr<< "Open failed\n";   
   > return 1;   
   > }   
   >   
   > int V;   
   >   
   > while (Inp) {   
   > cerr<< "--- here with while ---\n";   
   > Inp>> V;   
   > }   
   >   
   > is invalid, since it enters the while loop at least once (for an empty   
   > file)   
   > The working alternative   
   >   
   > while (Inp) {   
   > if ( ! Inp ) break;   
   > ....   
   > }   
   >   
   > is ugly.   
   >   
   > Is there a recommended, readable way to do this?   
      
   I don't think that there exists a universally satisfactory solution to   
   your problem, especially because your question is not really   
   well-defined (to me).   
      
   A fundamental problem in your question is that files are typically   
   "volatile" in nature, so your test may succeed, but you still could try   
   to read a no-longer existing, no-longer empty, or no-longer accessible file.   
      
   In addition to that it would not much help if you find out that a file   
   is not "empty", because it may turn out that it has a "space capacity"   
   of two byte, but still not being able to contain an int value (which may   
   be larger).   
      
   Even, if the capacity for an int value might be OK, this does not   
   guarantee that you could succeed in attempting to read an int value,   
   because the file content might not be reasonably read as an int value.   
      
   I'm saying all of this in advance, because I consider it as a   
   questionable idea that you could get rid of failure-tolerance during   
   file content accesses by performing an initial "empty" test. Again, the   
   definition of "empty" is missing in your question and might not be   
   helpful here anyway.   
      
   If you want to test for a file size as an initial test, I recommend to   
   look at the Boost Filesystem library, which provides a lot of useful   
   functions in the realm of files, including those to determine a file size.   
      
   You could also try to use an approach based on tellg and seekg, but be   
   warned that this seemingly "portable" approach does not guarantee to   
   return the real file size as defined by the file system. The rough   
   approach works like this:   
      
   #include
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca