C库函数 - setbuf()
C库函数 - setbuf() #include <stdio.h> int main () { char buf[BUFSIZ]; setbuf(stdout, buf); puts("This is codingdict"); fflush(stdout); return(0); }
阅读全文 ↗C库函数 - setbuf() #include <stdio.h> int main () { char buf[BUFSIZ]; setbuf(stdout, buf); puts("This is codingdict"); fflush(stdout); return(0); }
阅读全文 ↗C库函数 - rewind() #include <stdio.h> int main () { char str[] = "This is codingdict.com"; FILE *fp; int ch; /* First let's write some content in the file */ fp = fopen( "file.txt" , "w" ); fwrite(str , 1 , sizeof(str) , fp ); fclose(fp); fp = fopen( "file.txt" , "r" ); while(1) { ch = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", ch); } rewind(fp); printf("\n"); while(1) { ch = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", ch); } fclose(fp); return(0); }
阅读全文 ↗C库函数 - freopen() #include <stdio.h> int main () { FILE *fp; printf("This text is redirected to stdout\n"); fp = freopen("file.txt", "w+", stdout); printf("This text is redirected to file.txt\n"); fclose(fp); return(0); }
阅读全文 ↗C库函数 - fread() #include <stdio.h> #include <string.h> int main () { FILE *fp; char c[] = "this is codingdict"; char buffer[100]; /* Open file for both reading and writing */ fp = fopen("file.txt", "w+"); /* Write data to the file */ fwrite(c, strlen(c) + 1, 1, fp); /* Seek to the beginning of the file */ fseek(fp, 0, SEEK_SET); /* Read and display data */ fread(buffer, strlen(c)+1, 1, fp); printf("%s\n", buffer); fclose(fp); return(0); }
阅读全文 ↗C库函数 - ferror() #include <stdio.h> int main () { FILE *fp; char c; fp = fopen("file.txt", "w"); c = fgetc(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } clearerr(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } fclose(fp); return(0); }
阅读全文 ↗C库函数 - clearerr() #include <stdio.h> int main () { FILE *fp; char c; fp = fopen("file.txt", "w"); c = fgetc(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } clearerr(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } fclose(fp); return(0); }
阅读全文 ↗C库宏 - offsetof() #include <stddef.h> #include <stdio.h> struct address { char name[50]; char street[50]; int phone; }; int main () { printf("name offset = %d byte in address structure.\n", offsetof(struct address, name)); printf("street offset = %d byte in address structure.\n", offsetof(struct address, street)); printf("phone offset = %d byte in address structure.\n", offsetof(struct address, phone)); return(0); }
阅读全文 ↗C库宏 - NULL() #include <stddef.h> #include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp != NULL ) { printf("Opend file file.txt successfully\n"); fclose(fp); } fp = fopen("nofile.txt", "r"); if( fp == NULL ) { printf("Could not open file nofile.txt\n"); } return(0); }
阅读全文 ↗C库宏 - va_end() #include <stdarg.h> #include <stdio.h> int mul(int, ...); int main () { printf("15 * 12 = %d\n", mul(2, 15, 12) ); return 0; } int mul(int num_args, ...) { int val = 1; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) { val *= va_arg(ap, int); } va_end(ap); return val; }
阅读全文 ↗C库宏 - va_arg() #include <stdarg.h> #include <stdio.h> int sum(int, ...); int main () { printf("Sum of 15 and 56 = %d\n", sum(2, 15, 56) ); return 0; } int sum(int num_args, ...) { int val = 0; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) { val += va_arg(ap, int); } va_end(ap); return val; }
阅读全文 ↗C库宏 - va_start() #include<stdarg.h> #include<stdio.h> int sum(int, ...); int main(void) { printf("Sum of 10, 20 and 30 = %d\n", sum(3, 10, 20, 30) ); printf("Sum of 4, 20, 25 and 30 = %d\n", sum(4, 4, 20, 25, 30) ); return 0; } int sum(int num_args, ...) { int val = 0; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) { val += va_arg(ap, int); } va_end(ap); return val; }
阅读全文 ↗C库函数 - fabs() #include <stdio.h> #include <math.h> int main () { int a, b; a = 1234; b = -344; printf("The absolute value of %d is %lf\n", a, fabs(a)); printf("The absolute value of %d is %lf\n", b, fabs(b)); return(0); }
阅读全文 ↗