📄 王大刚--c语言编程宝典--f.htm
字号:
<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 width="94%" color=#ee9b73 SIZE=1>
</TD>
<TD class=tt3 vAlign=bottom width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/026.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/024.htm">前一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A
href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -