📄 王大刚--c语言编程宝典--r.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.hjflying.8u8.com/cl/035.htm -->
<HTML><HEAD><TITLE>王大刚-->C语言编程宝典-->R</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GB2312">
<META content="王大刚 C语言编程宝典 R" name=keywords>
<META content="王大刚 - C语言编程宝典 - R" name=description>
<STYLE>#page {
LEFT: 0px; POSITION: absolute; TOP: 0px
}
.tt3 {
FONT: 9pt/12pt "宋体"
}
.tt2 {
FONT: 12pt/15pt "宋体"
}
A {
TEXT-DECORATION: none
}
A:hover {
COLOR: blue; TEXT-DECORATION: underline
}
</STYLE>
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY text=#000000 vLink=#006699 aLink=#9900ff link=#006699 bgColor=#ffffff
leftMargin=3 topMargin=3 marginwidth="3" marginheight="3">
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
<TBODY>
<TR>
<TD class=tt3 vAlign=top width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/036.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/034.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>
<TD class=tt2 width="84%" bgColor=#f5f8f8>
<CENTER><B><FONT style="FONT-SIZE: 16.5pt" face=楷体_GB2312
color=#ff6666>R</FONT></B></CENTER>
<HR width="94%" color=#ee9b73 SIZE=1>
<P>函数名: raise <BR>功 能: 向正在执行的程序发送一个信号 <BR>用 法: int raise(int
sig); <BR>程序例: <BR>
<P>#include <signal.h> <BR>
<P>int main(void) <BR>{ <BR> int a, b; <BR>
<P> a = 10; <BR> b = 0; <BR> if (b ==
0) <BR> /* preempt divide by zero error */
<BR> raise(SIGFPE); <BR> a = a /
b; <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: rand <BR>功 能: 随机数发生器 <BR>用 法: void rand(void);
<BR>程序例: <BR>
<P>#include <stdlib.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> int i; <BR>
<P> printf("Ten random numbers from 0 to 99\n\n");
<BR> for(i=0; i<10; i++) <BR>
printf("%d\n", rand() % 100); <BR> return 0; <BR>} <BR>
<BR> <BR>
<P>函数名: randbrd <BR>功 能: 随机块读 <BR>用 法: int randbrd(struct fcb
*fcbptr, int reccnt); <BR>程序例: <BR>
<P>#include <process.h> <BR>#include <string.h> <BR>#include
<stdio.h> <BR>#include <dos.h> <BR>
<P>int main(void) <BR>{ <BR> char far *save_dta;
<BR> char line[80], buffer[256]; <BR> struct fcb
blk; <BR> int i, result; <BR>
<P> /* get user input file name for dta */ <BR>
printf("Enter drive and file name (no path - i.e. a:file.dat)\n");
<BR> gets(line); <BR>
<P> /* put file name in fcb */ <BR> if
(!parsfnm(line, &blk, 1)) <BR> {
<BR> printf("Error in call to parsfnm\n");
<BR> exit(1); <BR> }
<BR> printf("Drive #%d File: %s\n\n", blk.fcb_drive,
blk.fcb_name); <BR>
<P> /* open file with DOS FCB open file */ <BR>
bdosptr(0x0F, &blk, 0); <BR>
<P> /* save old dta, and set new one */ <BR>
save_dta = getdta(); <BR> setdta(buffer); <BR>
<P> /* set up info for the new dta */ <BR>
blk.fcb_recsize = 128; <BR> blk.fcb_random = 0L;
<BR> result = randbrd(&blk, 1); <BR>
<P> /* check results from randbrd */ <BR> if
(!result) <BR> printf("Read OK\n\n");
<BR> else <BR> {
<BR> perror("Error during read");
<BR> exit(1); <BR> } <BR>
<P> /* read in data from the new dta */ <BR>
printf("The first 128 characters are:\n"); <BR> for (i=0;
i<128; i++) <BR> putchar(buffer[i]); <BR>
<P> /* restore previous dta */ <BR>
setdta(save_dta); <BR>
<P> return 0; <BR>} <BR> <BR>
<P>函数名: randbwr <BR>功 能: 随机块写 <BR>用 法: int randbwr(struct fcp
*fcbptr, int reccnt); <BR>程序例: <BR>
<P>#include <process.h> <BR>#include <string.h> <BR>#include
<stdio.h> <BR>#include <dos.h> <BR>
<P>int main(void) <BR>{ <BR> char far *save_dta;
<BR> char line[80]; <BR> char buffer[256] =
"RANDBWR test!"; <BR> struct fcb blk; <BR> int
result; <BR>
<P> /* get new file name from user */ <BR>
printf("Enter a file name to create (no path - ie. a:file.dat\n");
<BR> gets(line); <BR>
<P> /* parse the new file name to the dta */ <BR>
parsfnm(line,&blk,1); <BR> printf("Drive #%d File:
%s\n", blk.fcb_drive, blk.fcb_name); <BR>
<P> /* request DOS services to create file */ <BR>
if (bdosptr(0x16, &blk, 0) == -1) <BR> {
<BR> perror("Error creating file");
<BR> exit(1); <BR> } <BR>
<P> /* save old dta and set new dta */ <BR>
save_dta = getdta(); <BR> setdta(buffer); <BR>
<P> /* write new records */ <BR> blk.fcb_recsize =
256; <BR> blk.fcb_random = 0L; <BR> result =
randbwr(&blk, 1); <BR>
<P> if (!result) <BR>
printf("Write OK\n"); <BR> else <BR> {
<BR> perror("Disk error");
<BR> exit(1); <BR> } <BR>
<P> /* request DOS services to close the file */
<BR> if (bdosptr(0x10, &blk, 0) == -1) <BR> {
<BR> perror("Error closing file");
<BR> exit(1); <BR> } <BR>
<P> /* reset the old dta */ <BR> setdta(save_dta);
<BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: random <BR>功 能: 随机数发生器 <BR>用 法: int random(int num);
<BR>程序例: <BR>
<P>#include <stdlib.h> <BR>#include <stdio.h> <BR>#include
<time.h> <BR>
<P>/* prints a random number in the range 0 to 99 */ <BR>int main(void)
<BR>{ <BR> randomize(); <BR> printf("Random number
in the 0-99 range: %d\n", random (100)); <BR> return 0; <BR>}
<BR> <BR> <BR>
<P>函数名: randomize <BR>功 能: 初始化随机数发生器 <BR>用 法: void
randomize(void); <BR>程序例: <BR>
<P>#include <stdlib.h> <BR>#include <stdio.h> <BR>#include
<time.h> <BR>
<P>int main(void) <BR>{ <BR> int i; <BR>
<P> randomize(); <BR> printf("Ten random numbers
from 0 to 99\n\n"); <BR> for(i=0; i<10; i++)
<BR> printf("%d\n", rand() % 100);
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: read <BR>功 能: 从文件中读 <BR>用 法: int read(int handle, void
*buf, int nbyte); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <io.h> <BR>#include
<alloc.h> <BR>#include <fcntl.h> <BR>#include
<process.h> <BR>#include <sys\stat.h> <BR>
<P>int main(void) <BR>{ <BR> void *buf; <BR> int
handle, bytes; <BR>
<P> buf = malloc(10); <BR>
<P>/* <BR> Looks for a file in the current directory named
TEST.$$$ and attempts <BR> to read 10 bytes from it. To
use this example you should create the <BR> file TEST.$$$
<BR>*/ <BR> if ((handle = <BR>
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
<BR> { <BR> printf("Error
Opening File\n"); <BR> exit(1);
<BR> } <BR>
<P> if ((bytes = read(handle, buf, 10)) == -1) {
<BR> printf("Read Failed.\n");
<BR> exit(1); <BR> }
<BR> else { <BR> printf("Read:
%d bytes read.\n", bytes); <BR> } <BR> return 0;
<BR>} <BR> <BR> <BR>
<P>函数名: realloc <BR>功 能: 重新分配主存 <BR>用 法: void *realloc(void
*ptr, unsigned newsize); <BR>程序例: <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -