|
Turbo C 2.0、Borland C++库函数及用例(32) 功 能: 送一个字符到标准输出流(stdout)中 用 法: int fputchar(char ch); 程序例:#include <stdio.h>int main(void) { char msg[] = "This is a test"; int i = 0; while (msg[i]) { fputchar(msg[i]); i++; } return 0; } 函数名: fputs 功 能: 送一个字符到一个流中 用 法: int fputs(char *string, FILE *stream); 程序例:#include <stdio.h> int main(void) { /* write a string to standard output */ fputs("Hello world\n", stdout); return 0; } 函数名: fread 功 能: 从一个流中读数据 用 法: int fread( void *ptr, int size, int nitems, FILE *stream ); 程序例:#include <string.h> #include <stdio.h>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; } /* write some data to the file */ fwrite(msg, strlen(msg)+1, 1, stream); /* seek to the beginning of the file */ fseek(stream, SEEK_SET, 0); /* read the data and display it */ fread(buf, strlen(msg)+1, 1, stream); printf("%s\n", buf); fclose(stream); return 0; } 函数名: free 功 能: 释放已分配的块 用 法: void free(void *ptr); 程序例:#include <string.h> #include <stdio.h> #include <alloc.h>int main(void) { char *str; /* allocate memory for string */ str = malloc(10); /* copy "Hello" to string */ strcpy(str, "Hello"); /* display string */ printf("String is %s\n", str); /* free memory */ free(str); return 0; } 函数名: freemem 功 能: 释放先前分配的DOS内存块 用 法: int freemem(unsigned seg); 程序例:#include <dos.h> #include <alloc.h> #include <stdio.h>int main(void) { unsigned int size, segp; int stat; size = 64; /* (64 x 16) = 1024 bytes */ stat = allocmem(size, &segp); if (stat < 0) printf("Allocated memory at segment:\ %x\n", segp); else printf("Failed: maximum number of\ paragraphs available is %u\n", stat); freemem(segp); return 0;
|