Advertisement Jump to content
Sign in to follow this  
Codorke

StringCbCopy, StringCbCat : Filereading problem

This topic is 4901 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, I wanted to write some basic code for reading in a TXT file. I i used strcpy and strcat my applic had his own personality. He crashed only a few times but, he crashed. So i debugged it and when he crashes he had problems with strcpy and strcat. Now i wanted to use a safer method then strcpy and strcat but there still some bugs. Does someone wants to look at my code and help me out ?

// char* myText;   => private class member of cReaderTXT

// myText = new char[1];
// myText[0] = '\0';      => Initialization

//#define READIN_LINE_SIZE_TXT 256

bool cReaderTXT::leesBestand() {
	fstream bestand(getPath(), ios :: in);
	if(bestand.fail())return false;

	if(!bestand.eof()) {
		char buffer[READIN_LINE_SIZE_TXT];
		bestand.getline(buffer, sizeof(buffer));
		char* temp = new char[1];
		temp[0] = '\0';
		StringCbCopy(temp, sizeof(buffer)+sizeof(char), buffer);
		// strcpy(temp, buffer);
		StringCbCat(myText, sizeof(myText)+sizeof(temp)+sizeof(char), temp);
		// myText = temp;
		while(!bestand.eof()) {
			StringCbCat(myText, sizeof(myText)+sizeof(char)+sizeof(char), "\n");
			// strcat(myText, "\n\0");
			bestand.getline(buffer, sizeof(buffer));
			temp = new char[strlen(buffer)];
			StringCbCopy(temp, sizeof(buffer)+sizeof(char), buffer);
			// strcpy(temp, buffer);
			StringCbCat(myText, sizeof(myText)+sizeof(buffer)+sizeof(char), temp);
			// strcat(myText, temp);
		}
	}else return false;
	bestand.close();
	return true;
}

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Urk. If you're using C++ and fstream you might as well use std::string. Just use std::get_line() each line into a string then you can use string concatantion to put the strings together.

Or you can use a more a straightforward method to read the entire file into a vector.


std::ifstream ifs(getpath());

std::vector<char> data((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));

Share this post


Link to post
Share on other sites
I don't know about StringCbCopy but you are dynamically allocating an array of characters that has only 1 element which looks suspicious to me.

May i suggest a more elegant alternative & preferred method in C++ [grin], use std::string (which is a type alias for std::basic_string) with the free function std::getline remember it is not a member function of std::basic_ifstream its a free-function.

Share this post


Link to post
Share on other sites
C++ provides a standard string class for a reason:
bool cReaderTXT::leesBestand() {
fstream bestand(getPath(), ios :: in);
if(bestand.fail())return false;

string line;
while(getline(bestand, line)) {
myText += line;
}
if (myText == "") return false;
bestand.close();
return true;
}
Where myTest is also a string.

Enigma

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!