Hey guys!
Didn't chili go throu something about open up an text file and read something from it? in any lesson?
I want my program to open up an existing text file and read the ip address in the text file and safe it into an int.
The reason i want to do like this is so the user can easily manage which ip address the program will connect to without needing to open the source code to change ip address all the time.
I also want the program to create the text file if it does not exist and just add zeros into it, or maby the computors own ip address if this even is possible
don't really know how to start yet, have worked on the idea how this will work and have watched the intermediate lesson 1 as chili works with some scoreboard into a text file.to get a idea how to get this to work.
anyone that can give some hint and knows if it is possible to do this
file handling
-
- Posts: 1
- Joined: November 26th, 2012, 3:53 pm
Re: file handling
Well, Chilli did that in Beginner's lessons.Lesson 18 or 19, I don't remember.
Re: file handling
feel kinda stupid now...
googled and watched a 2 min youtube clip that made me get the answer on how to make the file and write to it.
so now i only have to get it to load it into my program, and see if i can make it write my own computor address on its own, without me checking it up
googled and watched a 2 min youtube clip that made me get the answer on how to make the file and write to it.
so now i only have to get it to load it into my program, and see if i can make it write my own computor address on its own, without me checking it up
Re: file handling
Hey guys!
I have finally made it work! It works as i thought it would but now i wanna change one thing.
here is the full code for getting the ip address:
void IPAddress(){
fstream file("ServerIPAddress.txt");
if(file.is_open()) {
while(file.good())
{
string address;
getline(file, address);
REMOTE_ADDRESS = address.c_str();
}
file.close();
}
else
{
file.open("ServerIPAddress.txt", ios::out);
if(file.is_open())
{
file<<"\"" "127.0.0.1" "\"";
}
}
file.close(); }
Now the thing i wanna change now is: in my text file it stands "127.0.0.1" and this is the address of the server to connect against. This is quite good, but i want it to only be numbers in the text file.
ex: 127.0.0.1
The problem that happends now is that if i remove the qotation marks, and when i run the program
my client wont find the server as the address is wrong without the qotation marks.
So i somehow need to add a qotation mark before i load my string and the add one in the end of my string when i have loaded the numbers in.
anyone that is maby more experied than i am that got a hint
I have finally made it work! It works as i thought it would but now i wanna change one thing.
here is the full code for getting the ip address:
void IPAddress(){
fstream file("ServerIPAddress.txt");
if(file.is_open()) {
while(file.good())
{
string address;
getline(file, address);
REMOTE_ADDRESS = address.c_str();
}
file.close();
}
else
{
file.open("ServerIPAddress.txt", ios::out);
if(file.is_open())
{
file<<"\"" "127.0.0.1" "\"";
}
}
file.close(); }
Now the thing i wanna change now is: in my text file it stands "127.0.0.1" and this is the address of the server to connect against. This is quite good, but i want it to only be numbers in the text file.
ex: 127.0.0.1
The problem that happends now is that if i remove the qotation marks, and when i run the program
my client wont find the server as the address is wrong without the qotation marks.
So i somehow need to add a qotation mark before i load my string and the add one in the end of my string when i have loaded the numbers in.
anyone that is maby more experied than i am that got a hint