|
Turbo C 2.0、Borland C++库函数及用例(27) return 0; } 函数名: fgetchar 功 能: 从流中读取字符 用 法: int fgetchar(void); 程序例:#include <stdio.h>int main(void) { char ch; /* prompt the user for input */ printf("Enter a character followed by \ <Enter>: "); /* read the character from stdin */ ch = fgetchar(); /* display what was read */ printf("The character read is: '%c'\n", ch); return 0; } 函数名: fgetpos 功 能: 取得当前文件的句柄 用 法: int fgetpos(FILE *stream); 程序例:#include <string.h> #include <stdio.h>int main(void) { FILE *stream; char string[] = "This is a test"; fpos_t filepos; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* write a string into the file */ fwrite(string, strlen(string), 1, stream); /* report the file pointer position */ fgetpos(stream, &filepos); printf("The file pointer is at byte\ %ld\n", filepos); fclose(stream); return 0; } 函数名: fgets 功 能: 从流中读取一字符串 用 法: char *fgets(char *string, int n, FILE *stream); 程序例:#include <string.h> #include <stdio.h>int main(void) { FILE *stream; char string[] = "This is a test"; char msg[20]; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* write a string into the file */ fwrite(string, strlen(string), 1, stream); /* seek to the start of the file */ fseek(stream, 0, SEEK_SET); /* read a string from the file */ fgets(msg, strlen(string)+1, stream); /* display the string */ printf("%s", msg); fclose(stream); return 0; } 函数名: filelength 功 能: 取文件长度字节数 用 法: long filelength(int handle); 程序例:#include <string.h> #include <stdio.h> #include <fcntl.h> #include <io.h>int main(void) { int handle; char buf[11] = "0123456789"; /* create a file containing 10 bytes */ handle = open("DUMMY.FIL", O_CREAT); write(handle, buf, strlen(buf)); /* display the size of the file */ printf("file length in bytes: %ld\n", filelength(handle)); /* close the file */
|