file handling

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Lorth
Posts: 139
Joined: July 19th, 2012, 4:12 pm

file handling

Post by Lorth » November 28th, 2012, 2:37 pm

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 :lol:

grizzly121
Posts: 1
Joined: November 26th, 2012, 3:53 pm

Re: file handling

Post by grizzly121 » November 28th, 2012, 2:50 pm

Well, Chilli did that in Beginner's lessons.Lesson 18 or 19, I don't remember.

Lorth
Posts: 139
Joined: July 19th, 2012, 4:12 pm

Re: file handling

Post by Lorth » November 28th, 2012, 3:00 pm

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. :lol:

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 :P

Lorth
Posts: 139
Joined: July 19th, 2012, 4:12 pm

Re: file handling

Post by Lorth » November 28th, 2012, 11:03 pm

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 ;)

Post Reply