急急 c++ 文件输出

来源:百度知道 编辑:UC知道 时间:2024/06/01 10:29:29
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <fstream.h>

struct student
{
int num;
char name[10];
int score;
};

void main()
{
student s[3]={{0001,"abcd",23},{0002,"efdsj",21},{0003,"dsafdf",15}},d[3];
fstream out1,out2,in1,in2;

out2.open("file.dat",ios::out);
if(!out2)
{
cout<<"can't open"<<endl;
exit(1);
}
for(int j=0;j<3;j++)
out2<<s[j].num<<s[j].name<<s[j].score;
out2.close();

in2.open("file.dat",ios::in);
if(!in2)
{
cout<<"can't open"<<endl;
exit(1);
}
for( j=0;j<3;j++)
{
in2>>d[j].num>>d[j].name>>d[j].score;
cout<<

你把这一句out2<<s[j].num<<s[j].name<<s[j].score;
改为:
out2<<s[j].num<<' '<<s[j].name<<' '<<s[j].score<<' ';
就OK.
也即是输出的内容要以空格隔开.

晕 你的结构体外面的变量到哪去了
主函数里面要定义的东西定义没??像j这些变量都没定义嘛...自己好好看下...

是这个问题么?
是的话,就开晕了。。。