编程札记

C

C库函数 sscanf()

C库函数 sscanf() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { int day, year; char weekday[20], month[20], dtm[100]; strcpy( dtm, "Saturday March 25 1989" ); sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year ); printf("%s %d, %d = %s\n", month, day, year, weekday ); return(0); }

阅读全文 ↗
C

C库函数 fscanf()

C库函数 fscanf() #include <stdio.h> #include <stdlib.h> int main () { char str1[10], str2[10], str3[10]; int year; FILE * fp; fp = fopen ("file.txt", "w+"); fputs("We are in 2012", fp); rewind(fp); fscanf(fp, "%s %s %s %d", str1, str2, str3, &year); printf("Read String1 |%s|\n", str1 ); printf("Read String2 |%s|\n", str2 ); printf("Read String3 |%s|\n", str3 ); printf("Read Integer |%d|\n", year ); fclose(fp); return(0); }

阅读全文 ↗
C

C库函数 vsprintf()

C库函数 vsprintf() #include <stdio.h> #include <stdarg.h> char buffer[80]; int vspfunc(char *format, ...) { va_list aptr; int ret; va_start(aptr, format); ret = vsprintf(buffer, format, aptr); va_end(aptr); return(ret); } int main () { int i = 5; float f = 27.0; char str[50] = "tutoriaspoint.com"; vspfunc("%d %f %s", i, f, str); printf("%s\n", buffer); return(0); }

阅读全文 ↗
C

C库函数 vprintf()

C库函数 vprintf() #include <stdio.h> #include <stdarg.h> void WriteFrmtd(char *format, ...) { va_list args; va_start(args, format); vprintf(format, args); va_end(args); } int main () { WriteFrmtd("%d variable argument\n", 1); WriteFrmtd("%d variable %s\n", 2, "arguments"); return(0); }

阅读全文 ↗
C

C库函数 sprintf()

C库函数 sprintf() #include <stdio.h> #include <math.h> int main () { char str[80]; sprintf(str, "Value of Pi = %f", M_PI); puts(str); return(0); }

阅读全文 ↗
C

C库函数 printf()

C库函数 printf() #include <stdio.h> int main () { int ch; for( ch = 75 ; ch <= 100; ch++ ) { printf("ASCII value = %d, Character = %c\n", ch , ch ); } return(0); }

阅读全文 ↗
C

C库函数 tmpnam()

C库函数 tmpnam() #include <stdio.h> int main () { char buffer[L_tmpnam]; char *ptr; tmpnam(buffer); printf("Temporary name 1: %s\n", buffer); ptr = tmpnam(NULL); printf("Temporary name 2: %s\n", ptr); return(0); }

阅读全文 ↗
C

C库函数 tmpfile()

C库函数 tmpfile() #include <stdio.h> int main () { FILE *fp; fp = tmpfile(); printf("Temporary file created\n"); /* you can use tmp file here */ fclose(fp); return(0); }

阅读全文 ↗
C

C库函数 setvbuf()

C库函数 setvbuf() #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库函数 setbuf()

C库函数 setbuf() #include <stdio.h> int main () { char buf[BUFSIZ]; setbuf(stdout, buf); puts("This is codingdict"); fflush(stdout); return(0); }

阅读全文 ↗
C

C库函数 rewind()

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

阅读全文 ↗