Professional Documents
Culture Documents
File Tp tin
To, c, ghi, v cp nht file Truy xut file tun t Truy xut file ngu nhin
Struct
L mt kiu d liu:
Tp hp nhiu thnh phn c lin quan v ngha vi nhau Cc thnh phn c th thuc nhiu kiu c s khc T kha ch nh y l kiu struct nhau
Tn ca kiu struct v dng khai bo bin
Struct
Khng th cha mt thnh phn d liu l chnh n C th cha mt thnh phn d liu l con tr tr n chnh kiu struct ny Mt nh ngha struct khng thc s chim gi b nh SinhVien sv, a, b, *ptrSV; SinhVien DanhSachSV[100];
Struct
Cc ton t hp l
Php gn
Ton t ly a ch (&)
Truy xut cc thnh phn d liu (., ->)
Ton t sizeof dng ly kch thc thc tnh theo byte ca kiu struct
Struct
#include <stdio.h> #include <conio.h> typedef struct SinhVien { char HoTen[60]; int Tuoi; float DiemTB; }; int main() { SinhVien dssv[10]; int n; float t; printf(S lng sinh vin: ); scanf(%d, &n); for (int i=0; i<n; i++) { printf(sinh vin %d:\n, i); printf(H tn: ); scanf(%s, dssv[i].HoTen); printf(Tuoi: ); scanf(%d, &dssv[i].Tuoi); printf(Diem TB: ); scanf(%f, &t); dssv[i].DiemTB = t; } printf(\nDanh sch sinh vin:\n); for (i=0; i<n; i++) { printf(sinh vin %d:\n, i); printf(H tn: %s\n, dssv[i].HoTen); printf(Tuoi: %d\n, dssv[i].Tuoi); printf(Diem TB: %f\n, dssv[i].DiemTB); } getch(); return 0;
Struct
Truyn ton b bin kiu struct Ch truyn mt / nhiu thnh phn d liu (.) Truyn theo gi tr Truyn theo tham chiu
Struct
#include <stdio.h> #include <conio.h> typedef struct SinhVien { char HoTen[60]; int Tuoi; float DiemTB; }; void NhapSV(SinhVien &sv); void XuatSV(SinhVien sv); void DauRot(float diem); int main() { SinhVien dssv[10]; int n; float t; printf(S lng sinh vin: ); scanf(%d, &n); for (int i=0; i<n; i++) { printf(sinh vin %d:\n, i); NhapSV(dssv[i]); } printf(\nDanh sch sinh vin:\n); for (i=0; i<n; i++) { printf(sinh vin %d:\n, i); XuatSV(dssv[i]); DauRot(dssv[i].DiemTB); } getch(); return 0; }
Struct
void NhapSV(SinhVien &sv) { printf(H tn: ); scanf(%s, dssv[i].HoTen); printf(Tuoi: ); scanf(%d, &dssv[i].Tuoi); printf(Diem TB: ); scanf(%f, &t); dssv[i].DiemTB = t; } void XuatSV(SinhVien sv) { printf(H tn: %s\n, dssv[i].HoTen); printf(Tuoi: %d\n, dssv[i].Tuoi); printf(Diem TB: %f\n, dssv[i].DiemTB); } void DauRot(float diem) { if (diem>=5) printf(==> U\n); else printf(==> RT\n); }
File
L mt dy tun t cc byte d liu Kt thc bng mt k t c bit (^Z) C qun l file m bng mt bin kiu FILE
Mode
ngha M file ch c. To file mi ghi. Nu file c, ni dung ca n s b xa b. Thm vo; m hay to file mi ghi vo cui file. M file cp nht (c v ghi). To file mi cp nht. Nu file c, ni dung ca n s b xa b. Thm vo; m hay to file cp nht; thao tc ghi c thc hin cui file. M file ch c trong ch nh phn. To file ghi trong ch nh phn. Nu file c, ni dung ca n s b xa b. Thm vo; m hay to file mi ghi vo cui file trong ch nh phn. M file cp nht (c v ghi) trong ch nh phn. To file mi cp nht trong ch nh phn. Nu file c, ni dung ca n s b xa b. Thm vo; m hay to file cp nht trong ch nh phn; thao tc ghi c thc hin cui file. Cc ch m file
File - M / ng file
r w
FILE* fopen(const char* filename, const char* mode); FILE* f = fopen(thotinh.txt,rt); int fclose(FILE* f); fclose(f) int feof (FILE* f); if ( feof (f) )
r+ w+
fclose: ng file m
a+
rb wb
ab
rb+
wb+
ab+
Text File
fgetc: c 1 k t t file
int fgetc (FILE* f) int kytu = fgetc (f); int fputc (int c, FILE* f) fputc (A, f); int fgets (char*s, int n, FILE* f) char HoTen[60]; fgets (HoTen, 60, f);
Text File
int fscanf (FILE* f, const char* format [, arglist ] ) int fprintf (FILE* f, const char* format [, arglist ] ) fscanf(f, %i, &n); fprintf(f, %i + %i = %i, a, b, tong);
Text File
3 Trn Vn A 23 8.65 Cng Tng Tn N Ngc Din L Th Hng Nga 80 4.9 Trn Trng Trc 19 9.2
Text File
void GhiSV (FILE* f, SinhVien sv) { fprintf(f, %s\n%d\n%f\n, sv.HoTen, sv.Tuoi, sv.DiemTB); } void DocSV (FILE* f, SinhVien &sv) { float t; fgets(sv.HoTen, 60, f); fscanf(f, %d, &sv.Tuoi); fscanf(f, %f, &t); sv.DiemTB = t; } int GhiDSSV (SinhVien dssv[], int n) { FILE* f = fopen(sv.txt, w); if (f==NULL) return 0; fprintf(f, %d\n, n); for (int i=0; i<n; i++) GhiSV(f, dssv[i]); fclose(f); return 1; } int DocDSSV (SinhVien dssv[], int& n) { FILE* f = fopen(sv.txt, r); if (f==NULL) return 0; fscanf(f, %d, &n); for (int i=0; i<n; i++) DocSV(f, dssv[i]); fclose(f); return 1; }
Text File
int CopyTextFile(char* nguon, char* dich) { FILE* fnguon = fopen(nguon, r); FILE* fdich = fopen(dich, w); if (fnguon==NULL || fdich==NULL) return 0; char kytu; while ( !feof (fnguon) ) { kytu = fgetc(fnguon); fputc(kytu, fdich); } fclose(fnguon); fclose(fdich); return 1; }
Binary File
3 Trn Vn A 23 8.65 Cng Tng Tn N Ngc Din L Th Hng Nga 80 4.9 Trn Trng Trc 19 9.2
Chiu di c nh: sizeof(SinhVien)
Binary File
D liu c lu tr theo dng th (raw), thng khng th c bng mt. Cc hm dng cho binary file:
int fwrite (void* data, size_t size, int n, FILE* f); int fread (void* data, size_t size, int n, FILE* f); fwrite (&sv, sizeof(sinhvien), 1, f); fread (&sv, sizeof(sinhvien), 1, f); fwrite (dssv, sizeof(sinhvien), n, f);