Hello Hay9,
Since you are learning the same as I am let me cover some things that yo umay not be understanding correctly.
When it comes to file streams the are 3 basic types:
fstream
ifstream
ofstream
|
The "fstream" is like a generic file stream. It will open a file to use, but it is also stupid in that you have to tell it if you plan to use it for input, output or both. It does not work both ways just because you think it can or because you want it to.
Lets say that you want the "fstream" to be used as an input stream you would say:
std::fstream("filename", std::ios::in);
. Sorry in the beginning I got stuck on the
std::ios::in
or
std::ios::out
. There are other ways. More simply I believe you can use
std::fstream("filename", File::in);
or
std::fstream("filename", fstream::in);
, although these I have not used very often.
For the "ifstream" as the "i" implies it is an input stream. You do not need to tell it that you will be using it as an input stream. To use it all you need is
std::ifstream File
.
Now whether you use "fstream" or "ifstream" you need to follow the open with a check, shown later. Otherwise yo could continue with the program as if nothing is wrong you just will not be able to read anything from the file and your program will look as if it does not work.
The "ofstream" the "o" implies output and like the "ifstream" you do not need to tell it that it is for output. With the "ofstream"an also with the "fstream" you do need to tell it other things that are needed like if you want to
truncate the file or
append the file. You do this with the bitwise OR (|) operator. Parts of this page
http://www.cplusplus.com/reference/fstream/ofstream/open/ should help. See the example in the link.
The next thing you may not find in your reading is how you can open a file stream.
Two options are:
1 2 3
|
std::ifstream inFile;
inFile.open("file Name") // <--- Or you could a "std::string" variable here.
|
Or:
std::ifstream inFile("file Name"); // <--- Or you could a "std::string" variable here.
This not only defines the file stream, but also opens the file when constructed. This would be the more preferred method because it is all done at one time, but either way works.
To start with your code would be something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// this instance of the Character class is the "active" object the player is "playing with"
Character PlayerS;
// now I create a Character object vector in the header file
std::vector <Character> PlayerTemp; // <--- Carefull. You are missing the (;).
std::ifstream File("Savefile.txt"); // <--- Defines the file stream and opens the file. Best choice.
if (!inFile)
{
std::cout << "\n File " << std::quoted("Savefile.txt") << " did not open" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". Optional.
return 1; // <--- If in "main" leaves the program. If in a function returns to where it was called from.
}
|
In the if condition there are other ways of checking, but this would be the best choice as it has a better chance of catching a problem than the other ways.
You used
if (File.is_open())
. Works, but a narrow focus. Also to use this you would need an else statement should the if condition be false.
You can use an if/else to run the program, but it is not the best way of programming.
The "return" statement will cause you to leave the program to fix the problem. Returning (0) zero means that the program ended successfully and there was no problem. Any number great than (0) zero is considered a failure and that there was some kind of problem. You can use that number to help determine what went wrong or just to help find it in your code.
You may find these links helpful:
http://www.cplusplus.com/reference/fstream/
http://www.cplusplus.com/reference/fstream/ifstream/
http://www.cplusplus.com/reference/fstream/ofstream/
When I get into the while loop these line confuse and concern me.
1 2 3
|
Character PlayerT;
linebuffer >> *PlayerT.getType() >> *PlayerT.getName();
|
Small thing. Defining the class as "Character" is OK starting the class name with a capital letter. When it comes to the variable name it is better to start all regular variables with a lower case letter. In the end you are free to name your variables any way that you want.
What is generally used is that regular variables start with a lower case letter. Classes and structs start with a capital letter. And variables defined as a constant, with either "constexpr" or "const" are in all capital letters. Now whether you use "camelCase" or make use of the underscore is up to you. Just be consistent in it use.
The same is true when using {}s. There are many different styles to choose form.
https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements
My personal choice is the "Allman" style because it is the easiest to read and work with. You do not have to hunt for the opening {. Mixing styles in the source code can be confusing. Also using blank lines to break up the code helps in reading and finding problems.
The main goal here is to make the code as easy to read as you can. Mostly for your benefit, but also for other to read. The easier the code is to read it will improve the chance of a quicker response.
When it comes to compile time the compiler will ignore any white space, blank lines and comments and not make them part of the compiled program.
It is also a good idea to comment any code that is not easily understood.
I would like to see the rest of your program meaning all the code, other files that make up the program, the class to see what you have done. It is possible that there are other parts not seen that could be improved on or done differently, with less work.
In the end the more you show the more input you can receive.
Andy