Sign in to follow this  
derek7

I surprise why not get error.

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[i]<<" ";
  
}

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this