home bbs files messages ]

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    
   #include    
      
   std::uintmax_t file_size(const char* name) {   
      std::ifstream fs(name, std::ios_base::binary);   
      if (!fs)   
        return 0; // OK?   
      fs.seekg(0, std::ios_base::beg);   
      std::ifstream::pos_type pos1 = fs.tellg();   
      fs.seekg(0, std::ios_base::end);   
      std::ifstream::pos_type pos2 = fs.tellg();   
      const std::ifstream::pos_type err_pos(-1);   
      if (pos1 == err_pos || pos2 == err_pos) {   
        return 0; // OK?   
      }   
      const std::ifstream::off_type offs = pos2 - pos1;   
      if (offs < 0) {   
        return 0; // OK?   
      }   
      return offs;   
   }   
      
   Consider this as a rough estimate, not as an precise file size. There   
   exists a handful of alternative variations of this theme, including   
   those directly querying corresponding properties of the file buffer.   
      
   Now you could define your concept of an "empty" file in terms of such a   
   size estimate threshold, for example. Again, this does not guarantee   
   that your reading attempt will succeed.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca