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库函数 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库函数 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"); }