# [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];


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

##### Share on other sites
I'm not used to C++ file I/O, but isn't it in.read()?

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628391
• Total Posts
2982419

• 10
• 9
• 19
• 24
• 11