📄 fr.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<meta name="Author" content="wdg">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>网上学堂 --> C语言编程宝典之一 -->函数名: r</title>
</head>
<body>
<div align="center"><center>
<table border="1" cellpadding="4" width="640"
bordercolordark="#FFFFFF" bordercolorlight="#FFFFFF">
<tr>
<td bgcolor="#FFE6B0" bordercolor="#8080FF" class="p9"><font
color="#BB0000">导航条:--></font> <a
href="../../index.html">网上学堂</a> --> <a
href="../tcindex.htm"><font face="宋体">C</font>语言编程宝典之一</a>
-->函数名: r</td>
</tr>
<tr>
<td bordercolor="#8080FF" class="p9"><p>函数名: raise <br>
功 能: 向正在执行的程序发送一个信号
<br>
用 法: int raise(int sig); <br>
程序例: </p>
<p>#include <signal.h> </p>
<p>int main(void) <br>
{ <br>
int a, b; </p>
<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>
</p>
<p>函数名: rand <br>
功 能: 随机数发生器 <br>
用 法: void rand(void); <br>
程序例: </p>
<p>#include <stdlib.h> <br>
#include <stdio.h> </p>
<p>int main(void) <br>
{ <br>
int i; </p>
<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>
</p>
<p>函数名: randbrd <br>
功 能: 随机块读 <br>
用 法: int randbrd(struct fcb *fcbptr, int
reccnt); <br>
程序例: </p>
<p>#include <process.h> <br>
#include <string.h> <br>
#include <stdio.h> <br>
#include <dos.h> </p>
<p>int main(void) <br>
{ <br>
char far *save_dta; <br>
char line[80], buffer[256]; <br>
struct fcb blk; <br>
int i, result; </p>
<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); </p>
<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); </p>
<p> /* open file with DOS FCB open file */ <br>
bdosptr(0x0F, &blk, 0); </p>
<p> /* save old dta, and set new one */ <br>
save_dta = getdta(); <br>
setdta(buffer); </p>
<p> /* set up info for the new dta */ <br>
blk.fcb_recsize = 128; <br>
blk.fcb_random = 0L; <br>
result = randbrd(&blk, 1); </p>
<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>
} </p>
<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]); </p>
<p> /* restore previous dta */ <br>
setdta(save_dta); </p>
<p> return 0; <br>
} <br>
</p>
<p>函数名: randbwr <br>
功 能: 随机块写 <br>
用 法: int randbwr(struct fcp *fcbptr, int
reccnt); <br>
程序例: </p>
<p>#include <process.h> <br>
#include <string.h> <br>
#include <stdio.h> <br>
#include <dos.h> </p>
<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; </p>
<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); </p>
<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); </p>
<p> /* request DOS services to create file */
<br>
if (bdosptr(0x16, &blk, 0) == -1) <br>
{ <br>
perror("Error
creating file"); <br>
exit(1); <br>
} </p>
<p> /* save old dta and set new dta */ <br>
save_dta = getdta(); <br>
setdta(buffer); </p>
<p> /* write new records */ <br>
blk.fcb_recsize = 256; <br>
blk.fcb_random = 0L; <br>
result = randbwr(&blk, 1); </p>
<p> if (!result) <br>
printf("Write
OK\n"); <br>
else <br>
{ <br>
perror("Disk
error"); <br>
exit(1); <br>
} </p>
<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>
} </p>
<p> /* reset the old dta */ <br>
setdta(save_dta); </p>
<p> return 0; <br>
} <br>
<br>
</p>
<p>函数名: random <br>
功 能: 随机数发生器 <br>
用 法: int random(int num); <br>
程序例: </p>
<p>#include <stdlib.h> <br>
#include <stdio.h> <br>
#include <time.h> </p>
<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>
</p>
<p>函数名: randomize <br>
功 能: 初始化随机数发生器 <br>
用 法: void randomize(void); <br>
程序例: </p>
<p>#include <stdlib.h> <br>
#include <stdio.h> <br>
#include <time.h> </p>
<p>int main(void) <br>
{ <br>
int i; </p>
<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>
</p>
<p>函数名: read <br>
功 能: 从文件中读 <br>
用 法: int read(int handle, void *buf, int
nbyte); <br>
程序例: </p>
<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> </p>
<p>int main(void) <br>
{ <br>
void *buf; <br>
int handle, bytes; </p>
<p> buf = malloc(10); </p>
<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>
} </p>
<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>
</p>
<p>函数名: realloc <br>
功 能: 重新分配主存 <br>
用 法: void *realloc(void *ptr, unsigned
newsize); <br>
程序例: </p>
<p>#include <stdio.h> <br>
#include <alloc.h> <br>
#include <string.h> </p>
<p>int main(void) <br>
{ <br>
char *str; </p>
<p> /* allocate memory for string */ <br>
str = malloc(10); </p>
<p> /* copy "Hello" into string */ <br>
strcpy(str, "Hello"); </p>
<p> printf("String is %s\n Address
is %p\n", str, str); <br>
str = realloc(str, 20); <br>
printf("String is %s\n New
address is %p\n", str, str); </p>
<p> /* free memory */ <br>
free(str); </p>
<p> return 0; <br>
} <br>
<br>
</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -