30/09/2018, 16:20
Cần giúp về file C++ có đường dẫn tới ổ E?
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!
";
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,"
");
//cout<<"ho & ten SV: "<<s.hoten<<endl<<endl;
while (!feof(fp))
{
fgets(s.hoten,100,fp);
}
fprintf(fp,"
");
//cout<<"so mon hoc: "<<s.somon<<endl<<endl;
fprintf(fp,"%d
", s.somon);
//cout<<"Tong TC: "<<tongtc(s)<<endl<<endl;
fprintf(fp,"%d
", tongtc(s));
//cout<<"Tong TC tich luy: "<<tongtctl(s)<<endl<<endl;
fprintf(fp,"%d
", tongtctl(s));
//cout<<"diem TB: "<<dtb(s)<<endl<<endl;
fprintf(fp,"%d
", dtb(s));
//cout <<"diem TBTL: "<<dtbtl(s)<<endl<<endl;
fprintf(fp,"%d
", dtbtl(s));
// cout<<s.kq[i].mamh<<" "<<s.kq[i].tenmh<<" "<<s.kq[i].sotc<<" "<<s.kq[i].diem;
for(int i=0; i<s.somon; i++)
{
fprintf(fp,"===========================
");
fprintf(fp,"%d
", s.kq[i].mamh);
while (!feof(fp))
{
fgets(s.kq[i].tenmh,50,fp);
}
fprintf(fp,"
");
fprintf(fp,"%d
", s.kq[i].sotc);
fprintf(fp,"%d
", s.kq[i].diem);
fprintf(fp,"===========================
");
}
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:
";
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: "<<"
";
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"<<" "<<"Ten MH"<<" "<<"So TC"<<" "<<"Diem"<<endl;
cout<<"====================================================="<<endl;
for(int i=0; i<s.somon; i++)
{
cout<<s.kq[i].mamh<<" "<<s.kq[i].tenmh<<" "<<s.kq[i].sotc<<" "<<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!
";
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,"
");
//cout<<"ho & ten SV: "<<s.hoten<<endl<<endl;
while (!feof(fp))
{
fgets(s.hoten,100,fp);
}
fprintf(fp,"
");
//cout<<"so mon hoc: "<<s.somon<<endl<<endl;
fprintf(fp,"%d
", s.somon);
//cout<<"Tong TC: "<<tongtc(s)<<endl<<endl;
fprintf(fp,"%d
", tongtc(s));
//cout<<"Tong TC tich luy: "<<tongtctl(s)<<endl<<endl;
fprintf(fp,"%d
", tongtctl(s));
//cout<<"diem TB: "<<dtb(s)<<endl<<endl;
fprintf(fp,"%d
", dtb(s));
//cout <<"diem TBTL: "<<dtbtl(s)<<endl<<endl;
fprintf(fp,"%d
", dtbtl(s));
// cout<<s.kq[i].mamh<<" "<<s.kq[i].tenmh<<" "<<s.kq[i].sotc<<" "<<s.kq[i].diem;
for(int i=0; i<s.somon; i++)
{
fprintf(fp,"===========================
");
fprintf(fp,"%d
", s.kq[i].mamh);
while (!feof(fp))
{
fgets(s.kq[i].tenmh,50,fp);
}
fprintf(fp,"
");
fprintf(fp,"%d
", s.kq[i].sotc);
fprintf(fp,"%d
", s.kq[i].diem);
fprintf(fp,"===========================
");
}
fclose(fp);
}
Bài liên quan
Ý bạn là như thế nào lưu files hay code có vấn đề gì ?? bạn nên nói rõ ra
Trước hết, như @TuChiDo đã nói,
Anh đã giúp sửa lại nội dung câu hỏi.
Code này quá dài, không cần thiết, hãy thử với một đoạn code ngắn hơn, và chỉ viết nội dung vào file.
Cách ghi file cực kỳ đơn giản trong C++.