7 Replies - 472 Views - Last Post: 05 August 2014 - 07:13 AM Rate Topic: -----

#1 Fatov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-May 14

Filestream problem

Posted 04 August 2014 - 01:06 AM

Hello,
why this doesent work ?

char *	file::get_text(char const * path)
{
	std::ifstream ifs = std::ifstream(path, std::ios::in | std::ios::binary);
	ifs.seekg(std::ios::end, 0);
	std::size_t const length = ifs.tellg();
	char * text = new char[length];
	ifs.seekg(std::ios::beg, 0);
	ifs.read(text, length);
	return text;
}
void	file::set_text(char const * path, char * text)
{
	std::ofstream ofs = std::ofstream(path, std::ios::out, std::ios::binary);
	ofs.write(text, std::strlen(text));
}


And is there a better method to get the length of the text ?
Becaus if there would be a very big text the mehtod iterate over all chars and that only to get the length...

Is This A Good Question/Topic? 0
  • +

Replies To: Filestream problem

#2 Fatov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-May 14

Re: Filestream problem

Posted 04 August 2014 - 01:30 AM

Ah, I found the mistake.
I exchanged the two seekg arguments.
But now at the end of the text are so crazy characters.
Why ?
Posted Image
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Filestream problem

Posted 04 August 2014 - 05:31 AM

The problem is that you are forgetting that the in memory representation of C style strings in C and C++ require the use of a null terminator.
Was This Post Helpful? 0
  • +
  • -

#4 Fatov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-May 14

Re: Filestream problem

Posted 04 August 2014 - 06:09 AM

Thanks now, it works.
Would you improve something ?

char *	file::get_text(char const * path)
{
	std::ifstream ifs = std::ifstream(path, std::ios::in | std::ios::binary);
	ifs.seekg(0, std::ios::end);
	std::size_t const length = ifs.tellg();
	char * text = new char[length + 1];
	ifs.seekg(0, std::ios::beg);
	ifs.read(text, length);
	text[length] = '\0';
	return text;
}
void	file::set_text(char const * path, char * text)
{
	std::ofstream ofs = std::ofstream(path, std::ios::out, std::ios::binary);
	ofs.write(text, std::strlen(text));
}

Was This Post Helpful? 0
  • +
  • -

#5 Fatov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-May 14

Re: Filestream problem

Posted 04 August 2014 - 09:55 AM

Hi,
I have another problem,
why I get this error:

Quote

invalid operands of types 'const char [35]' and 'const char* const' to binary 'operator<<'
return "Failed to open file stream \"" << path << "\" !";

To this function in my own exception:
const char * my_exception::what() const throw()
{
	return "Failed to open file stream \"" << path << "\" !";
}


path is a memeber:
char const * path;


And another question, how I can cout a GLenum.
If I try cout<<my_gl_enum_variable; I get a error.

This post has been edited by Fatov: 04 August 2014 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,460
  • Joined: 25-December 09

Re: Filestream problem

Posted 04 August 2014 - 11:09 AM

The message is telling you that path is not a const char pointer, it is a const char[35]. Why are you trying to throw an exception for a non-exceptional case?

Also why are you trying to use binary mode to read and write this information? And if you do stick with the binary file you should really write then read the same number of bytes, use a constant for the size.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 Fatov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-May 14

Re: Filestream problem

Posted 05 August 2014 - 01:30 AM

I throw a exception, because in the case it failed I can in the console exactly see, where this error occurred.
And I don't like a constant size, because I want load glsl shaders, and the shaders size can varygate.

Quote

The message is telling you that path is not a const char pointer, it is a const char[35]


hä,
but path is a const char pointer ...

Quote

path is a member:

char const * path;

What I don't understand ?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,460
  • Joined: 25-December 09

Re: Filestream problem

Posted 05 August 2014 - 07:13 AM

I misread the error message:

Quote

invalid operands of types 'const char [35]' and 'const char* const' to binary 'operator<<'
return "Failed to open file stream \"" << path << "\" !";

I've highlighted the important part of the message above. Look at the code that generates the error:
const char * my_exception::what() const throw()
{
	return "Failed to open file stream \"" << path << "\" !";
}



Do you think "Failed to open file stream \"" << path is how you concatenate two C-strings? Remember C-strings are not streams. And don't forget you're trying to return a const char*, that const is very important since you can't return a "local" C-string from a function.

I still recommend you stop using exceptions for this non-exceptional problem. Basic if/else statements are usually all you need. Print the error message to the console or log file and return an error message to the calling function, to let the caller know there was a problem that needs to be dealt with, usually by returning an error message to it's caller until you reach main() which then returns an error code to the operating system.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1