程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C語言函數整理大全五(P-S)

C語言函數整理大全五(P-S)

編輯:關於C語言

函數名: 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved