C++中怎么把数据信息写到一个文件中,请高手指点程序如下:

#include<iostream>#include<fstream>usingnamespacestd;charname[10][80];//thisarrayhold... #include <iostream>
#include<fstream>
using namespace std;

char name[10][80]; // this array holds employee names
char phone[10][20]; // their phone numbers
float hours[10]; // hours worked per week
float wage[10]; // wage
int i;
int menu();
void enter(), report(),save();

int main()
{
int choice;

do {
choice = menu(); // get selection
switch(choice) {
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default: cout << "Try again.\n\n";
}
} while(choice != 0);

return 0;
}

// Return a user's selection.
int menu()
{
int choice;

cout << "0. Quit\n";
cout << "1. Enter information\n";
cout << "2. Report information\n";
cout << "\nChoose one: ";
cin >> choice;

return choice;
cin.get();
}

// Enter information.
void enter()
{

char temp[80];

for(i=0; i<3; i++) {
cout << "Enter last name: ";
cin >> name[i];
cout << "Enter phone number: ";
cin >> phone[i];
cout << "Enter number of hours worked: ";
cin >> hours[i];
cout << "Enter wage: ";
cin >> wage[i];
save();

}
cout<<"输入完毕!";
}

// Display report.
void report()
{
int i;

for(i=0; i<10; i++) {
cout << name[i] << ' ' << phone[i] << '\n';
cout << "Pay for the week: " << wage[i] * hours[i];
cout << '\n';
}
}
void save()
{
fstream outfile;
outfile.open("information");
if(!outfile)
{
cout<<" Can't open.\n";

}
outfile<<name[i]<<" "<<phone[i]<<" "<<hours[i]<<" "<<wage[i]<<"\n";
outfile.close();
}
上面是程序,怎么把人员信息写入到一个文件中?
展开
 我来答
wozuiruhe
2008-10-23 · TA获得超过229个赞
知道答主
回答量:213
采纳率:0%
帮助的人:0
展开全部
void save()
{
ofstream outfile;
outfile.open("information",ios::app);
if(!outfile)
{
cout<<" Can't open.\n";
}
outfile<<name[i]<<" "<<phone[i]<<" "<<hours[i]<<" "<<wage[i]<<"\n";
outfile.close();
}
改成这样就好了。
chimmy869
2008-10-23 · 超过63用户采纳过TA的回答
知道答主
回答量:183
采纳率:0%
帮助的人:190万
展开全部
用文件操作:下面将name写入1.txt中
FILE *fp;
char ch;
if((fp=fopen("1.txt","w"))==NULL) //工程下的1.txt文件
{
printf("cannot open source file.\n");
exit(1);
}
fputs(name, fp);
fclose(fp);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式