You are on page 1of 19

Struct & File

Struct & File

Struct Kiu d liu t nh ngha c cu trc

To v s dng struct Khai bo v truyn tham s kiu struct cho hm


Truyn gi tr Truyn tham chiu

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

typedef struct SinhVien { char HoTen[60]; int Tuoi; float DiemTB; };

Kiu SinhVien c 3 thnh phn d liu

Struct

nh ngha kiu 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];

Khai bo bin thuc kiu struct


Struct

Cc ton t hp l

Php gn

SinhVien a, b, dssv[100]; a = b; dssv[50] = a;

Ton t ly a ch (&)
Truy xut cc thnh phn d liu (., ->)

a Tuoi = 20; dssv[20] DiemTB = 10.0;

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 tham s cho hm

Truyn ton b bin kiu struct Ch truyn mt / nhiu thnh phn d liu (.) Truyn theo gi tr Truyn theo tham chiu

void NhapSV(SinhVien &sv); void XuatSV(SinhVien sv); void DauRot(float diem);

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

Dng lu tr bn vng nhng d liu trong chng trnh

D liu lu tr trong bin lun ch l tm thi

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

stdin stdout stderr

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

fopen: M file c / to file mi

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

feof: Kim tra cui file ?


ab

rb+

wb+

ab+

Text File

Cc hm trong th vin chun (<stdio.h>)

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);

fputc: Ghi 1 k t vo file


fgets: c 1 chui k t (1 dng) t file


Text File

Cc hm trong th vin chun (<stdio.h>)

fputs: Ghi 1 chui k t (1 dng) vo file


int fputs (char* s, FILE* f) fputs (Nguyn Vn A, f);

fscanf / fprintf: Tng t nh scanf / printf nhng hot ng trn 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);

File Truy xut ngu nhin

fseek: Di con tr c, ghi ca file n v tr c th c cung cp

int fseek(FILE* f, long offset, int start);

SEEK_SET: u file SEEK_END: cui file SEEK_CUR: v tr hin ti

fseek(f, sizeof(sinhvien)*5, SEEK_SET); fseek(f, -sizeof(sinhvien), SEEK_END); fseek(f, sizeof(sinhvien), SEEK_CUR);

You might also like