Home > Back-end >  How to read data from the TXT file into the C class
How to read data from the TXT file into the C class

Time:09-20

The members of the class is the

Save the data function
Void Company: : Save (STD: : list & The list)
{
Ofstream outfile (" F: \ \ 131 \ \ v \ \ Worker \ \ Worker \ \ Worker \ \ Employee TXT ", the ios: : trunc);
If (outfile. Is_open ())
{
Outfile & lt; & lt; Setw (4) & lt; & lt; "Number" & lt; & lt; Setw (9) & lt; & lt; "Name" & lt; & lt; Setw (9) & lt; & lt; "Age" & lt; & lt; Setw (9) & lt; & lt; "Gender" & lt; & lt; Setw (9) & lt; & lt; "Zip code" & lt; & lt; Setw (9) & lt; & lt; "Department" & lt; & lt; Setw (10) & lt; & lt; "Wages" & lt; & lt; endl;

For (auto it=list. The begin (); it !=list. The end (); It++)
{
Outfile & lt; & lt; * it & lt; & lt; endl;
}
}
Outfile. Close ();
}

CodePudding user response:

Read out the way you write

CodePudding user response:

For (auto it=list. The begin (); it !=list. The end (); It++)
{
Outfile & lt; & lt; * it & lt; & lt; endl;
}

Here is your class reloading & lt; & lt; Operator, should also be heavy & gt;> Operator, it is ok to read in the same order

CodePudding user response:

How to write in how to read out, the operation differ

CodePudding user response:

For (auto it=list. The begin (); it !=list. The end (); It++)
{
Outfile & lt; & lt; * it & lt; & lt; endl;
}
Keep what information, the code is not complete

CodePudding user response:

Class structure can consider binary save and read:

Storage using the function, read use read function

Storage:

Ofstream fout (" file ", ios_base: : out | ios_base: : binary);

For (auto it=list. The begin (); it !=list. The end (); It++) {

Fout. Write (char * (* it), sizeof (* it));

}

Read:

Ifstream fout (" file ", ios_base: : out | ios_base: : binary);

The Employee * PTR=new Employee;

Fout. Read (char * (* PTR), sizeof (* PTR));

CodePudding user response:

The building Lord this action is not good, two variables to the output file, the intermediate need a delimiter!!!!!
MFC CArchive under reference

CodePudding user response:

Read the file "parse the file to a variable

CodePudding user response:

Simple way is to amend the class members to a structure, all use fixed-length data,
Written to the file and read out, using the entire structure of the object,

CodePudding user response:

I also have this problem, the core of the problem is, can't like ordinary variables directly read and write the data, the common variables given address directly read came in, but objects are encapsulated, member variables are not directly read, unless the member variables defined as public, but this is not just the destruction of encapsulation?

CodePudding user response:

refer to the eighth floor bjgxjob response:
simple way is to amend the class members to a structure, all use fixed-length data,
Written to the file and read out, using the entire structure of the object,
but that don't damage the encapsulation?

CodePudding user response:

reference 1st floor ztenv response:
way you write read
write when the data is written to a file directly, but read, because the data is encapsulated in the object, can't direct access to the address, so that reading is not enough ah, can only read can't write

CodePudding user response:

reference 11 floor Summer, snow response:
Quote: refer to 1st floor ztenv response:

Way you write read
write when the data is written to a file directly, but read, because the data is encapsulated in the object, can't direct access to the address, so reading is not enough ah, can only write cannot read

Itself is a problem that, look at the serialized, don't use this way,

CodePudding user response:

String is not very good control, you can use the binary write
Common type:
Void Serialize (STD: : ostream & amp; OS, const T & amp; Val)
{
OS. Write ((const char *) & amp; Val, sizeof (T));
}
For the character type:
Void Serialize (STD: : ostream & amp; OS, const T & amp; Val)
{
Unsigned int size=val. The size ();
OS. Write ((const char *) & amp; The size, sizeof (size));
OS. Write ((const char *) val. The data (), the size * sizeof (typename T: : value_type));
}
  • Related