• Advertisement
Sign in to follow this  

Crash when reading from a file

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

Hi this piece of code crashes once it gets to the getline function (it prints Test in the console). char* cT; cT ="Test"; cout << cT; file >> cT; cout << cT; Its unlikely that it is the file that causes the error as I can read it to a std::string but I need it as a char* to be able to work with it. I tried copying the data of a std::string into a char* but that also crashes :(

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Temrek
Hi this piece of code crashes once it gets to the getline function (it prints Test in the console).


char* cT;
cT ="Test";

cout << cT;
file >> cT;

cout << cT;


Its unlikely that it is the file that causes the error as I can read it to a std::string but I need it as a char* to be able to work with it.

I tried copying the data of a std::string into a char* but that also crashes :(


std::string uses dynamic allocation. Just using char pointers/arrays like that is asking for trouble.

Make sure the array is big enough, it's only 5 chars now (and cannot be more) since that's what you set it to (1 'T', 2 'e', 3 's', 4 't', 5 '\0').

Why do you need to use char*'s? Can't you use a string and then use char *std::string::c_str()?

Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by Temrek
Hi this piece of code crashes once it gets to the getline function (it prints Test in the console).


char* cT;
cT ="Test";

cout << cT;
file >> cT;

cout << cT;


Its unlikely that it is the file that causes the error as I can read it to a std::string but I need it as a char* to be able to work with it.

I tried copying the data of a std::string into a char* but that also crashes :(


Nononononono.

A char* is a pointer - i.e., not a self-contained object.

Somewhere in your executable are stored the five characters 'T', 'e', 's', 't', '\0' that make up the 'string literal' of "Test". Then you set a pointer to point at this text. That works for output, but you're not supposed to write to those memory locations at all, and it's anyone's guess what is contained beyond those 5 characters.

Using operator >> into a char* behaves as if you have a pointer to a C-style string - that is, it tries to read bytes into the memory location that is pointed at - exactly something you don't want to do.

(Also, this is not "the getline function"; there is a member function of streams called getline(), and also a free function getline(), both of which are actually intended to read a line of text. Reading into a char* - or a std::string for that matter - directly with the operator>> instead reads a *word* of text.)

Redux: Use std::string. It is an object that is appropriate for representing text and actually "owning" the text that it represents. It will work here, whereas char* will not (without significant workarounds that will make any maintenance a huge pain).

You DO NOT "need" the data to be in a char* "to be able to work with it", unless you mean that you want to pass it to some API function that accepts a char*. In face, you will almost certainly find it much easier, safer and less error-prone to work on it as a std::string. In the case that you are passing it to an API function, you can extract a pointer to the std::string's "internal buffer" - which is what the API needs - by using its .c_str() member function. This expects that the API will not try to change the contained text. If it does, please provide more specifics on what exactly you are doing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement