intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Thư viện các hàm trong C

Chia sẻ: Nguyễn Thị Phương Phương | Ngày: | Loại File: PDF | Số trang:57

139
lượt xem
31
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Đây là Thư viện các hàm trong C căn bản mà mìnhsưu tầm được! nó rất hay và hữu ích đó các bạn. Hi vọng bài viết này sẽ giúp ích cho các bạn trong việc học lập trình C. Hàm dùng lấy 1 ký tự từ tệp do con trỏ tệp stream trỏ đến . Nếu thành công hàm trả về mã ASCII của ký tự đọc được . Nếu gặp lỗi hay gặp kết thúc tệp thì hàm trả về EOF .

Chủ đề:
Lưu

Nội dung Text: Thư viện các hàm trong C

  1. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn Thư Vi n C Hi! ây là Thư vi n các hàm trong C căn b n mà mình sưu t m ư c! nó r t hay và h u ích ó các b n. Hi v ng bài vi t này s giúp ích cho các b n trong vi c h c l p trình C. Thân! Mr.Vũ Hi u. 1. In/Out Danh sách các hàm s d ng In/Out 1.clearerr 2.fclose 3.feof 4.ferror 5.fflush 6.fgetc 7.fgetpos 8.fgets 9.fopen 10.fprintf 11.fputc 12.fputs 13.fread 14.freopen 15.fscanf 16.fseek 17.fsetpos 18.ftell 19.fwrite 20.getc 21.getchar 22.gets Mr.Vũ Kim Hi u 1
  2. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn 23.perror 24.printf 25.putc 26.putchar 27.puts 28.remove 29.rename 30.rewind 31.scanf 32.setbuf 33.setvbuf 34.sprintf 35.sscanf 36.tmpfile 37.tmpnam 38.ungetc 39.vprintf, vfprintf, and vsprintf Hàm clearerr Khai báo : Trích d n: void clearerr(FILE *stream); Xoá tín hi u l i gây ra b i h th ng khi thao tác trên t p tin g p l i . Example Code: #include int main(void) { FILE *fp; char ch; Mr.Vũ Kim Hi u 2
  3. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn /* mo tep tin de ghi */ fp = fopen("DUMMY.FIL", "w"); /*gay nen mot loi bang cach doc file*/ ch = fgetc(fp); printf("%c\n",ch); if (ferror(fp)) { /* xuat ra man hinh thong bao loi */ printf("Error reading from DUMMY.FIL\n"); /* xoa tin hieu loi */ clearerr(fp); } fclose(fp); return 0; } Hàm fclose Khai báo : Trích d n: int fclose(FILE *stream); óng t p l i sau khi thao tác , stream là tên con tr t p . Khi thành công hàm tr v 0 , trái l i tr v EOF ( h ng ch th k t thúc t p ) Example Code: #include #include int main(void) { FILE *fp; char buf[11] = "0123456789"; fp = fopen("DUMMY.FIL", "w"); fwrite(&buf, strlen(buf), 1, fp); /* dong tep */ fclose(fp); return 0; } Hàm feof Khai báo : Trích d n: Mr.Vũ Kim Hi u 3
  4. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn int feof(FILE *stream); Hàm dùng ki m tra con tr t p có n cu i t p hay chưa . N u nó n cu i t p thì hàm tr v giá tr khác 0 , ngư c l i tr v 0 Example Code: #include int main(void) { FILE *stream; /* mo mot tep de doc */ stream = fopen("DUMMY.FIL", "r"); /* doc mot ky tu tu tep */ fgetc(stream); /*kiem tra xem cuoi tep hay chua */ if (feof(stream)) printf("We have reached end-of-file\n"); /* dong tep*/ fclose(stream); return 0; } Hàm ferror Khai báo : Trích d n: int ferror(FILE *stream); Ki m tra l i thao tác trên t p . Hàm tr v giá tr khác 0 n u có l i , ngư c l i tr v 0 . Example : Code: #include int main(void) { FILE *stream; /* mo tep de ghi */ stream = fopen("DUMMY.FIL", "w"); /* gay ra mot loi bang cach doc*/ (void) getc(stream); if (ferror(stream)) /* kiem tra loi tren tep*/ { /* ghi loi ra man hinh*/ printf("Error reading from DUMMY.FIL\n"); Mr.Vũ Kim Hi u 4
  5. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn /* xoa tin hieu loi*/ clearerr(stream); } fclose(stream); return 0; } Hàm fflush Khai báo : Trích d n: int fflush(FILE *stream); Hàm dùng làm s ch vùng m c a t p . N u thành công hàm cho giá tr 0 , trái l i hàm tr v EOF Example Code: #include #include #include #include void flush(FILE *stream); int main(void) { FILE *stream; char msg[] = "This is a test"; /* tao 1 tep */ stream = fopen("DUMMY.FIL", "w"); /*ghi mot vai du lieu len tep */ fwrite(msg, strlen(msg), 1, stream); clrscr(); printf("Press any key to flush DUMMY.FIL:"); getch(); flush(stream); printf("\nFile was flushed, Press any key to quit:"); getch(); return 0; } void flush(FILE *stream) { int duphandle; /* lam sach vung dem */ fflush(stream); Mr.Vũ Kim Hi u 5
  6. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn duphandle = dup(fileno(stream)); close(duphandle); } Hàm fgetc Khai báo : Trích d n: int fgetc(FILE *stream); Hàm dùng l y 1 ký t t t p do con tr t p stream tr n . N u thành công hàm tr v mã ASCII c a ký t c ư c . N u g p l i hay g p k t thúc t p thì hàm tr v EOF . Example Code: #include #include #include int main(void) { FILE *stream; char string[] = "This is a test"; char ch; /* mo tep de ghi va doc */ stream = fopen("DUMMY.FIL", "w+"); /* ghi len tep mot chuoi */ fwrite(string, strlen(string), 1, stream); /* dat con tro tep len dau tep */ fseek(stream, 0, SEEK_SET); do { /* doc mot ky tu tu tep */ ch = fgetc(stream); /* in ra man hinh ky tu doc duoc */ putch(ch); } while (ch != EOF); fclose(stream); return 0; } Hàm fgetpos Khai báo : Trích d n: Mr.Vũ Kim Hi u 6
  7. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn int fgetpos( FILE *stream,fpos_t *pos ) Hàm dùng l y v trí con tr t p ( byte th m y ) lưu vào trong bi n pos . N u thành công hàm tr v 0 , ngư c l i hàm tr v giá tr khác 0 . Example Code: #include #include void showpos(FILE *stream); int main(void) { FILE *stream; fpos_t filepos; /* mo file de doc ghi */ stream = fopen("DUMMY.FIL", "w+"); /* luu lai vi tri con tro tep hien thoi */ fgetpos(stream, &filepos); /* ghi du lieu len tep */ fprintf(stream, "This is a test"); /* in ra man hinh vi tri con tro hien thoi */ showpos(stream); /* dat lai vi tri con tro tep */ if (fsetpos(stream, &filepos) == 0) showpos(stream); else { fprintf(stderr, "Error setting file pointer.\n"); exit(1); } /* dong tep*/ fclose(stream); return 0; } void showpos(FILE *stream) { fpos_t pos; /* in ra man hinh vi tri con tro tep hien thoi */ fgetpos(stream, &pos); printf("File position: %ld\n", pos); } Hàm fgets Khai báo : Trích d n: Mr.Vũ Kim Hi u 7
  8. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn char *fgets(char *s,int n,FILE *fp); Hàm dùng c m t chu i ký t dài n-1 ký t t t p fp vào chu i s . Vi c c k t thúc khi ã c n-1 ký t hay g p d u xu ng dòng hay g p k t thúc t p . Sau khi c , chu i s s ư c t ng thêm vào ký t NULL vào cu i chu i . Hàm tr v i ch chu i s n u thành công , trái l i tr v NULL . Example Code: #include #include int main(void) { FILE *stream; char string[] = "This is a test"; char msg[20]; /* mo tep de doc ghi */ stream = fopen("DUMMY.FIL", "w+"); /* ghi mot chuoi vao tep */ fwrite(string, strlen(string), 1, stream); /* dat con tro tep len dau tep */ fseek(stream, 0, SEEK_SET); /* doc mot chuoi tu tep */ fgets(msg, strlen(string)+1, stream); /* in chuoi ra man hinh */ printf("%s", msg); fclose(stream); return 0; } Hàm fopen Khai báo : Trích d n: FILE *fopen(char *filename,char *type ); Hàm dùng m t p . V i filename là chu i ch a tên t p c n m ( bao g m c ư ng d n c th ) . Lưu ý m t chút v i filename này . N u b n nh p chu i t bàn phím thì khác v i b n ánh s n nó trong code c a b n . Ví d t p ta c n m tên là abc.jpg n m C. i v i ánh tên t p t bàn phím thì ta ánh như sau : C:\abc.jpg . Nhưng n u ta so n s n trong code thì ta so n như sau : C:\\abc.jpg ( t c là thêm 1 g ch \ ) . Ki u m t p có r t nhi u cách nhưng chung quy ta có các trư ng h p sau cho ki u văn b n : "r" : c "w" : ghi "r+" hay "w+" : c và ghi "a" : ghi b sung "a+" : c ghi b sung Mr.Vũ Kim Hi u 8
  9. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn i v i m file nh phân b n ch c n thêm ch b vào sau các chu i trên . N u vi c m t p thành công hàm cho a ch con tr t p , trái l i hàm tr v NULL . Example Code: #include int main(void) { FILE *in, *out; if ((in = fopen("\\AUTOEXEC.BAT", "r")) == NULL) { fprintf(stderr, "Cannot open input file.\n"); return 1; } if ((out = fopen("\\AUTOEXEC.BAK", "w")) == NULL) { fprintf(stderr, "Cannot open output file.\n"); return 1; } while (!feof(in)) fputc(fgetc(in), out); fclose(in); fclose(out); return 0; } Hàm fprintf Khai báo : Trích d n: int fprintf(FILE *fp,char *s,...); Hàm này ch c năng y chang như hàm printf nhưng thay vì ghi ra màn hình thì nó ghi lên t p fp . N u thành công hàm tr v s byte ghi ư c lên t p , trái l i hàm tr v EOF . Example Code: #include int main(void) { FILE *stream; int i = 100; char c = 'C'; float f = 1.234; Mr.Vũ Kim Hi u 9
  10. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn /* mo tep de doc ghi */ stream = fopen("DUMMY.FIL", "w+"); /* ghi du lieu len tep */ fprintf(stream, "%d %c %f", i, c, f); /* dong tep */ fclose(stream); return 0; } Hàm fputc Khai báo : Trích d n: int fputc(int ch,FILE *fp); Hàm ghi m t ký t có mã ASCII là ch lên t p fp . khi thành công tr v mã ASCII c a ký t đư c ghi , trái l i hàm cho EOF . Example Code: #include int main(void) { char msg[] = "Hello world"; int i = 0; while (msg[i]) { fputc(msg[i], stdout); i++; } return 0; } Hàm fputs Khai báo : Trích d n: int fputs(char *s,FILE *fp); Hàm ghi lên t p fp chu i s . Chú ý r ng chu i s nên k t thúc b ng ký t NULL , n u ko vi c ghi lên có th b sai l ch . Ký t NULL này ch ra v trí k t thúc t p cho rõ ràng ch nó ko ư c ghi lên t p . Hàm tr v ký t cu i cùng ư c ghi lên t p n u thành công , trái l i hàm cho EOF . Example Code: Mr.Vũ Kim Hi u 10
  11. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn #include int main(void) { /* ghi mot chuoi ra dong xuat chuan ( man hinh ) */ fputs("Hello world\n", stdout); return 0; } Hàm fread Khai báo : Trích d n: int fread(void *ptr,int size,int n,FILE *fp); Hàm c n m u tin , m i m u có kích thư c size t t p fp lưu vào trong vùng nh ptr . Hàm tr v m t giá tr b ng s m u tin th c s c ư c . N u g p l i hay g p k t thúc t p hàm tr v 0 . Example Code: #include #include int main(void) { FILE *stream; char msg[] = "this is a test"; char buf[20]; if ((stream = fopen("DUMMY.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output file.\n"); return 1; } /* ghi du lieu len tep */ fwrite(msg, strlen(msg)+1, 1, stream); /* dat con tro tep len dau tep */ fseek(stream, SEEK_SET, 0); /* doc du lieu tu tep va xuat ra man hinh */ fread(buf, strlen(msg)+1, 1, stream); printf("%s\n", buf); fclose(stream); return 0; } Mr.Vũ Kim Hi u 11
  12. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn Hàm freopen Khai báo : Trích d n: FILE *freopen(char *filename,char *type,FILE *fp); Hàm này có ch c năng gán con tr t p fp vào t p filename . Sau ó có th m c ghi trên t p này thông qua con tr t p fp . Hàm này c bi t có ích khi ta mu n gán các con tr t p chu n ( stdin , stdout , stderr ) vào 1 t p c a ta . Hàm tr v NULL n u g p l i . Example Code: #include int main(void) { /* gan con tro tep chuan stdout ( man hinh ) vao tep output.fil */ if (freopen("OUTPUT.FIL", "w", stdout) == NULL) fprintf(stderr, "error redirecting stdout\n"); /* khi do cau lenh nay se khong con xuat ra man hinh nua ma xuat vao tep output.fil */ printf("This will go into a file."); /*dong tep stdout lai */ fclose(stdout); return 0; } Hàm fscanf Khai báo : Trích d n: int fscanf(FILE *fp,char *s,...); Hàm có ch c năng c d li u t t p . Làm vi c gi ng như hàm scanf nhưng thay vì c t màn hình thì hàm fscanf c t t p thôi . Hàm tr v 1 giá tr b ng s trư ng c ư c . Example Code: #include #include int main(void) { int i; printf("Input an integer: "); /* doc mot so nguyen tu dong vao chuan */ Mr.Vũ Kim Hi u 12
  13. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn if (fscanf(stdin, "%d", &i)) printf("The integer read was: %i\n", i); else { fprintf(stderr, "Error reading an integer from stdin.\n"); exit(1); } return 0; } Hàm fseek Khai báo : Trích d n: int fseek(FILE *fp,long sb,int xp); Hàm dùng di chuy n con tr t p fp n v trí b t kỳ . V i sb là s byte c n di chuy n , xp là v trí xu t phát . N u sb dương t c là di chuy n v cu i t p , n u âm thì di chuy n v u t p . N u xp là 0 hay SEEK_SET t c là xu t phát t u t p , n u là SEEK_CUR hay 1 t c là xu t phát t i v trí hi n th i c a con tr t p , n u là SEEK_END hay 2 t c là xu t phát t i cu i t p . Example Code: #include long filesize(FILE *stream); int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream)); fclose(stream); return 0; } long filesize(FILE *stream) { long curpos, length; curpos = ftell(stream); fseek(stream, 0L, SEEK_END); length = ftell(stream); fseek(stream, curpos, SEEK_SET); return length; } Mr.Vũ Kim Hi u 13
  14. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn Hàm fsetpos Khai báo : Trích d n: int fsetpos(FILE *stream , fpos_t pos); Hàm này thư ng i kèm cùng hàm fgetpos . Hàm dùng di chuy n con tr t p n v trí ươc lưu trong con tr pos . V trí này do hàm fgetpos lưu l i t trư c . Hàm tr v 0 n u thành công , ngư c l i tr v giá tr khác 0 . Example Code: #include #include void showpos(FILE *stream); int main(void) { FILE *stream; fpos_t filepos; /* mo file de doc ghi */ stream = fopen("DUMMY.FIL", "w+"); /* luu lai vi tri con tro tep hien thoi */ fgetpos(stream, &filepos); /* ghi du lieu len tep */ fprintf(stream, "This is a test"); /* in ra man hinh vi tri con tro hien thoi */ showpos(stream); /* dat lai vi tri con tro tep */ if (fsetpos(stream, &filepos) == 0) showpos(stream); else { fprintf(stderr, "Error setting file pointer.\n"); exit(1); } /* dong tep*/ fclose(stream); return 0; } void showpos(FILE *stream) { fpos_t pos; /* in ra man hinh vi tri con tro tep hien thoi */ fgetpos(stream, &pos); printf("File position: %ld\n", pos); Mr.Vũ Kim Hi u 14
  15. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn } Hàm ftell Khai báo : Trích d n: long ftell(FILE *fp); Khi thành công hàm cho vi t v trí con tr t p hi n th i ang v trí th m y ( tính t ut p), s hi u c a byte tính t 0 . Ngư c l i hàm tr v -1L . Nói thêm 1 tí v -1L có nghĩa là -1 mà lưu dư i d ng long ( dùng 4 byte lưu ) . Example Code: int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("The file pointer is at byte %ld\n", ftell(stream)); fclose(stream); return 0; } Hàm fwrite Khai báo : Trích d n: int fwrite( void *ptr,int size,int n,FILE *fp ); Hàm dùng ghi n m u tin kích thư c size byte t vùng nh ptr lên t p fp . Hàm tr v 1 giá tr b ng s m u tin th c s c ư c. Example Code: #include struct mystruct { int i; char ch; }; int main(void) { FILE *stream; struct mystruct s; Mr.Vũ Kim Hi u 15
  16. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* mo tep TEST.$$$ */ { fprintf(stderr, "Cannot open output file.\n"); return 1; } s.i = 0; s.ch = 'A'; fwrite(&s, sizeof(s), 1, stream); /* vi t c u trúc s lên tep */ fclose(stream); /* dong tep */ return 0; } 2. String & Character Danh sách các hàm v i String & Character 1.atof 2.atoi 3.atol 4.isalnum 5.isalpha 6.iscntrl 7.isdigit 8.isgraph 9.islower 10.isprint 11.ispunct 12.isspace 13.isupper 14.isxdigit 15.memchr 16.memcmp 17.memcpy 18.memmove Mr.Vũ Kim Hi u 16
  17. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn 19.memset 20.strcat 21.strchr 22.strcmp 23.strcoll 24.strcpy 25.strcspn 26.strerror 27.strlen 28.strncat 29.strncmp 30.strncpy 31.strpbrk 32.strrchr 33.strspn 34.strstr 35.strtod 36.strtok 37.strtol 38.strtoul 39.strxfrm 40.tolower 41.toupper 1. atof() Tên hàm: atof() nh nghĩa: PHP Code: Mr.Vũ Kim Hi u 17
  18. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn #include double atof( const char *str ); Chu i 'str' ph i b t u b ng kí t s , n u không s b l i chương trình (termination) Ví d v 3 trư ng h p PHP Code: #include #include int main(void) { char *strA="30.4"; char *strB="17.3 thu xem dc khong?"; char *strC="Cai nay chac co loi 33.4"; double a,b,c; a = atof(strA); b = atof(strB); //c = atof(strC);
  19. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn i = atoi( " 512 bottles of beer on the wall" ); printf("\nGia tri cua i la: %d",i); // Truong hop duoi day se in ra 0 i = atoi(" that bai 512 "); printf("\nGia tri cua i la: %d",i); return 0; } 3. atol() Tên hàm: atol() nh nghĩa: PHP Code: #include long atol( const char *str ); Tương t như atoi() Minh h a PHP Code: #include #include int main(void) { int i; i = atol( "512" ); printf("\nGia tri cua i la: %d",i); i = atol( "512.035" ); printf("\nGia tri cua i la: %d",i); i = atol( " 512.035" ); printf("\nGia tri cua i la: %d",i); i = atol( " 512+34" ); printf("\nGia tri cua i la: %d",i); i = atol( " 512 bottles of beer on the wall" ); printf("\nGia tri cua i la: %d",i); // Truong hop duoi day se in ra 0 i = atol(" that bai 512 "); printf("\nGia tri cua i la: %d",i); return 0; } 4. isalnum() Tên hàm: isalnum() nh nghĩa: PHP Code: #include int isalnum( int ch ); Ki m tra m t kí t là s hay ch cái. N u úng tr v giá tr khác 0 N u sai tr v 0 PHP Code: #include #include Mr.Vũ Kim Hi u 19
  20. Thö Vieän C Y!M: Information.Technology@yahoo.com.vn int main(void) { char x='X'; if(isalnum(x)) { printf("Day la ki tu %c",x); } else { printf("Ki tu %c ko phai la so hoac chu",x); } return 0; } 5.isalpha() Tên hàm: isalpha() nh nghĩa: PHP Code: #include int isalpha( int ch ); Ki m tra m t kí t có ph i là ch cái hay không. úng thì tr v khác 0 Sai thì tr v 0 PHP Code: #include #include int main(void) { char x='0'; if(isalpha(x)) { printf("Day la ki tu chu cai %c",x); } else { printf("Ki tu %c ko phai la chu cai",x); } return 0; } 6. iscntrl() Tên hàm: iscntrl() nh nghĩa: PHP Code: #include int iscntrl( int ch ); Ki m tra m t kí t có thu c nhóm kí t i u khi n hay không Nhóm kí t i u khi n (Control Character) n m t : 0x00 n 0x1F và kí t 0x7F úng thì tr v khác 0 Sai thì tr v 0 Mr.Vũ Kim Hi u 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2