Jump to content
  • Advertisement
Sign in to follow this  
reallycoolme

ifstream visual studio

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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!