Sao em ghi vào ổ E ko đc vậy?
Em chạy tới đoạn này thì bị báo là Ko mo duoc tap tin!
int main()
{
sv a;
nhapsv(a);
insv(a);
ghifilevb("E:/sinhvien3.txt",a);
return 0;
}
Đây là nội dung hàm: ghifilevb
void ghifilevb (char fname[], sv s)
{
FILE *fp;
fp = fopen(fname,"wt");
if (!fp)
{
cout <<"Ko mo duoc tap tin!\n";
return ;
}
/*while (!feof(fr)){
fgets(s, fr);
fputs(s,fw);
}*/
//cout<<endl<<"MSSV: "<<s.mssv<<endl<<endl;
while (!feof(fp))
{
fgets(s.mssv,9,fp);
}
fprintf(fp,"\n");
//cout<<"ho & ten SV: "<<s.hoten<<endl<<endl;
while (!feof(fp))
{
fgets(s.hoten,100,fp);
}
fprintf(fp,"\n");
//cout<<"so mon hoc: "<<s.somon<<endl<<endl;
fprintf(fp,"%d\n", s.somon);
//cout<<"Tong TC: "<<tongtc(s)<<endl<<endl;
fprintf(fp,"%d\n", tongtc(s));
//cout<<"Tong TC tich luy: "<<tongtctl(s)<<endl<<endl;
fprintf(fp,"%d\n", tongtctl(s));
//cout<<"diem TB: "<<dtb(s)<<endl<<endl;
fprintf(fp,"%d\n", dtb(s));
//cout <<"diem TBTL: "<<dtbtl(s)<<endl<<endl;
fprintf(fp,"%d\n", dtbtl(s));
// cout<<s.kq[i].mamh<<"\t"<<s.kq[i].tenmh<<"\t\t\t"<<s.kq[i].sotc<<"\t"<<s.kq[i].diem;
for(int i=0; i<s.somon; i++)
{
fprintf(fp,"===========================\n");
fprintf(fp,"%d\n", s.kq[i].mamh);
while (!feof(fp))
{
fgets(s.kq[i].tenmh,50,fp);
}
fprintf(fp,"\n");
fprintf(fp,"%d\n", s.kq[i].sotc);
fprintf(fp,"%d\n", s.kq[i].diem);
fprintf(fp,"===========================\n");
}
fclose(fp);
}
Đây là toàn bộ code:
#include <iostream>
#include <ctype.h>
#include <string.h>
#include "stdio.h"
using namespace std;
typedef struct MH
{
int mamh;// ma mon hoc
char tenmh[50];// ten mon hoc
int sotc;
int diem;
};
typedef struct sv
{
char mssv[9];
char hoten[100];
int somon;
MH kq[100];
int tctl,tongtc;
double dtbc,dtbtl;
};
//cac ham con
void nhapmh(MH &m);
void nhapsv(sv &s);
void insv(sv s);
int tongtc(sv s);
int tongtctl(sv s);
double dtb(sv s);
double dtbtl(sv s);
//ghi file
void ghifilevb (char fname[], sv s);
int main()
{
sv a;
nhapsv(a);
insv(a);
ghifilevb("E:/sinhvien3.txt",a);
return 0;
}
//cac ham con
void nhapsv(sv &s)
{
cout<<"nhap MSSV: ";
cin.getline(s.mssv,9);
cout<<"nhap ho ten: ";
cin.getline(s.hoten,100);
cout<<"so mon hoc: ";
cin>>s.somon;
cout<<"nhap thong tin cac mon: \n";
for(int i=0; i<s.somon; i++)
{
cout<<"nhap ma mon thu "<<i<<" : ";
cin>>s.kq[i].mamh;
cout<<"nhap ten mon thu "<<i<<" : ";
cin.ignore();
cin.getline(s.kq[i].tenmh,50);
cout<<"nhap diem mon thu " <<i<<": ";
cin>>s.kq[i].diem;
cout<<"nhap tchi mon thu "<<i<<": ";
cin>>s.kq[i].sotc;
}
}
void insv(sv s)
{
cout<<endl<<endl<<endl;
cout<<"----------THONG TIN SINH VIEN-----------";
cout<<endl<<endl<<endl;
cout<<endl<<"MSSV: "<<s.mssv<<endl<<endl;
cout<<"ho & ten SV: "<<s.hoten<<endl<<endl;
cout<<"so mon hoc: "<<s.somon<<endl<<endl;
cout<<"Sinh vien "<<"\""<<s.hoten<<"\""<<" co cac thong so sau: "<<"\n\n";
cout<<"Tong TC: "<<tongtc(s)<<endl<<endl;
cout<<"Tong TC tich luy: "<<tongtctl(s)<<endl<<endl;
cout<<"diem TB: "<<dtb(s)<<endl<<endl;
cout <<"diem TBTL: "<<dtbtl(s)<<endl<<endl;
cout<<"====================================================="<<endl;
cout<<"-------------------DIEM CHI TIET---------------------"<<endl;
cout<<"====================================================="<<endl;
cout<<"Ma MH"<<"\t"<<"Ten MH"<<"\t\t\t"<<"So TC"<<"\t"<<"Diem"<<endl;
cout<<"====================================================="<<endl;
for(int i=0; i<s.somon; i++)
{
cout<<s.kq[i].mamh<<"\t"<<s.kq[i].tenmh<<"\t\t\t"<<s.kq[i].sotc<<"\t"<<s.kq[i].diem;
cout<<endl<<endl;
}
}
int tongtc(sv s)
{
int s1=0;
for(int i=0; i<s.somon; i++)
{
s1 += s.kq[i].sotc;
}
return s1;
}
int tongtctl(sv s)
{
int s1=0;
for(int i=0; i<s.somon; i++)
{
if(s.kq[i].diem>=5)
s1 += s.kq[i].sotc;
}
return s1;
}
double dtb(sv s)
{
int s1=0;
for(int i=0; i<s.somon; i++)
{
s1 += s.kq[i].diem*s.kq[i].sotc;
}
double s2 = (double)s1/(tongtc(s));
return s2;
}
double dtbtl(sv s)
{
int s1=0;
for(int i=0; i<s.somon; i++)
{
if(s.kq[i].diem>=5)
s1 += s.kq[i].diem*s.kq[i].sotc;
}
double s2 = (double)s1/(tongtctl(s));
return s2;
}
//7. in danh sach nhung mon hoc con no
void danhsachno(sv s)
{
cout<<"nhung mon con no la: "<<endl;
for(int i=0; i<s.somon; i++)
{
if(s.kq[i].diem<5)
cout<<"mon thu "<<i;
cout<<endl;
}
}
void ghifilevb (char fname[], sv s)
{
FILE *fp;
fp = fopen(fname,"wt");
if (!fp)
{
cout <<"Ko mo duoc tap tin!\n";
return ;
}
/*while (!feof(fr)){
fgets(s, fr);
fputs(s,fw);
}*/
//cout<<endl<<"MSSV: "<<s.mssv<<endl<<endl;
while (!feof(fp))
{
fgets(s.mssv,9,fp);
}
fprintf(fp,"\n");
//cout<<"ho & ten SV: "<<s.hoten<<endl<<endl;
while (!feof(fp))
{
fgets(s.hoten,100,fp);
}
fprintf(fp,"\n");
//cout<<"so mon hoc: "<<s.somon<<endl<<endl;
fprintf(fp,"%d\n", s.somon);
//cout<<"Tong TC: "<<tongtc(s)<<endl<<endl;
fprintf(fp,"%d\n", tongtc(s));
//cout<<"Tong TC tich luy: "<<tongtctl(s)<<endl<<endl;
fprintf(fp,"%d\n", tongtctl(s));
//cout<<"diem TB: "<<dtb(s)<<endl<<endl;
fprintf(fp,"%d\n", dtb(s));
//cout <<"diem TBTL: "<<dtbtl(s)<<endl<<endl;
fprintf(fp,"%d\n", dtbtl(s));
// cout<<s.kq[i].mamh<<"\t"<<s.kq[i].tenmh<<"\t\t\t"<<s.kq[i].sotc<<"\t"<<s.kq[i].diem;
for(int i=0; i<s.somon; i++)
{
fprintf(fp,"===========================\n");
fprintf(fp,"%d\n", s.kq[i].mamh);
while (!feof(fp))
{
fgets(s.kq[i].tenmh,50,fp);
}
fprintf(fp,"\n");
fprintf(fp,"%d\n", s.kq[i].sotc);
fprintf(fp,"%d\n", s.kq[i].diem);
fprintf(fp,"===========================\n");
}
fclose(fp);
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?