Hey guys! I'm a beginner to c++ and I want to know how I would be able to have a read or write function to a separate file.
I'm creating a launcher for minecraft server which has the ability to edit the config through the console.
I had to throw it all onto one line as I had no clue how to do otherwise, it works but I'm sure there is a neater way such as header files, but I don't know how to use those! hah!
Anyway I just wanted to know if I could get a line from the text file "server.properties" and then have the ability to edit a part of it like:
"hardcore=false"
It will give me the option to change that to true.
Here is the server.properties.
The ability to add custom lines of code to the server.properties at the end of the file would be nice too.
There are several ways to go about this, you could put the properties
in a 3d array but I think the simplest is to simply dump out the file with
line numbers, give the user a choice of which line to edit. Let them type
in their string, then write the file replacing line X with their string.
OK, thank you! It's finally able to print out the lines, now how easy would it be to replace a line, for example. If I chose line 30 how easy would it be to edit it?
EDIT: I meant to say, what functions will I be using as I don't have the faintest idea on how to even begin replacing the lines.
//main.cpp (example of use)
#include <iostream>
#include "MCServerConfig.h"
using std::string;
int main()
{
MCServerProperties config;
string input;
//Open config file
std::cout << "Enter the name of the config file: ";
getline(std::cin, input);
try {
config.load(input);
} catch(std::ios_base::failure x) {
std::cerr << x.what();
return -1;
}
//Allows user to read random parameters
std::cout << "Enter name of the parameter to see its value,\n" <<
"\"!\" to exit" << std::endl;
while(getline(std::cin, input)) {
if(input == "!")
break;
config.read(input);
}
//Saving by different name
config.save("save.txt");
return 0;
}