Sign in to follow this  

[stl] failing to read file o.0

Recommended Posts

okay ive got the following chunk of code:
std::ifstream in(args[5], ios::binary);
	//in.seekg (0, ios::beg);
	if (!in.is_open())
		cout << "Failed to open " << args[5] << endl;
		return 0;
	char buffer[256];
	int bytes = in.readsome(buffer, 256);

so it manages to open the file, which is a file with some text in it. Now, in.readsome returns 0 like wtf?!

Share this post

Link to post
Share on other sites
std::basic_ifstream::readsome reads from a stream's stream buffer only, that means what's been buffered into memory and available already so in this case since you're calling it on a freshly opened file input stream obviously the buffer is empty hence returns zero bytes read from the buffer. Use std::basic_ifstream::read for the initialize read at the very least.

Another thing to realize is that these operations are unformatted I/O, are you sure you really want to do this for a text file rather than formatted I/O.

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