📄 025.htm
字号:
<BR>
<BR>
<P>函数名: FP_OFF
<BR>功 能: 获取远地址偏移量
<BR>用 法: unsigned FP_OFF(void far *farptr);
<BR>程序例:
<BR>
<P>/* FP_OFF */
<BR>
<P>#include <dos.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> char *str = "fpoff.c";
<BR>
<P> printf("The offset of this file in memory\
<BR> is: %Fp\n",
FP_OFF(str));
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: FP_SEG
<BR>功 能: 获取远地址段值
<BR>用 法: unsigned FP_SEG(void far *farptr);
<BR>程序例:
<BR>
<P>/* FP_SEG */
<BR>
<P>#include <dos.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> char *filename = "fpseg.c";
<BR>
<P> printf("The offset of this file in memory\
<BR> is: %Fp\n", FP_SEG(filename));
<BR>
<P> return(0);
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: fputc
<BR>功 能: 送一个字符到一个流中
<BR>用 法: int fputc(int ch, FILE *stream);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> char msg[] = "Hello world";
<BR> int i = 0;
<BR>
<P> while (msg[i])
<BR> {
<BR> fputc(msg[i], stdout);
<BR> i++;
<BR> }
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: fputchar
<BR>功 能: 送一个字符到标准输出流(stdout)中
<BR>用 法: int fputchar(char ch);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> char msg[] = "This is a test";
<BR> int i = 0;
<BR>
<P> while (msg[i])
<BR> {
<BR> fputchar(msg[i]);
<BR> i++;
<BR> }
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: fputs
<BR>功 能: 送一个字符到一个流中
<BR>用 法: int fputs(char *string, FILE *stream);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> /* write a string to standard output */
<BR> fputs("Hello world\n", stdout);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: fread
<BR>功 能: 从一个流中读数据
<BR>用 法: int fread(void *ptr, int size, int nitems, FILE *stream);
<BR>程序例:
<BR>
<P>#include <string.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> FILE *stream;
<BR> char msg[] = "this is a test";
<BR> char buf[20];
<BR>
<P> if ((stream = fopen("DUMMY.FIL", "w+"))
<BR> == NULL)
<BR> {
<BR> fprintf(stderr,
<BR>
"Cannot open output file.\n");
<BR> return 1;
<BR> }
<BR>
<P> /* write some data to the file */
<BR> fwrite(msg, strlen(msg)+1, 1, stream);
<BR>
<P> /* seek to the beginning of the file */
<BR> fseek(stream, SEEK_SET, 0);
<BR>
<P> /* read the data and display it */
<BR> fread(buf, strlen(msg)+1, 1, stream);
<BR> printf("%s\n", buf);
<BR>
<P> fclose(stream);
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: free
<BR>功 能: 释放已分配的块
<BR>用 法: void free(void *ptr);
<BR>程序例:
<BR>
<P>#include <string.h>
<BR>#include <stdio.h>
<BR>#include <alloc.h>
<BR>
<P>int main(void)
<BR>{
<BR> char *str;
<BR>
<P> /* allocate memory for string */
<BR> str = malloc(10);
<BR>
<P> /* copy "Hello" to string */
<BR> strcpy(str, "Hello");
<BR>
<P> /* display string */
<BR> printf("String is %s\n", str);
<BR>
<P> /* free memory */
<BR> free(str);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<P>函数名: freemem
<BR>功 能: 释放先前分配的DOS内存块
<BR>用 法: int freemem(unsigned seg);
<BR>程序例:
<BR>
<P>#include <dos.h>
<BR>#include <alloc.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> unsigned int size, segp;
<BR> int stat;
<BR>
<P> size = 64; /* (64 x 16) = 1024 bytes */
<BR> stat = allocmem(size, &segp);
<BR> if (stat < 0)
<BR> printf("Allocated memory at segment:\
<BR> %x\n", segp);
<BR> else
<BR> printf("Failed: maximum number of\
<BR> paragraphs available is %u\n",
<BR> stat);
<BR> freemem(segp);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: freopen
<BR>功 能: 替换一个流
<BR>用 法: FILE *freopen(char *filename, char *type, FILE *stream);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> /* redirect standard output to a file */
<BR> if (freopen("OUTPUT.FIL", "w", stdout)
<BR> == NULL)
<BR> fprintf(stderr, "error redirecting\
<BR>
stdout\n");
<BR>
<P> /* this output will go to a file */
<BR> printf("This will go into a file.");
<BR>
<P> /* close the standard output stream */
<BR> fclose(stdout);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: frexp
<BR>功 能: 把一个双精度数分解为尾数的指数
<BR>用 法: double frexp(double value, int *eptr);
<BR>程序例:
<BR>
<P>#include <math.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> double mantissa, number;
<BR> int exponent;
<BR>
<P> number = 8.0;
<BR> mantissa = frexp(number, &exponent);
<BR>
<P> printf("The number %lf is ", number);
<BR> printf("%lf times two to the ", mantissa);
<BR> printf("power of %d\n", exponent);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: fscanf
<BR>功 能: 从一个流中执行格式化输入
<BR>用 法: int fscanf(FILE *stream, char *format[,argument...]);
<BR>程序例:
<BR>
<P>#include <stdlib.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> int i;
<BR>
<P> printf("Input an integer: ");
<BR>
<P> /* read an integer from the
<BR> standard input stream */
<BR> if (fscanf(stdin, "%d", &i))
<BR> printf("The integer read was: %i\n",
<BR>
i);
<BR> else
<BR> {
<BR> fprintf(stderr, "Error reading an \
<BR>
integer from stdin.\n");
<BR> exit(1);
<BR> }
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: fseek
<BR>功 能: 重定位流上的文件指针
<BR>用 法: int fseek(FILE *stream, long offset, int fromwhere);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>long filesize(FILE *stream);
<BR>
<P>int main(void)
<BR>{
<BR> FILE *stream;
<BR>
<P> stream = fopen("MYFILE.TXT", "w+");
<BR> fprintf(stream, "This is a test");
<BR> printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
<BR> fclose(stream);
<BR> return 0;
<BR>}
<BR>
<P>long filesize(FILE *stream)
<BR>{
<BR> long curpos, length;
<BR>
<P> curpos = ftell(stream);
<BR> fseek(stream, 0L, SEEK_END);
<BR> length = ftell(stream);
<BR> fseek(stream, curpos, SEEK_SET);
<BR> return length;
<BR>}
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<P>函数名: fsetpos
<BR>功 能: 定位流上的文件指针
<BR>用 法: int fsetpos(FILE *stream, const fpos_t *pos);
<BR>程序例:
<BR>
<P>#include <stdlib.h>
<BR>#include <stdio.h>
<BR>
<P>void showpos(FILE *stream);
<BR>
<P>int main(void)
<BR>{
<BR> FILE *stream;
<BR> fpos_t filepos;
<BR>
<P> /* open a file for update */
<BR> stream = fopen("DUMMY.FIL", "w+");
<BR>
<P> /* save the file pointer position */
<BR> fgetpos(stream, &filepos);
<BR>
<P> /* write some data to the file */
<BR> fprintf(stream, "This is a test");
<BR>
<P> /* show the current file position */
<BR> showpos(stream);
<BR>
<P> /* set a new file position, display it */
<BR> if (fsetpos(stream, &filepos) == 0)
<BR> showpos(stream);
<BR> else
<BR> {
<BR> fprintf(stderr, "Error setting file
\
<BR> pointer.\n");
<BR> exit(1);
<BR> }
<BR>
<P> /* close the file */
<BR> fclose(stream);
<BR> return 0;
<BR>}
<BR>
<P>void showpos(FILE *stream)
<BR>{
<BR> fpos_t pos;
<BR>
<P> /* display the current file pointer
<BR> position of a stream */
<BR> fgetpos(stream, &pos);
<BR> printf("File position: %ld\n", pos);
<BR>}
<BR>
<BR>
<P>函数名: fstat
<BR>功 能: 获取打开文件信息
<BR>用 法: int fstat(char *handle, struct stat *buff);
<BR>程序例:
<BR>
<P>#include <sys\stat.h>
<BR>#include <stdio.h>
<BR>#include <time.h>
<BR>
<P>int main(void)
<BR>{
<BR> struct stat statbuf;
<BR> FILE *stream;
<BR>
<P> /* open a file for update */
<BR> if ((stream = fopen("DUMMY.FIL", "w+"))
<BR> == NULL)
<BR> {
<BR> fprintf(stderr, "Cannot open output
\
<BR>
file.\n");
<BR> return(1);
<BR> }
<BR> fprintf(stream, "This is a test");
<BR> fflush(stream);
<BR>
<P> /* get information about the file */
<BR> fstat(fileno(stream), &statbuf);
<BR> fclose(stream);
<BR>
<P> /* display the information returned */
<BR> if (statbuf.st_mode & S_IFCHR)
<BR> printf("Handle refers to a device.\n");
<BR> if (statbuf.st_mode & S_IFREG)
<BR> printf("Handle refers to an ordinary
\
<BR>
file.\n");
<BR> if (statbuf.st_mode & S_IREAD)
<BR> printf("User has read permission on
\
<BR>
file.\n");
<BR> if (statbuf.st_mode & S_IWRITE)
<BR> printf("User has write permission on
\
<BR>
file.\n");
<BR>
<P> printf("Drive letter of file: %c\n",
<BR> 'A'+statbuf.st_dev);
<BR> printf("Size of file in bytes: %ld\n",
<BR> statbuf.st_size);
<BR> printf("Time file last opened: %s\n",
<BR> ctime(&statbuf.st_ctime));
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: ftell
<BR>功 能: 返回当前文件指针
<BR>用 法: long ftell(FILE *stream);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> FILE *stream;
<BR>
<P> stream = fopen("MYFILE.TXT", "w+");
<BR> fprintf(stream, "This is a test");
<BR> printf("The file pointer is at byte \
<BR> %ld\n", ftell(stream));
<BR> fclose(stream);
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: fwrite
<BR>功 能: 写内容到流中
<BR>用 法: int fwrite(void *ptr, int size, int nitems, FILE *stream);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>
<P>struct mystruct
<BR>{
<BR> int i;
<BR> char ch;
<BR>};
<BR>
<P>int main(void)
<BR>{
<BR> FILE *stream;
<BR> struct mystruct s;
<BR>
<P> if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open
file TEST.$$$ */
<BR> {
<BR> fprintf(stderr, "Cannot open output
file.\n");
<BR> return 1;
<BR> }
<BR> s.i = 0;
<BR> s.ch = 'A';
<BR> fwrite(&s, sizeof(s), 1, stream); /* write struct
s to file */
<BR> fclose(stream); /* close file */
<BR> return 0;
<BR>}
<BR>
<BR>
<hr color="#EE9B73" size="1" width="94%">
</TD>
<TD CLASS="tt3" VALIGN="bottom" width="8%" bgcolor="#e0e0e0"><strong><A HREF="026.htm">后一页</A><BR>
<A HREF="024.htm">前一页</A><BR>
<A HREF="index.html">回目录</A><BR>
<A HREF="../../../../index.htm">回首页</A><BR>
</strong>
</TD>
</TR>
</table>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -