Jump to content
  • Advertisement
Sign in to follow this  
derek7

I surprise why not get error.

This topic is 4824 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

#include <fstream>
#include <vector>
#include <iostream> 
#include <windows.h>
 
main()
{	
 	std::ifstream infile("coast.raw", std::ios_base::binary) ;
	std::vector<BYTE> in (14096);
	infile.read((char*)&in[0], in.size());
	for (int i=0;i<in.size();i++)
	std::cout<<(int)in<<" ";
  
}

and the coast.raw just have 4096 pixel,but I read 14096.so why the read() not give a error instead of fill 0 for inexistence data.

Share this post


Link to post
Share on other sites
Advertisement
Maybe you are just reading past that file and just whatever is on the harddisk after it. Though I'm not certain as I've never tried to do that.

Share this post


Link to post
Share on other sites
What makes you think that it actually reads that many bytes? You do not check for the number of read bytes. The size of the vector IS 14096 after all. It is declared thusly, and is not changed by a read operation.

Share this post


Link to post
Share on other sites
You get an error. Try adding:

if (infile.fail ())
{
std::cout << "Failed!";
}


and you'll see.

But the error is non-fatal, so it doesn't crash.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kippesoep
You get an error. Try adding:
*** Source Snippet Removed ***
and you'll see.

But the error is non-fatal, so it doesn't crash.


the if(!infile){...} the same as the if(infile.fail()){...}?

Share this post


Link to post
Share on other sites
Read does give you an error. You're just not checking for it. Read the other replies again. Furthermore infile is an std::ifstream and hence cannot be used for null checks. And certainly, even if infile was a pointer, it would not be set to null when the read goes beyond eof.

Share this post


Link to post
Share on other sites
Quote:
Original post by tomlu709
Read does give you an error. You're just not checking for it. Read the other replies again. Furthermore infile is an std::ifstream and hence cannot be used for null checks. And certainly, even if infile was a pointer, it would not be set to null when the read goes beyond eof.


Actually, std::basic_ios defines operator!, which returns whether the stream is bad or not.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!