Files I/O operations in C++.

Started by
19 comments, last by KurtCPP 20 years, 3 months ago
quote:Original post by daerid
#include <iostream>#include <fstream>#include <string>size_t get_file_length(const std::string& filename){   std::ifstream file(filename.c_str());   std::string fcontents(       std::istream_iterator<char>(file),       std::istream_iterator<char>()       );   return fcontents.length();}


EDIT: Note that this won''t work accurately for binary files.


Dumping it into a string is a waste of time and memory; std::distance() will tell you how far apart the iterators are without recording what''s in the file.

And using istream_iterators is wrong, as they''ll gobble up whitespace. istreambuf_iterators will count everything.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement