How can I save text files in a container using C++?
I'm having a problem with my program. I want to save a text file in my custom container, but I don't know what the problem is.
#include <iostream> #include <filesystem> #include <vector> #include <string> #include <fstream> #include <algorithm> using namespace std; template <typename Template> class Container { private: Template* Eigenschaft1; size_t Größe; size_t Speichergröße; public: Container(size_t ContainerGröße = 10000) : Größe(0), Speichergröße(ContainerGröße) { Eigenschaft1 = new Template[Speichergröße]; } void Speichern(const Template& Wert) { for (int i = 0; i < Größe; i++) { Eigenschaft1[Größe++] = Wert; } } }; class Personal { private: string Name; string Position; int Alter; double Gehalt; public: Personal(); void Speicherung(string Name, string Position, int Alter, double Gehalt, Container<ofstream>& Container); void Ausgabe(); void Hinzufügen(Container<ofstream>& Container); }; Personal::Personal() { Name = Name; Position = Position; Alter = Alter; Gehalt = Gehalt; } void Personal::Speicherung(string Name, string Position, int Alter, double Gehalt, Container<ofstream>& Container) { string Entscheidung; ofstream Datei1("Datei.csv"); Datei1 << "Name: " << Name << endl; Datei1 << "Position" << Position << endl; Datei1 << "Alter: " << Alter << endl; Datei1 << "Gehalt: " << Gehalt << endl; cout << "M\u00F6chten sie die Person speichern: "; if (Entscheidung == "Ja") { Container.Speichern(Datei1); } else if (Entscheidung == "Nein") { } } void Personal::Ausgabe() { ifstream Datei1("Datei.csv"); getline(Datei1, Name); Datei1 >> Position; Datei1 >> Alter; Datei1 >> Gehalt; cout << "Name: " << Name << endl; cout << "Alter: " << Alter << endl; cout << "Beruf: " << Position << endl; cout << "Gehalt: " << Gehalt << endl; } void Personal::Hinzufügen(Container<ofstream>& Container) { Personal P; cout << "Bitte geben side den Namen ein: "; cin >> P.Name; cout << "Bitte geben sie den Beruf an: "; cin >> P.Position; cout << "Bitte geben sie das Alter der Person an: "; cin >> P.Alter; cout << "Bitte geben sie das Gehalt der Person an: "; cin >> P.Gehalt; Speicherung(P.Name, P.Position, P.Alter, P.Gehalt, Container); } template <typename T> int main() { string Passwort1 { "Erfolg1234" }; string Passwort2; Container<ofstream> Container; Personal P; int Entscheidung; cout << "Geben sie das Passwort ein: "; cin >> Passwort2; if (Passwort2 == Passwort1) { cout << "Wollen sie eine neue Position hinzufügen (1)" << endl; cout << "Wollen sie auf eine Position zugreifen (2)" << endl; cin >> Entscheidung; if (Entscheidung == 1) { P.Hinzufügen(Container); } else if (Entscheidung == 2) { P.Ausgabe(); } } else { cout << "Falsches Passwort" << endl; } }
In my opinion, there are several points in your code that are either measurable or in which the meaning must be questioned.
(1) In general, it would first be good to follow conventional conventions:
2) A few specific code lines I would like to address:
(a) The assignments in your Staff – Constructors don't make sense.
On the left and right you have the same variable. So you overwrite each field with yourself.
It would make more sense if you had a construct that fills your fields with parameters.
Example:
The underscores before the field names are just a common convention to identify private fields. The values are set via an initialization list. Alternatively, such a design definition would also be possible:
(b) In Add to cart -Method does not need new Staff -Instance to be created. Instead, you can use the instance to which the method is already bound.
Consequently, the data do not need to be explicitly Save because this method is bound to the same object and can therefore access the same states.
(c) It is better to declare variables only in the access area where they are actually used. In your main -Method, for example, introduce several variables, although they would only be required if certain conditions occur.
In this way, your program code can also become clearer. On the one hand, it is possible to combine (initialization) one line from time to time with declaration and definition, and it is faster when unused variables are declared. On the other hand, variables can be assigned to their respective context more quickly: if you want to see where eg Decision the search area is reduced. In my above snippet only the if – To be considered hull. For your code, it should be assumed first that the variable in the whole main -Function is intended.
(d) In C++ there are own container classes for arrays or lists. There is no reason to work with C arrays or pointers instead.
Example:
As usual, arrays have a statically fixed size. In contrast, the std::vector -Type a dynamic list. In the example, the latter has been assigned an initial size. For the annexing of new elements (outside the reserved space), push-back method used.
(e) The main -Function cannot be a template.
There is also a lack of specific return. C++ can also handle this implicitly, safer (in terms of functionality on different target platforms) is however an explicit return.
In stdlib as an alternative own macros .
(f) With your loop, you compare values with different data types.
The field has the type size_t while i is an int . Better use only one type ( size_t ).
3) You obviously want to image personal data that can be stored in a file and read out again via your program. There is therefore a model class ( Staff ) and a serializer ( Containers ). Last class I would name differently (more clearly) (eg Staff Administrator ) and it would be good to equip both classes only with the respective properties/methods that are actually relevant to them.
The model class only serves for pure data storage. So she only holds the fields for name, age, etc.
All about administration (storing and reading) is, on the other hand, exclusively part of the administrator class.
With regard to the data format, I would think again. On the one hand, I would only save the pure data (without labeling), otherwise you need to see that when you read it, you will cut the label back from the value. If each property is written in a separate line, you do not need a CSV format. This would be more worthwhile if you really structured accordingly, for example:
Such a format would also be well suited if you want to save several people in the file (each line reflects the data of a personal entity).
When reading the file, you would have to ignore the first line (header) and separate the other lines using the comma (eg via sstream ).
All that concerns the reading of the data from the console and the output would be better saved in a separate function, which is main is called later.
This clear functional separation ensures more flexibility (the model class could still be used for other cases without having a dependency on any stream) and overview (the code is logically structured).