求大神把这一段c语言的代码帮我改成C++语言的,急用啊!!!!!!谢谢啦
voidWritetoText(goodsgs[],intn)/*将所有记录写入文件*/{inti=0;FILE*fp;/*定义文件指针*/if((fp=fopen("d...
void WritetoText(goods gs[],int n) /*将所有记录写入文件*/
{
int i=0;
FILE *fp; /*定义文件指针*/
if((fp=fopen("d:\\商品.doc","wb"))==NULL) /*打开文件*/
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循环写入数据*/
while(i<n)
{
fprintf(fp,"%-5s%d %d %d\n",gs[i].name,gs[i].time,gs[i].year,gs[i].no);
i++;
}
fclose(fp); /*关闭文件*/
printf("\t\t\tSuccessed!\n"); /*返回成功信息*/
}
int AddfromText(goods gs[],int n) /*从文件中读入数据*/
{
int i=0,num;
FILE *fp; /*定义文件指针*/
if((fp=fopen("d:\\商品.doc","rb"))==NULL) /*打开文件*/
{
printf("\t\t\tcann't open the file\n"); /*打开失败信息*/
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*读入总记录量*/
while(i<num) /*循环读入数据*/
{
fscanf(fp,"%s%d%d%d",gs[n+i].name,&gs[n+i].time,&gs[n+i].year,&gs[n+i].no);
i++;
}
n+=num;
fclose(fp); /*关闭文件*/
printf("\t\t\tSuccessed!\n");
printf("\t\t\t");
system("pause");
return(n);
} 展开
{
int i=0;
FILE *fp; /*定义文件指针*/
if((fp=fopen("d:\\商品.doc","wb"))==NULL) /*打开文件*/
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循环写入数据*/
while(i<n)
{
fprintf(fp,"%-5s%d %d %d\n",gs[i].name,gs[i].time,gs[i].year,gs[i].no);
i++;
}
fclose(fp); /*关闭文件*/
printf("\t\t\tSuccessed!\n"); /*返回成功信息*/
}
int AddfromText(goods gs[],int n) /*从文件中读入数据*/
{
int i=0,num;
FILE *fp; /*定义文件指针*/
if((fp=fopen("d:\\商品.doc","rb"))==NULL) /*打开文件*/
{
printf("\t\t\tcann't open the file\n"); /*打开失败信息*/
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*读入总记录量*/
while(i<num) /*循环读入数据*/
{
fscanf(fp,"%s%d%d%d",gs[n+i].name,&gs[n+i].time,&gs[n+i].year,&gs[n+i].no);
i++;
}
n+=num;
fclose(fp); /*关闭文件*/
printf("\t\t\tSuccessed!\n");
printf("\t\t\t");
system("pause");
return(n);
} 展开
2014-12-29
展开全部
#include <fstream>
#include <iostream>
#include<iomanip>
using namespace std;
struct goods{
string name;
int time;
int year;
int no;
};
void WritetoText(goods gs[], int n) /*将所有记录写入文件*/
{
int i = 0;
fstream fp; /*定义文件指针*/
fp.open("d:\\商品.doc", ios::out);
if (fp) /*打开文件*/
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fp << n << endl; /*循环写入数据*/
while (i < n)
{
fp << setw(5) << gs[i].name << " " << gs[i].time << setw(4) << " " << gs[i].year << setw(4) << " " << gs[i].no;
i++;
}
fp.close(); /*关闭文件*/
cout<<"\t\t\tSuccessed!\n"; /*返回成功信息*/
}
int AddfromText(goods gs[], int n) /*从文件中读入数据*/
{
int i = 0, num;
fstream fp; /*定义文件指针*/
fp.open("d:\\商品.doc", ios::in);
if (fp) /*打开文件*/
{
cout<<"\t\t\tcann't open the file\n"; /*打开失败信息*/
cout<<"\t\t\t";
system("pause");
return(n);
}
fp>> num; /*读入总记录量*/
while (i < num) /*循环读入数据*/
{
fp>>gs[n + i].name>>gs[n + i].time>>gs[n + i].year>>gs[n + i].no;
i++;
}
n += num;
fp.close(); /*关闭文件*/
cout<<"\t\t\tSuccessed!\n";
cout<<"\t\t\t";
system("pause");
return(n);
}
展开全部
#include <fstream>
#include <iostream>
using namespace std;
void WritetoText(goods gs[],int n) /*将所有记录写入文件*/
{
int i=0;
//FILE *fp; /*定义文件指针*/
fostream fp;
if((fp.open("d:\\商品.doc",ios::out,ios::binary))==NULL) /*打开文件*/
{
cout<<"\t\t\tcann't open the file\n";
system("pause");
exit(0);
}
fp<<n<<endl; /*循环写入数据*/
while(i<n)
{
fp<<gs[i].name<<gs[i].time<<gs[i].year<<gs[i].no<<endl;
i++;
}
fp.close(); /*关闭文件*/
cout<<"\t\t\tSuccessed!\n"; /*返回成功信息*/
}
int AddfromText(goods gs[],int n) /*从文件中读入数据*/
{
int i=0,num;
//FILE *fp; /*定义文件指针*/
fistream fp;
if((fp.fopen("d:\\商品.doc",ios::binary,ios::in))==NULL) /*打开文件*/
{
cout<<"\t\t\tcann't open the file\n"; /*打开失败信息*/
// printf("\t\t\t");
system("pause");
return(n);
}
fp>>num; /*读入总记录量*/
while(i<num) /*循环读入数据*/
{
fp>>gs[n+i].name>>gs[n+i].time>>gs[n+i].year>>gs[n+i].no;
i++;
}
n+=num;
fp.close(); /*关闭文件*/
cout<<"\t\t\tSuccessed!\n";
system("pause");
return(n);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询