当前位置:编程学习 > C/C++ >>

C语言函数整理大全五(P-S)

函数名: parsfnm
功 能: 分析文件名
用 法: char *parsfnm (char *cmdline, struct fcb *fcbptr, int option);
程序例:
#include
#include
#include
#include
int main(void)
{
char line[80];
struct fcb blk;
/* get file name */
printf("Enter drive and file name (no path - ie. a:file.dat) ");
gets(line);
/* put file name in fcb */
if (parsfnm(line, &blk, 1) == NULL)
printf("Error in parsfm call ");
else
printf("Drive #%d Name: s ", blk.fcb_drive, blk.fcb_name);
return 0;
}

函数名: peek
功 能: 检查存储单元
用 法: int peek(int segment, unsigned offset);
程序例:
#include
#include
#include
int main(void)
{
int value = 0;
printf("The current status of your keyboard is: ");
value = peek(0x0040, 0x0017);
if (value & 1)
printf("Right shift on ");
else
printf("Right shift off ");
if (value & 2)
printf("Left shift on ");
else
printf("Left shift off ");
if (value & 4)
printf("Control key on ");
else
printf("Control key off ");
if (value & 8)
printf("Alt key on ");
else
printf("Alt key off ");
if (value & 16)
printf("Scroll lock on ");
else
printf("Scroll lock off ");
if (value & 32)
printf("Num lock on ");
else
printf("Num lock off ");
if (value & 64)
printf("Caps lock on ");
else
printf("Caps lock off ");
return 0;
}

函数名: peekb
函数名: qsort
功 能: 使用快速排序例程进行排序
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
程序例:
#include
#include
#include
int sort_function( const void *a, const void *b);
char list[5][4] = { "cat", "car", "cab", "cap", "can" };

int main(void)
{
int x;
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x )
printf("%s ", list[x]);
return 0;
}
int sort_function( const void *a, const void *b)
{
return( strcmp(a,b) );
}

函数名: qsort
功 能: 使用快速排序例程进行排序
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
程序例:
#include
#include
#include
int sort_function( const void *a, const void *b);
char list[5][4] = { "cat", "car", "cab", "cap", "can" };

int main(void)
{
int x;
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x )
printf("%s ", list[x]);
return 0;
}
int sort_function( const void *a, const void *b)
{
return( strcmp(a,b) );
}

函数名: raise
功 能: 向正在执行的程序发送一个信号
用 法: int raise(int sig);
程序例:
#include
int main(void)
{
int a, b;
a = 10;
b = 0;
if (b == 0)
/* preempt divide by zero error */
raise(SIGFPE);
a = a / b;
return 0;
}

函数名: rand
函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s ", ptr);
return 0;
}

函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:
#include
#include
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf ", input, value);
return 0;
}

函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char input[16] = "abc,d";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%s ", p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%s ", p);
return 0;
}

函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例:
#include
#include
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ld ", string, lnumber);
return 0;
}

函数名: strupr
功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例:
#include
#include
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters */
ptr = strupr(string);
printf("%s ", ptr);
return 0;
}

函数名: swab
功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例:
#include
#include
#include
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %s ", target);
return 0;
}

函数名: system
功 能: 发出一个DOS命令
用 法: int system(char *command);
程序例:
#include
#include
int main(void)
{
printf("About to spawn command.com and run a DOS command ");
system("dir");
return 0;
}

功 能: 随机数发生器
用 法: void rand(void);
程序例:
#include
#include
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99 ");
for(i=0; i<10; i )
printf("%d ", rand() % 100);
return 0;
}

函数名: randbrd
功 能: 随机块读
用 法: int randbrd(struct fcb *fcbptr, int reccnt);
程序例:
#include
#include
#include
#include
int main(void)
{
char far *save_dta;
char line[80], buffer[256];
struct fcb blk;
int i, result;
/* get user input file name for dta */
printf("Enter drive and file name (no path - i.e. a:file.dat) ");
gets(line);
/* put file name in fcb */
if (!parsfnm(line, &blk, 1))
{
printf("Error in call to parsfnm ");
exit(1);
}
printf("Drive #%d File: %s ", blk.fcb_drive, blk.fcb_name);
/* open file with DOS FCB open file */
bdosptr(0x0F, &blk, 0);
/* save old dta, and set new one */
save_dta = getdta();
setdta(buffer);
/* set up info for the new dta */
blk.fcb_recsize = 128;
blk.fcb_random = 0L;
result = randbrd(&blk, 1);
/* check results from randbrd */
if (!result)
printf("Read OK ");
else
{
perror("Error during read");
exit(1);
}
/* read in data from the new dta */
printf("The first 128 characters are: ");
for (i=0; i<128; i )
putchar(buffer[i]);
/* restore previous dta */
setdta(save_dta);
return 0;
}

函数名: randbwr
功 能: 随机块写
用 法: int randbwr(struct fcp *fcbptr, int reccnt);
程序例:
#include
#include
#include
#include
int main(void)
{
char far *save_dta;
char line[80];
char buffer[256] = "RANDBWR test!";
struct fcb blk;
int result;
/* get new file name from user */
printf("Enter a file name to create (no path - ie. a:file.dat ");
gets(line);
/* parse the new file name to the dta */
parsfnm(line,&a
补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,