编程札记

C

C库函数 remove()

C库函数 remove() #include <stdio.h> #include <string.h> int main () { int ret; FILE *fp; char filename[] = "file.txt"; fp = fopen(filename, "w"); fprintf(fp, "%s", "This is codingdict.com"); fclose(fp); ret = remove(filename); if(ret == 0) { printf("File deleted successfully"); } else { printf("Error: unable to delete the file"); } return(0); }

阅读全文 ↗
C

C库 freopen()

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

C库函数 fread()

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

C库函数 fflush()

C库函数 fflush() #include <stdio.h> #include <string.h> #include <unistd.h> int main () { char buff[1024]; memset( buff, '\0', sizeof( buff )); fprintf(stdout, "Going to set full buffering on\n"); setvbuf(stdout, buff, _IOFBF, 1024); fprintf(stdout, "This is codingdict.com\n"); fprintf(stdout, "This output will go into buff\n"); fflush( stdout ); fprintf(stdout, "and this will appear when programm\n"); fprintf(stdout, "will come after sleeping 5 seconds\n"); sleep(5); return(0); }

阅读全文 ↗
C

C库函数 ferror()

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

C库函数 clearerr()

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

C库宏 offsetof()

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

C库宏 NULL()

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

C库宏 va_end()

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

C库宏 va_arg()

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

C库宏 va_start()

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

C库函数 raise()

C库函数 raise() #include <signal.h> #include <stdio.h> #include <stdlib.h> void signal_catchfunc(int); int main () { int ret; ret = signal(SIGINT, signal_catchfunc); if( ret == SIG_ERR) { printf("Error: unable to set signal handler.\n"); exit(0); } printf("Going to raise a signal\n"); ret = raise(SIGINT); if( ret !=0 ) { printf("Error: unable to raise SIGINT signal.\n"); exit(0); } printf("Exiting...\n"); return(0); } void signal_catchfunc(int signal) { printf("!! signal caught !!\n"); }

阅读全文 ↗