Home > Back-end >  The C compiler but there is something wrong with it is read in function
The C compiler but there is something wrong with it is read in function

Time:09-20

Why I write to the file data, and the data read from the file is not the same!

The code below
# include
# include
# include
using namespace std;
//int n=0;
Struct Worker {
int num;
char name[20];
int age;
Float wage;
};
Void input (int n, Worker * w) {


for(int i=0; iCout<& lt;" Num: ";
Cin> W [I] num;
Cout<& lt;" Name: ";
Cin> W [I]. Name;
Cout<& lt;" Age: ";
Cin> W [I] the age;
Cout<& lt;" Wage: ";
Cin> W [I]. Wage;
}
}

Void bubble_sort (int n, Worker * w) {
The Worker w1.
Bool change=true;
For (int I=n - 1; I>=1 & amp; & Change==true; I -) {
Change=false;
for(int j=0; JIf (w [j] num> W [j + 1] {num)
W1=w [j];
[j]=w w [j + 1);
W [j + 1)=w1;
Change=true;
}
}
}
}

Void write_file (char * filename, Worker * w) {
Fstream outfile (filename, the ios: : out | ios: : | in ios: : binary);
if(! Outfile) {
Cerr<& lt;" Open the file error!" exit(1);
}
Outfile. Write ((char *) w, sizeof (w));
Outfile. Close ();
}

Void show (int n, Worker * w) {
for(int i=0; iCout<& lt;" Num: "& lt; Cout<& lt;" Name: "& lt; Cout<& lt;" Age: "& lt; Cout<& lt;" Wage: "& lt; }
}

Void read_file (char * filename, int n) {
Fstream ifile (filename, the ios: : out | ios: : | in ios: : binary);
if(! Ifile) {
Cout<& lt;" The open ifle error!" exit(1);
}
The Worker * w2=new Worker [n].
for(int i=0; iIfile. Read (w2 (char *), sizeof (w2 [I]));
Cout<& lt;" Num: "& lt; Cout<& lt;" Name: "& lt; Cout<& lt;" Age: "& lt; Cout<& lt;" Wage: "& lt; }
Ifile. Close ();
The delete [] w2;
}

The Worker * add (int & amp; Num, Worker * w) {
Int num1.
If (w==NULL | | num==0) {
Cout<& lt;" You have not creat a list!" Return NULL;
}
Cout<& lt;" Both please input number of extra workers: ";
Cin> Num1.
The Worker * w1=new Worker [num + num1];
for(int i=0; iIf (i>=0 & amp; & iW1=w [I] [I];
}
If (i>=num& & iCout<& lt;" Num: ";
Cin> W1 [I] num;
Cout<& lt;" Name: ";
Cin> W1 [I]. Name;
Cout<& lt;" Age: ";
Cin> W1 [I] the age;
Cout<& lt;" Wage: ";
Cin> W1 [I]. Wage;
}
}
Num +=num1;
Return w1.
}

Int main () {
int n=0;
Int num=0;
The Worker * w=NULL;
Cout<& lt;" Worker_system "& lt; Cout<& lt;" 1. Creat "& lt; Cout<& lt;" 2. Add "& lt; Cout<& lt;" 3. The sort "& lt; Cout<& lt;" 4. Delete "& lt; Cout<& lt;" 5. Show "& lt; Cout<& lt;" 6. The exit "& lt; While (1) {
Cout<& lt;" Do your choice: ";
Cin> n;
{if (n==6)
break;
}
The switch (n) {
Case 1:
//coutIf (num==0) {
Cout<& lt;" Do the import number of worker: ";
Cin> Num.
W=new Worker (num);
Input (num, w);
Write_file (" worker_sys. TXT ", w);
break;
}
The else {
Cout<& lt;" Already creat a list!" break;
}
Case 2:
W=add (num, w);
//coutWrite_file (" worker_sys. TXT ", w);
break;
Case 3:
If (num==0) {
//bubble_sort (num, w); break;
Cout<& lt;" You have not creat a list!" break;
}
The else {
Bubble_sort (num, w);
break;
}
Case 4: break;
Case 5:
If (num==0) {
Cout<& lt;" You have not creat a list!" break;
}
The else {
The read_file (" worker_sys. TXT ", num);
break;
}
//the read_file (" worker_sys. TXT ", num); break;
Default: cout<& lt;" The error "; break;
}
}
return 0;
}

CodePudding user response:

Where different didn't understand

CodePudding user response:

reference 1st floor weixin_41556000 response:
where different understand

The input of the age, wage and the output of the age, wage is different

CodePudding user response:

Outfile. Write ((char *) w, sizeof (w));
Should be changed to
Outfile. Write ((char *) w, sizeof (* w));

CodePudding user response:

reference double cup with wine reply: 3/f
outfile. Write ((char *) w, sizeof (w));
Should be changed to
Outfile. Write ((char *) w, sizeof (* w));

Changed or have a problem

CodePudding user response:

null
  • Related