xml地图|网站地图|网站标签 [设为首页] [加入收藏]

string与指针的使用问题,导致数据无法写入文件

1)按职工号由小到大的顺序将5个员工的数据输出到磁盘文件中保存。#includefstream#includeiostream#includestring#definefilename"staff.dat"usingnamespacestd;classStaff{public:Staff(intn=0,stringnam="0",chars='0',inta=0,floatw=0):num(n),name(nam),sex(s),age(a),wage(w){}intnum;stringname;charsex;intage;floatwage;};voidmain(){Staffsta[7]={Staff(101,"张三",'m',25,3000),Staff(102,"李四",'m',26,3000),Staff(103,"王五",'m',26,3000),Staff(104,"张三丰",'m',26,3000),Staff(105,"李四牛",'m',26,3000)};fstreamss;ss.open(filename,ios::in|ios::out|ios::binary);if(!ss.is_open()){cout"打开失败"endl;}for(inti=0;i5;i++){coutsta[i].num""sta[i].name""sta[i].sex""sta[i].age""sta[i].wageendl;ss.write((char*)sta[i],sizeof(sta[i]));}cout"保存成功"endl;ss.close();}

必威 1

c++ 错误 c+++++++++++++++++++
#include
#include
#include
#include
using namespace std;
const int TMAX = 50;
class Person //基类
{
public:
int num; //编号
string name; //姓名
string sex; //性别
int age; //年龄
Person(int n = 0, string nm = " ", string s = " ", int a = 0) :num, name, sex, age {};//默认参数构造函数
//void Input() {};//输入成员信息
//void Output() {};//输出成员信息
int GetNum()//获得成员编号
{
return num;
}
string GetName()//获得成员姓名
{
return name;
}
};
class Teacher :virtual public Person//教师类
{
public:
string depart;//部门
string major;//专业
string job;//职业
//int choice1;//选择成员部门
//int choice2;//选择成员性别
Teacher(int n = 0, string nm = " ", string s = " ", int a = 0, string d = "电子系", string mj = "", string jb = "")
:Person(n, nm, s, a), depart, major, job {};
void Input()//出入各数据成员
{
cout << "input the num" << endl;
cin >> num;
cout << "input the name" << endl;
cin >> name;
cout << "input the sex" << endl;
cin >> sex;
//cout << "" << endl;
//cout << "请输入你的选择" << endl;
//cin >> choice2;
//switch
//{
//case 1:sex = "男"; break;
//case 2:sex = "女"; break;
//default:cout << "输入错误" << endl; break;
//}
cout << "input the age" << endl;
cin >> age;
cout << "input the depart" << endl;
cin >> depart;
//cout << setw;
//cout << "1.电子系" << "2.机械系" << "3.经管系" << "4.土木系" << "5.计算机系" << "6.外语系"
// << "7.财会系" << "8.材料系" << "9.文法系" << endl;
//cout << "请输入你的选择" << endl;
//cin >> choice1;
//switch
//{
//case 1:depart = "电子系"; break;
//case 2:depart = "机械系"; break;
//case 3:depart = "经管系"; break;
//case 4:depart = "土木系"; break;
//case 5:depart = "计算机系"; break;
//case 6:depart = "外语系"; break;
//case 7:depart = "财会系"; break;
//case 8:depart = "材料系"; break;
//case 9:depart = "文法系"; break;
//default:cout << "输入错误" << endl; break;
//}
cout << "input the major" << endl;
cin >> major;
cout << "input the job" << endl;
cin >> job;
}
void Output()//输出个数据成员
{
cout << setw;
cout << num << name << sex << age << depart << major << job << endl;

// fill ostream

一个简单的C++小程序,用来读取名为1.txt的文本文件里的学生信息,并在2.txt中显示各项的总分。#includeiostream#includefstream#includestringusingnamespacestd;classstudent{public:stringname;intstunum;doublecppscore;doubleengscore;doublemathscore;};doubletotalCscore=0;doubletotalEscore=0;doubletotalMscore=0;intmain(){stringfilepath="1.txt";ifstreamfile;file.open(filepath,ios::in);if(!file.is_open()){return0;}intnum=0;for(;;){num++;students;stringstu;getline(file,stu);coutstuendl;if(stu.empty()){break;}char*p=stu[0];for(char*n=s.name[0];*p!='';p++,n++){*n=*p;}p++;stringstunum;for(char*n=stunum[0];*p!='';p++,n++){*n=*p;}p++;s.stunum=stoi(stunum);stringcppscore;for(char*n=cppscore[0];*p!='';p++,n++){*n=*p;}p++;coutstod(cppscore)"stoded"endl;s.cppscore=stod(cppscore);couts.cppscore"cpp"endl;totalCscore=s.cppscore+totalCscore;stringengscore;for(char*n=engscore[0];*p!='';p++,n++){*n=*p;}p++;coutstod(engscore)"stoded"endl;s.engscore=stod(engscore);couts.engscore"eng"endl;totalEscore=s.engscore+totalEscore;stringmathscore;for(char*n=mathscore[0];*p!='';p++,n++){*n=*p;}coutstod(mathscore)"stoded"endl;s.mathscore=stod(mathscore);couts.mathscore"math"endl;totalMscore=s.mathscore+totalMscore;}ofstreamofile;ofile.open("2.txt",ios::out);ofile"c总分为"totalCscoreendl"e总分为"totalEscoreendl"m总分为"totalMscoreendl;system("pause");return0;}1.txt2.txt程序运行截图可以看到,在对前两行的学生信息读取时是没有差错的,但是对第三行和第四行的学生成绩读取时就出现了问题,如将100读为1000或1005,请问问题是出在哪里?

}

include <iostream>

};
class Staff :virtual public Person//行政人员类
{
public:
string zzmm;//政治面貌
string zc;//职称
//int choice2;//选择成员性别
Staff(int n = 0, string nm = "", string s = "男", int a = 0, string z = "", string zc = "")
:Person(n, nm, s, a), zzmm, zc {}; //构造函数
void Input()//输入各数据成员函数
{
cout << "input the num" << endl;
cin >> num;
cout << "input the name" << endl;
cin >> name;
cout << "input the sex" << endl;
cin >> sex;
//cout << "" << endl;
//cout << "请输入你的选择" << endl;
//cin >> choice2;
//switch
//{
//case 1:sex = "男"; break;
//case 2:sex = "女"; break;
//default:cout << "输入错误" << endl; break;
//}
cout << "input the age" << endl;
cin >> age;
cout << "input the zzmm" << endl;
cin >> zzmm;
cout << "input the zc" << endl;
cin >> zc;
}
void Output()//输出各数据成员函数
{
cout << setw;
cout << num << name << sex << age << zzmm << zc << endl;
}
};
class Staf_Manage //行政人员功能类
{
Staff Sta[TMAX]; //TMAX为常量,为数组中可储存的行政人员记录的最大值
int top; //表示当前系统中储存的记录个数
Staf_Manage() //初始时记录的个数为0,所以设置top=0
{
top = 0;
}
void Add(); //实现该类记录的添加
void Show(); //实现该类所有记录的显示

include <fstream>

void Seacher(); //实现该类记录的查询
void Edit(); //实现该类记录的修改
void Delete(); //实现该类记录的删除
void Total(); //实现该类记录的统计
void Save(); //实现该类记录的保存
void Read(); //实现该类记录的读取
};
void Staf_Manage::Add()//添加行政人员成员数据的函数
{
if (top >= TMAX)
{
cout << "用户已满" << endl;
}
Staff t;
cout << "输入新添加的人员的信息" << endl;
t.Input();
for (int i = 0; i if (t.GetNum() == Sta[i].GetNum
{
cout }
Sta[top] = t;
top = top + 1;
cout }
void Staf_Manage::Show()//显示行政人员成员数据的函数
{
if
{
cout }
cout cout for (int i = 0; i Sta[i].Output();
}
void Staf_Manage::Seacher()//查找行政人员成员数据的函数
{
if
{
cout }
int choice;
cout cin >> choice;
switch
{
case 1: {
cout << "请输入要查找的编号" << endl;
int num;
cin >> num;
for (int i = 0; i < top; i++)
{
if (Sta[i].GetNum
{
Sta[i].Output();
}
}
cout << "查无此人" << endl;
}break;
case 2: {
cout << "请输入要查找的姓名" << endl;
string name;
cin >> name;
for (int i = 0; i {
if (Sta[i].GetName() == name)
{
Sta[i].Output();
}
}
cout }break;
default:cout }
void Staf_Manage::Delete()//删除行政人员成员数据的函数
{
if
{
cout }
cout int choice;
cin >> choice;
switch
{
case 1:
{
cout << "请输入编号:";
int num;
cin >> num;
for (int i = 0; i < top; i++)
{
if (num == Sta[i].num)
{
cout << "是否确认删除? 1.是 2.否 " << endl;
int choice;
cin >> choice;
switch
{
case 1:
{
for (int j = i; j < top - 1; j++)
Sta[j] = Sta[j + 1];
cout << "删除成功!" << endl;
top--;
}; break;
case 2:return; break;
default:cout << "无此项,失效!" << endl; break;
}
}
}
cout << "无此人!" << endl;
}; break;
case 2:
{
cout << "请输入姓名:";
string name;
cin >> name;
for (int i = 0; i < top; i++)
{
if (name == Sta[i].name)
{
cout << "是否确认删除? 1.是 2.否 " << endl;
int choice;
cin >> choice;
switch
{
case 1:
{
for (int j = i; j < top - 1; j++)
Sta[j] = Sta[j + 1];
cout << "删除成功!" << endl;
top--;
}; break;
case 2:return;
default:cout << "无此项,失效!" << endl; break;
}
}
}
cout << "无此人!" << endl;
}; break;
default:cout << "没有此项,失效!" << endl; break;
}
}
void Sta_Manage::Edit()//修改行政人员成员数据的函数
{
if
{
cout << "当前系统中没有存储记录!" << endl;
}
int num;
cout << "请输入要查找的行政人员编号:" << endl;
cin >> num;
for (int i = 0; i < top; i++)
{
if (Sta[i].GetNum
{
cout << "请对这位行政人员的信息进行修改:" << endl;
Sta[i].Input();
cout << "修改成功" << endl;
}
}
cout << "查无此人" << endl;
}
void Sta_Manage::Total()//统计行政人员成员数据的函数
{
cout << "1. 按人数统计 2.按性别统计 " << endl;
int choice;
cout << "请输入你的选择" << endl;
cin >> choice;
switch
{
case 1:cout << "行政人员类数量为:" << top << endl; break;
case 2:
{
int a = 0, b = 0;
for (int i = 0; i < top; i++)
{
if (Sta[i].sex == "男")a++;
if (Sta[i].sex == "女")b++;
}
cout << "男性人数为" << a << endl;
cout << "女性人数为" << b << endl;
}; break;
default:cout << "输入错误,请重新输入" << endl; break;
}
}
void Sta_Manage::Save()//存储行政人员成员数据的函数
{
ofstream out("D:ying3.txt", ios::out);
if
{
cout << "打开失败!" << endl;
return 0;
}
cout << "打开成功!" << endl;
for (int i = 0; i < top; i++)
{
out << Sta[i].num << "" << Sta[i].name << "" << Sta[i].sex << "" << Sta[i].age
<< "" << Sta[i].zzmm << "" << Sta[i].zc << endl;
}
cout << "写入成功!" << endl;
out.close();
}
void Sta_Manage::Read()//读出行政人员成员数据的函数
{
ifstream in("D:ying3.txt", ios::in);
if
{
cout << "打开失败!" << endl;
return 0;
}
int i = 0;
while (in << Sta[i].num << "" << Sta[i].name << "" << Sta[i].sex << "" << Sta[i].age
<< "" << Sta[i].zzmm << "" << Sta[i].zc)
{
Sta[i].Output();
i++;
top++;
}
in.closs();
};

include <iomanip>

其他的没可以不看 为了方便一起复制了为什么错了哪么多

include <string>

using namespace std;
class WF
{
int i;
char f;
public:
WF(int i,char f) :i(i),f(f){}

friend ostream& operator<<(ostream& os,WF o)
{
os.width(o.i);
os.fill(o.f);
return os;
}
};
int main()
{
cout.width(10);//fill ten bytes screen.
cout << 123 << endl;//width only used next time//宽度填充仅对下一次输出有效
cout << 123 << endl;
cout.width(1); //fill one byte screen
cout << 123 << endl;
cout.width(10);
cout.fill(''); //free space used of ‘’.often used ..until the end of the program
cout <<123<<endl;
cout.width(10);
cout << 123 << endl;
cout.precision(8);//set precision //设置精度
cout << (double)123.0 <<endl;
//often see output control sign
必威 ,cout.setf(ios::left); //left 向左对齐

cout.setf(ios:: right | ios::showpos);
cout.setf(ios::scientific | ios::uppercase);
cout << WF(8,'') << 123 <<endl;
cout.unsetf(ios::scientific | ios::uppercase);
cout.unsetf(ios:: right | ios::showpos);
//====================================
cout << setw(10) << setfill('~') << left << 123 << endl;
cout << setw(10) << setfill('+') << right << 123 << endl;
//================================
string str;
str = "hello";
cout << sizeof(str) <<endl;
str = "hello,world!";
cout << sizeof(str) <<endl; //输出结果一样,sizeof只注重类型,不关注内容。。是指针
//ofstream fout("app.txt",ios::app);//add to the file end//追加在文件后面 //顺序读写方法
//seekg(位置 int)跳着读写方法
//getline(cin,str);
ofstream fout("seek.txt",ios::out | ios::binary);
fout << "hello,sd0705!" <<endl;
fout.seekp(0);//first write H
fout.put('H');
fout.seekp(7);
fout <<"S";
fout << str << endl;
fout.close();
/

ios::right //右对齐
/========
ios::dec
ios::oct
ios::hex
/========
ios::showbase// take prefix output 带着前缀输出
ios::showpoint //take point output 带着小数点输出
ios::scientific //science output 科学输入法输出 3.5 e 9
ios::uppercase // uppercase 大写输出
ios::showpos// use sign output //使用符号输出
ios::app //start writing at end of file
ios::ate //start reading or writing at end of file
ios::in //open for reading
ios::trunc //truncate file to zero length if exists
ios::nocreate //erroe when opening if file does not exist
ios::noreplace :error when opening for output if file exist
ios::binary //open file in binary mode
ios::out //open for writing
/=======
//flush :clear out buffer
endl: insert a 'n' and clear out buffer
oct:set the output as oct
dec
hex
{
setw(宽度)
setfill(填充字符)
setprecision(精度)
必须有头文件 <iomanip>
}
/
//cout.left
//=======================after revision
/
char ch;
int num;
cout << "cin num and char" << endl;
cin >> num >> ch;
cout << WF(num,ch) << 123 <<endl; //must have output //必须有输出
*/

}

本文由必威发布于编程,转载请注明出处:string与指针的使用问题,导致数据无法写入文件