Jump to content
  • Advertisement
Sign in to follow this  
royteusink

Read one single line from file...

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

Why does my code not return the value from the file? (Got no errors) It doesn't read my text file 8-) Releam.cfg
127.0.0.1
Source
#include <string>
#include <fstream>

const char* READ_IP_FROM_FILE()
{
	std::ifstream file("Releam.cfg");
	std::string line;
	std::getline(file, line);
        file.close();
	printf("READ_IP_FROM_FILE() = ");
	printf(line.c_str());
	printf("\n");
	return line.c_str();
}

void main()
{
        printf((char*)READ_IP_FROM_FILE());
}
Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
In your READ_IP_FROM_FILE() function, you are reading the data into a std::string and then returning a pointer into that string from the function. However, the destructor of the string gets called after the function returns, meaning that your pointer is pointing into deallocated memory. This is bad. Consider returning the string by value instead of returning the pointer instead.

Share this post


Link to post
Share on other sites
Consider using modern C++, for example (without any error checking)

#include <string>
#include <iostream>
#include <fstream>

std::string READ_IP_FROM_FILE()
{
std::ifstream file("Releam.cfg");
std::string line;
std::getline(file, line);

return line;
}

int main()
{
std::cout << "READ_IP_FROM_FILE() = " << READ_IP_FROM_FILE() << "\n";
}



works

Also, "void main()" is incorrect - in standard C++ the return value of main must be an int.

Share this post


Link to post
Share on other sites
Now i got this and still it doens't work. It must return a CHAR*. The printf is just a temporary output.


std::string READ_IP_FROM_FILE()
{
std::ifstream file("Releam.cfg");
std::string line;
std::getline(file, line);

return line;
}

int RunServer(void)
{
std::string IPCONFIG;
IPCONFIG = READ_IP_FROM_FILE();
printf(IPCONFIG.c_str());
}




[Edited by - royteusink on July 28, 2006 9:56:14 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by royteusink
Now i got this and still it doens't work. It must return a CHAR*. The printf is just a temporary output.

*** Source Snippet Removed ***
Define "doesn't work". It looks fine to me (But then I'm a horrible C file IO heathen)

EDIT: Why on earth are you casting away the const from c_str() to printf()?

Share this post


Link to post
Share on other sites
And you still haven't defined "doesn't work". Is it crashing? Is the file not getting opened? Is nothing being output? Do dust bunnies dance and play on top of your monitor every time the program is run?

Share this post


Link to post
Share on other sites
Quote:
Original post by royteusink
Now i got this and still it doens't work. It must return a CHAR*.


Nonsense. (If you really, really need to interface with someone else's code that expects a char*, see basic marshalling techniques here. If it's your own code, rewrite it to accept a std::string.)

Quote:
The printf is just a temporary output.


That's not an excuse for not using C++ I/O. BTW, regardless of which I/O you use, you should put debugging stuff on stderr, not stdout. BTW 2, using printf() as in the current main() is a security hole: I can send your program to undefined behaviour land (and this sort of thing can sometimes offer an opportunity to smash the stack) by putting '%s' in the cfg file.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!