📄 ff.htm
字号:
fseek(stream, SEEK_SET, 0); </p>
<p> /* read the data and display it */ <br>
fread(buf, strlen(msg)+1, 1, stream); <br>
printf("%s\n", buf); </p>
<p> fclose(stream); <br>
return 0; <br>
} <br>
<br>
</p>
<p>函数名: free <br>
功 能: 释放已分配的块 <br>
用 法: void free(void *ptr); <br>
程序例: </p>
<p>#include <string.h> <br>
#include <stdio.h> <br>
#include <alloc.h> </p>
<p>int main(void) <br>
{ <br>
char *str; </p>
<p> /* allocate memory for string */ <br>
str = malloc(10); </p>
<p> /* copy "Hello" to string */ <br>
strcpy(str, "Hello"); </p>
<p> /* display string */ <br>
printf("String is %s\n", str); </p>
<p> /* free memory */ <br>
free(str); </p>
<p> return 0; <br>
} <br>
</p>
<p>函数名: freemem <br>
功 能: 释放先前分配的DOS内存块 <br>
用 法: int freemem(unsigned seg); <br>
程序例: </p>
<p>#include <dos.h> <br>
#include <alloc.h> <br>
#include <stdio.h> </p>
<p>int main(void) <br>
{ <br>
unsigned int size, segp; <br>
int stat; </p>
<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); </p>
<p> return 0; <br>
} <br>
<br>
</p>
<p>函数名: freopen <br>
功 能: 替换一个流 <br>
用 法: FILE *freopen(char *filename, char *type,
FILE *stream); <br>
程序例: </p>
<p>#include <stdio.h> </p>
<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"); </p>
<p> /* this output will go to a file */ <br>
printf("This will go into a
file."); </p>
<p> /* close the standard output stream */ <br>
fclose(stdout); </p>
<p> return 0; <br>
} <br>
<br>
<br>
</p>
<p>函数名: frexp <br>
功 能:
把一个双精度数分解为尾数的指数 <br>
用 法: double frexp(double value, int *eptr); <br>
程序例: </p>
<p>#include <math.h> <br>
#include <stdio.h> </p>
<p>int main(void) <br>
{ <br>
double mantissa, number; <br>
int exponent; </p>
<p> number = 8.0; <br>
mantissa = frexp(number, &exponent); </p>
<p> printf("The number %lf is ",
number); <br>
printf("%lf times two to the ",
mantissa); <br>
printf("power of %d\n", exponent);
</p>
<p> return 0; <br>
} <br>
<br>
</p>
<p>函数名: fscanf <br>
功 能: 从一个流中执行格式化输入 <br>
用 法: int fscanf(FILE *stream, char
*format[,argument...]); <br>
程序例: </p>
<p>#include <stdlib.h> <br>
#include <stdio.h> </p>
<p>int main(void) <br>
{ <br>
int i; </p>
<p> printf("Input an integer: "); </p>
<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>
</p>
<p>函数名: fseek <br>
功 能: 重定位流上的文件指针 <br>
用 法: int fseek(FILE *stream, long offset, int
fromwhere); <br>
程序例: </p>
<p>#include <stdio.h> </p>
<p>long filesize(FILE *stream); </p>
<p>int main(void) <br>
{ <br>
FILE *stream; </p>
<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>
} </p>
<p>long filesize(FILE *stream) <br>
{ <br>
long curpos, length; </p>
<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>
</p>
<p>函数名: fsetpos <br>
功 能: 定位流上的文件指针 <br>
用 法: int fsetpos(FILE *stream, const fpos_t
*pos); <br>
程序例: </p>
<p>#include <stdlib.h> <br>
#include <stdio.h> </p>
<p>void showpos(FILE *stream); </p>
<p>int main(void) <br>
{ <br>
FILE *stream; <br>
fpos_t filepos; </p>
<p> /* open a file for update */ <br>
stream = fopen("DUMMY.FIL",
"w+"); </p>
<p> /* save the file pointer position */ <br>
fgetpos(stream, &filepos); </p>
<p> /* write some data to the file */ <br>
fprintf(stream, "This is a test");
</p>
<p> /* show the current file position */ <br>
showpos(stream); </p>
<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>
} </p>
<p> /* close the file */ <br>
fclose(stream); <br>
return 0; <br>
} </p>
<p>void showpos(FILE *stream) <br>
{ <br>
fpos_t pos; </p>
<p> /* display the current file pointer <br>
position of a stream */ <br>
fgetpos(stream, &pos); <br>
printf("File position: %ld\n",
pos); <br>
} <br>
</p>
<p>函数名: fstat <br>
功 能: 获取打开文件信息 <br>
用 法: int fstat(char *handle, struct stat
*buff); <br>
程序例: </p>
<p>#include <sys\stat.h> <br>
#include <stdio.h> <br>
#include <time.h> </p>
<p>int main(void) <br>
{ <br>
struct stat statbuf; <br>
FILE *stream; </p>
<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); </p>
<p> /* get information about the file */ <br>
fstat(fileno(stream), &statbuf); <br>
fclose(stream); </p>
<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"); </p>
<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>
</p>
<p>函数名: ftell <br>
功 能: 返回当前文件指针 <br>
用 法: long ftell(FILE *stream); <br>
程序例: </p>
<p>#include <stdio.h> </p>
<p>int main(void) <br>
{ <br>
FILE *stream; </p>
<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>
</p>
<p>函数名: fwrite <br>
功 能: 写内容到流中 <br>
用 法: int fwrite(void *ptr, int size, int
nitems, FILE *stream); <br>
程序例: </p>
<p>#include <stdio.h> </p>
<p>struct mystruct <br>
{ <br>
int i; <br>
char ch; <br>
}; </p>
<p>int main(void) <br>
{ <br>
FILE *stream; <br>
struct mystruct s; </p>
<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>
</p>
</td>
</tr>
</table>
</center></div><div align="center"><center>
<table border="0" cellspacing="1" width="640">
<tr>
<td class="p9" height="60"> <script>
document.write("<p><a href=\"http://view.gznet.com/cgi-bin/rl_views.cgi?UID=10013421\" target=sxrl>");
document.write("<img src=\"http://refer.gznet.com/cgi-bin/rl_refer2.cgi?UID=10013421&refer="+escape(top.document.referrer)+"\" width=1 height=1 border=0 alt=\" \">");
document.write("</a>");
</script></td>
</tr>
</table>
</center></div>
<p> </p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -