Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

KurtCPP

Files I/O operations in C++.

This topic is 5332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi every-1. I''m currently trying to figure out how the C++ fstream classes (both I and O) work. I''ve been taught everywhere the gcount() method returned the number of characters extracted during the last operation. But does it really return the count of characters actually read of asked?? If if write fin.read(buffer,100), but less than 100 char are actually present in the file, for example, should gcount() return 100 or ''less than 100''?? Then how can I count the chars in a file. I''ve been trying something like the following but the loop never ends :
int nCharCount = 1; //the null character at least

char *buffer = null;
while(!fin.eof()){
	fin.peek();
	nCharCount++;
   }
buffer = new char[nCharCount];
// this latter statement never gets reached, but WTH ???

Prog, Hex & Rock''n''Roll : I don''t like the Prog but the Prog likes me. Some nice poetry to sweeten your spirit and relax a bit before programming

Share this post


Link to post
Share on other sites
Advertisement
.peek() doesn''t advance the position in the stream, which is why your loop doesn''t exit. What happens is

1) fin.eof() is false
2) fin.peek() reads the first character and returns that value, which subsequently is discarded because it''s not assigned anywhere.
3) nCharCount is incremented
4) goto 1.

If the general problem you have is, as it seems, "I want to read a file completely into memory, but I don''t know ahead of time how much memory to allocate", then - FFS, use a Vector. It will make your life easier

Share this post


Link to post
Share on other sites
I would think you could do std::distance((std::istreambuf_iterator<char>(myIfstream)), std::istreambuf_iterator<char>());.

Share this post


Link to post
Share on other sites
Thank you all buddies. I''ll try to keep simple for now, so the basic fstream''s input functions should do. Templates will wait a bit more (at least until I''ve parsed the new GameDev''s templates article). I think seekg and tellg will do it. :-)

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming

Share this post


Link to post
Share on other sites
seekg and tellg cannot do it portably, I believe, as there need be no conversion between a streamsize and an integral type.

The method I described -- which requires reading every byte of the file -- is the one you must use.


[edited by - DrPizza on January 13, 2004 10:18:34 AM]

Share this post


Link to post
Share on other sites
Not really, I''ve seen a third party standard library implementation where the streamsize was a class that wrapped 64 bit artihmetic on a compiler that didn''t have a primitive type for 64 bit integers.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!