Sign in to follow this  

ifstream visual studio

Recommended Posts

My ifstream is not cooperating at all. std::ifstream input(slabName,std::ios::in); char *command; input>>command; //error occurs here, says 0xC0000005: Access violation writing location 0x004af430. at this step, there is the error *_Str = _Elem(); // add terminating null character am i missing something? it compiles just fine. could someone show me an ifstream tutorial? i have searched but have not found a single good ifstream tutorial.

Share this post

Link to post
Share on other sites
You need a pointer tutorial. You can't read data into command; it doesn't point anywhere (you have not initialized it).

Use std::string instead of char* for strings in C++. In this case in particular, even if you initialize command properly

char *command = new char[length_of_command_str + 1]; // + 1 for null terminator.

you cannot tell std::ifstream::operator>> what the maximum length is so you open yourself up to buffer overruns and crashes. You could use getline(), but std::string is in general far superior to char* so there's very little excuse not to use it.

This should work:

std::ifstream input(slabName,std::ios::in);
std::string command;
input >> command;

If you need to interop with legacy C API's or API's that expect char* strings, you can call std::string::c_str() which will return a const char*.

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