Skip to main content
GameDev.net gamedev.net
🔒 Locked

Text file contents with C++

Started by Black Knight Sep 22, 2009 at 8:12 PM 5 replies 1.9k views
Original Post
Black Knight
Black Knight
I need to read a text file(GLSL shader) and pass it to glCompileShader but my current file reading method doesn't work as it returns a std::string.And all my attempts of converting it to a char array failed.It usually doesn't read new lines and doesn't compile etc. Here is the c++ code :

std::string STUtil::fileToString(const std::string& filename)
	{
		//open file for reading
		std::fstream file(filename.c_str(),std::ios::in);
		
		//throw exception if we fail
		if(!file)
			throw std::ios::failure("file not found");
		
		//read all the file
		std::stringstream buffer; 
		buffer << file.rdbuf();
		std::string content(buffer.str()); 
		//return the string
		return content;
	}

I can go with something like :

FILE *fp;
		char *content = NULL;

		int count=0;


			fp = fopen(filename.c_str(),"rt");
			if (fp != NULL) 
			{
				fseek(fp, 0, SEEK_END);
				count = ftell(fp);
				rewind(fp);
				if (count > 0) 
				{
					content = new char [count+1];
					count = fread(content,sizeof(char),count,fp);
					content[count] = '\0';
				}
				fclose(fp);
			}
		
		return content;

But I'm wondering if it could be done with c++ streams.
Black Knight
Black Knight
After some more struggling I managed to make it work.I read line by line copy to a char vector and then copy to a regular char array.I am open for simpler ways of doing it :)

char* Shader::readFile(const std::string& filename) 	{		//open file for reading		std::fstream file(filename.c_str(),std::ios::in);		//throw exception if we fail		if(!file)			throw std::ios::failure("file not found");						char* content= 0;				int count = 0;				file.seekg(0,std::ios::end);		count = file.tellg();		file.seekg(0,std::ios::beg);				if(count > 0)		{			std::vector<char> charVector;			std::string line;			while(getline(file,line))			{				std::copy(line.begin(),line.end(),back_inserter(charVector));				charVector.push_back('\n');			}			content = new char[charVector.size()+1];						std::copy(charVector.begin(),charVector.end(),content);			content[charVector.size()] = '\0';		}					return content;	}
KulSeran
KulSeran
something like this should work:
  ifstream is;  is.open (filename.c_str(), ios::binary );  // get length of file:  is.seekg (0, std::ios::end);  std::string::size_type length = is.tellg();  is.seekg (0, std::ios::beg);  std::string buffer;  buffer.reserve( length );  std::copy( istreambuf_iterator<char>(is.rdbuf()), istreambuf_iterator<char>(), std::back_inserter( buffer ) );

*untested, but should work...
Erik Rufelt
Erik Rufelt
Open the file with std::ios::binary, to simply read all the bytes.
Black Knight
Black Knight
Haha cheers mate,why didn't I think about that before.fread was doing just that silly me [smile]

char* Shader::readFile(const std::string& filename) 	{		//open file for reading		std::fstream file(filename.c_str(),std::ios::in|std::ios::binary);		//throw exception if we fail		if(!file)			throw std::ios::failure("file not found");						char* content= 0;		int count = 0;				//get file size		file.seekg(0,std::ios::end);		count = file.tellg();		file.seekg(0,std::ios::beg);				//read file		content = new char[count+1];		file.read(content,count);		content[count] = '\0';				return content;	}


Much better now [smile]
dilyan_rusev
dilyan_rusev
I am not sure I miss something. As I understand it, you need to convert std::string to char*. Doesn't the standard std::string::c_str work for you?
Trienco
Trienco
The only potential issue with string.c_str() is that it returns a const char*. But then, anyone writing a library function that only reads the passed text and yet makes the parameter a non-const char* should be hit over the head with his keyboard (speaking from experience and finding it very annoying to be forced to copy string to char-array, just because some people are too damn lazy to use const).
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.