原文:C语言库函数大全及应用实例十四

                                      [编程资料]C语言库函数大全及应用实例十四

函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例:

#i nclude
#i nclude

int main(void)
{
char string[10] = "123456789";
char symbol = 'c';

printf("Before strset(): %s\n", string);
strset(string, symbol);
printf("After strset(): %s\n", string);
return 0;
}

函数名: strspn
功 能: 在串中查找指定字符集的子集的第一次出现
用 法: int strspn(char *str1, char *str2);
程序例:

#i nclude
#i nclude
#i nclude

int main(void)
{
char *string1 = "1234567890";
char *string2 = "123DC8";
int length;

length = strspn(string1, string2);
printf("Character where strings differ is at position %d\n", length);
return 0;
}

函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:

#i nclude
#i nclude

int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;

ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
}

函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:

#i nclude
#i nclude

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\n", input, value);
return 0;
}

函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:

#i nclude
#i nclude

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\n", 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\n", p);
return 0;
}

函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例:

#i nclude
#i nclude

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\n", string, lnumber);

return 0;
}

函数名: strupr
功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例:

#i nclude
#i nclude

int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;

/* converts string to upper case characters */
ptr = strupr(string);
printf("%s\n", ptr);
return 0;
}

函数名: swab
功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例:

#i nclude
#i nclude
#i nclude

char source[15] = "rFna koBlrna d";
char target[15];

int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %s\n", target);
return 0;
}

函数名: system
功 能: 发出一个DOS命令
用 法: int system(char *command);
程序例:

#i nclude
#i nclude

int main(void)
{
printf("About to spawn command.com and run a DOS command\n");
system("dir");
return 0;
}

函数名: tan
功 能: 正切函数
用 法: double tan(double x);
程序例:

#i nclude
#i nclude

int main(void)
{
double result, x;

x = 0.5;
result = tan(x);
printf("The tan of %lf is %lf\n", x, result);
return 0;
}

函数名: tanh
功 能: 双曲正切函数
用 法: double tanh(double x);
程序例:

#i nclude
#i nclude

int main(void)
{
double result, x;

x = 0.5;
result = tanh(x);
printf("The hyperbolic tangent of %lf is %lf\n", x, result);
return 0;
}

函数名: tell
功 能: 取文件指针的当前位置
用 法: long tell(int handle);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
int handle;
char msg[] = "Hello world";

if ((handle = open("TEST.$$$", O_CREAT | O_TEXT | O_APPEND)) == -1)
{
perror("Error:");
return 1;
}
write(handle, msg, strlen(msg));
printf("The file pointer is at byte %ld\n", tell(handle));
close(handle);
return 0;
}

函数名: textattr
功 能: 设置文本属性
用 法: void textattr(int attribute);
程序例:

#i nclude

int main(void)
{
int i;

clrscr();
for (i=0; i<9; i++)
{
textattr(i + ((i+1) << 4));
cprintf("This is a test\r\n");
}

return 0;
}

函数名: textbackground
功 能: 选择新的文本背景颜色
用 法: void textbackground(int color);
程序例:

#i nclude

int main(void)
{
int i, j;

clrscr();
for (i=0; i<9; i++)
{
for (j=0; j<80; j++)
cprintf("C");
cprintf("\r\n");
textcolor(i+1);
textbackground(i);
}

return 0;
}

函数名: textcolor
功 能: 在文本模式中选择新的字符颜色
用 法: void textcolor(int color);
程序例:
#i nclude

int main(void)
{
int i;

for (i=0; i<15; i++)
{
textcolor(i);
cprintf("Foreground Color\r\n");
}

return 0;
}

函数名: textheight
功 能: 返回以像素为单位的字符串高度
用 法: int far textheight(char far *textstring);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int y = 0;
int i;
char msg[80];

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

/* draw some text on the screen */
for (i=1; i<11; i++)
{
/* select the text style, direction, and size */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, i);

/* create a message string */
sprintf(msg, "Size: %d", i);

/* output the message */
outtextxy(1, y, msg);

/* advance to the next text line */
y += textheight(msg);
}

/* clean up */
getch();
closegraph();
return 0;
}

函数名: textmode
功 能: 将屏幕设置成文本模式
用 法: void textmode(int mode);
程序例:

#i nclude

int main(void)
{
textmode(BW40);
cprintf("ABC");
getch();

textmode(C40);
cprintf("ABC");
getch();

textmode(BW80);
cprintf("ABC");
getch();

textmode(C80);
cprintf("ABC");
getch();

textmode(MONO);
cprintf("ABC");
getch();

return 0;
}

函数名: textwidth
功 能: 返回以像素为单位的字符串宽度
用 法: int far textwidth(char far *textstring);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x = 0, y = 0;
int i;
char msg[80];

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

y = getmaxy() / 2;

settextjustify(LEFT_TEXT, CENTER_TEXT);
for (i=1; i<11; i++)
{
/* select the text style, direction, and size */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, i);

/* create a message string */
sprintf(msg, "Size: %d", i);

/* output the message */
outtextxy(x, y, msg);

/* advance to the end of the text */
x += textwidth(msg);
}

/* clean up */
getch();
closegraph();
return 0;
}

函数名: time
功 能: 取一天的时间
用 法: logn time(long *tloc);
程序例:

#i nclude
#i nclude
#i nclude

int main(void)
{
time_t t;

t = time(NULL);
printf("The number of seconds since January 1, 1970 is %ld",t);
return 0;
}

函数名: tmpfile
功 能: 以二进制方式打开暂存文件
用 法: FILE *tmpfile(void);
程序例:

#i nclude
#i nclude

int main(void)
{
FILE *tempfp;

tempfp = tmpfile();
if (tempfp)
printf("Temporary file created\n");
else
{
printf("Unable to create temporary file\n");
exit(1);
}

return 0;
}

函数名: tmpnam
功 能: 创建一个唯一的文件名
用 法: char *tmpnam(char *sptr);
程序例:

#i nclude

int main(void)
{
char name[13];

tmpnam(name);
printf("Temporary name: %s\n", name);
return 0;
}

函数名: tolower
功 能: 把字符转换成小写字母
用 法: int tolower(int c);
程序例:

#i nclude
#i nclude
#i nclude

int main(void)
{
int length, i;
char *string = "THIS IS A STRING";

length = strlen(string);
for (i=0; i
{
string[i] = tolower(string[i]);
}
printf("%s\n",string);

return 0;
}

函数名: toupper
功 能: 把字符转换成大写字母
用 法: int toupper(int c);
程序例:

#i nclude
#i nclude
#i nclude

int main(void)
{
int length, i;
char *string = "this is a string";

length = strlen(string);
for (i=0; i
{
string[i] = toupper(string[i]);
}

printf("%s\n",string);

return 0;
}

函数名: tzset
功 能: UNIX时间兼容函数
用 法: void tzset(void);
程序例:

#i nclude
#i nclude
#i nclude

int main(void)
{
time_t td;

putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time = %s\n", asctime(localtime(&td)));
return 0;
}

函数名: ultoa
功 能: 转换一个无符号长整型数为字符串
用 法: char *ultoa(unsigned long value, char *string, int radix);
程序例:

#i nclude
#i nclude

int main( void )
{
unsigned long lnumber = 3123456789L;
char string[25];

ultoa(lnumber,string,10);
printf("string = %s unsigned long = %lu\n",string,lnumber);

return 0;
}

函数名: ungetc
功 能: 把一个字符退回到输入流中
用 法: int ungetc(char c, FILE *stream);
程序例:

#i nclude
#i nclude

int main( void )
{
int i=0;
char ch;

puts("Input an integer followed by a char:");

/* read chars until non digit or EOF */
while((ch = getchar()) != EOF && isdigit(ch))
i = 10 * i + ch - 48; /* convert ASCII into int value */

/* if non digit char was read, push it back into input buffer */
if (ch != EOF)
ungetc(ch, stdin);

printf("i = %d, next char in buffer = %c\n", i, getchar());
return 0;
}

函数名: ungetch
功 能: 把一个字符退回到键盘缓冲区中
用 法: int ungetch(int c);
程序例:

#i nclude
#i nclude
#i nclude

int main( void )
{
int i=0;
char ch;

puts("Input an integer followed by a char:");

/* read chars until non digit or EOF */
while((ch = getche()) != EOF && isdigit(ch))
i = 10 * i + ch - 48; /* convert ASCII into int value */

/* if non digit char was read, push it back into input buffer */
if (ch != EOF)
ungetch(ch);

printf("\n\ni = %d, next char in buffer = %c\n", i, getch());
return 0;
}

函数名: unixtodos
功 能: 把日期和时间转换成DOS格式
用 法: void unixtodos(long utime, struct date *dateptr,
struct time *timeptr);
程序例:

#i nclude
#i nclude

char *month[] = {"---", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

#define SECONDS_PER_DAY 86400L /* the number of seconds in one day */

struct date dt;
struct time tm;

int main(void)
{
unsigned long val;

/* get today's date and time */
getdate(&dt);
gettime(&tm);
printf("today is %d %s %d\n", dt.da_day, month[dt.da_mon], dt.da_year);

/* convert date and time to unix format (number of seconds since Jan 1, 1970 */
val = dostounix(&dt, &tm);
/* subtract 42 days worth of seconds */
val -= (SECONDS_PER_DAY * 42);

/* convert back to dos time and date */
unixtodos(val, &dt, &tm);
printf("42 days ago it was %d %s %d\n",
dt.da_day, month[dt.da_mon], dt.da_year);
return 0;
}

函数名: unlink
功 能: 删掉一个文件
用 法: int unlink(char *filename);
程序例:

#i nclude
#i nclude

int main(void)
{
FILE *fp = fopen("junk.jnk","w");
int status;

fprintf(fp,"junk");

status = access("junk.jnk",0);
if (status == 0)
printf("File exists\n");
else
printf("File doesn't exist\n");

fclose(fp);
unlink("junk.jnk");
status = access("junk.jnk",0);
if (status == 0)
printf("File exists\n");
else
printf("File doesn't exist\n");

return 0;
}

函数名: unlock
功 能: 解除文件共享锁
用 法: int unlock(int handle, long offset, long length);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
int handle, status;
long length;

handle = sopen("c:\\autoexec.bat",O_RDONLY,SH_DENYNO,S_IREAD);

if (handle < 0)
{
printf("sopen failed\n");
exit(1);
}

length = filelength(handle);
status = lock(handle,0L,length/2);

if (status == 0)
printf("lock succeeded\n");
else
printf("lock failed\n");

status = unlock(handle,0L,length/2);

if (status == 0)
printf("unlock succeeded\n");
else
printf("unlock failed\n");

close(handle);
return 0;
}

函数名: vfprintf
功 能: 送格式化输出到一流中
用 法: int vfprintf(FILE *stream, char *format, va_list param);
程序例:

#i nclude
#i nclude
#i nclude

FILE *fp;

int vfpf(char *fmt, ...)
{
va_list argptr;
int cnt;

va_start(argptr, fmt);
cnt = vfprintf(fp, fmt, argptr);
va_end(argptr);

return(cnt);
}

int main(void)
{
int inumber = 30;
float fnumber = 90.0;
char string[4] = "abc";

fp = tmpfile();
if (fp == NULL)
{
perror("tmpfile() call");
exit(1);
}

vfpf("%d %f %s", inumber, fnumber, string);
rewind(fp);
fscanf(fp,"%d %f %s", &inumber, &fnumber, string);
printf("%d %f %s\n", inumber, fnumber, string);
fclose(fp);

return 0;
}

函数名: vfscanf
功 能: 从流中执行格式化输入
用 法: int vfscanf(FILE *stream, char *format, va_list param);
程序例:

#i nclude
#i nclude
#i nclude

FILE *fp;

int vfsf(char *fmt, ...)
{
va_list argptr;
int cnt;

va_start(argptr, fmt);
cnt = vfscanf(fp, fmt, argptr);
va_end(argptr);

return(cnt);
}

int main(void)
{
int inumber = 30;
float fnumber = 90.0;
char string[4] = "abc";

fp = tmpfile();
if (fp == NULL)
{
perror("tmpfile() call");
exit(1);
}
fprintf(fp,"%d %f %s\n",inumber,fnumber,string);
rewind(fp);

vfsf("%d %f %s",&inumber,&fnumber,string);
printf("%d %f %s\n",inumber,fnumber,string);
fclose(fp);

return 0;
}

函数名: vprintf
功 能: 送格式化输出到stdout中
用 法: int vprintf(char *format, va_list param);
程序例:

#i nclude
#i nclude

int vpf(char *fmt, ...)
{
va_list argptr;
int cnt;

va_start(argptr, format);
cnt = vprintf(fmt, argptr);
va_end(argptr);

return(cnt);
}

int main(void)
{
int inumber = 30;
float fnumber = 90.0;
char *string = "abc";

vpf("%d %f %s\n",inumber,fnumber,string);

return 0;
}

函数名: vscanf
功 能: 从stdin中执行格式化输入
用 法: int vscanf(char *format, va_list param);
程序例:

#i nclude
#i nclude
#i nclude

int vscnf(char *fmt, ...)
{
va_list argptr;
int cnt;

printf("Enter an integer, a float, and a string (e.g. i,f,s,)\n");
va_start(argptr, fmt);
cnt = vscanf(fmt, argptr);
va_end(argptr);

return(cnt);
}

int main(void)
{
int inumber;
float fnumber;
char string[80];

vscnf("%d, %f, %s", &inumber, &fnumber, string);
printf("%d %f %s\n", inumber, fnumber, string);

return 0;
}

函数名: vsprintf
功 能: 送格式化输出到串中
用 法: int vsprintf(char *string, char *format, va_list param);
程序例:

#i nclude
#i nclude
#i nclude

char buffer[80];

int vspf(char *fmt, ...)
{
va_list argptr;
int cnt;

va_start(argptr, fmt);
cnt = vsprintf(buffer, fmt, argptr);
va_end(argptr);

return(cnt);
}

int main(void)
{
int inumber = 30;
float fnumber = 90.0;
char string[4] = "abc";

vspf("%d %f %s", inumber, fnumber, string);
printf("%s\n", buffer);
return 0;
}

函数名: vsscanf
功 能: 从流中执行格式化输入
用 法: int vsscanf(char *s, char *format, va_list param);
程序例:

#i nclude
#i nclude
#i nclude

char buffer[80] = "30 90.0 abc";

int vssf(char *fmt, ...)
{
va_list argptr;
int cnt;

fflush(stdin);

va_start(argptr, fmt);
cnt = vsscanf(buffer, fmt, argptr);
va_end(argptr);

return(cnt);
}

int main(void)
{
int inumber;
float fnumber;
char string[80];

vssf("%d %f %s", &inumber, &fnumber, string);
printf("%d %f %s\n", inumber, fnumber, string);
return 0;
}

函数名: wherex
功 能: 返回窗口内水平光标位置
用 法: int wherex(void);
程序例:

#i nclude

int main(void)
{
clrscr();
gotoxy(10,10);
cprintf("Current location is X: %d Y: %d\r\n", wherex(), wherey());
getch();

return 0;
}

函数名: wherey
功 能: 返回窗口内垂直光标位置
用 法: int wherey(void);
程序例:

#i nclude

int main(void)
{
clrscr();
gotoxy(10,10);
cprintf("Current location is X: %d Y: %d\r\n", wherex(), wherey());
getch();

return 0;
}

函数名: window
功 能: 定义活动文本模式窗口
用 法: void window(int left, int top, int right, int bottom);
程序例:

C语言库函数大全及应用实例十四的更多相关文章

  1. C语言库函数大全及应用实例十二

    原文:C语言库函数大全及应用实例十二                                          [编程资料]C语言库函数大全及应用实例十二 函数名: setrgbpalette ...

  2. C语言库函数大全及应用实例十

    原文:C语言库函数大全及应用实例十                                             [编程资料]C语言库函数大全及应用实例十 函数名: qsort 功 能: 使 ...

  3. C语言库函数大全及应用实例十三

    原文:C语言库函数大全及应用实例十三                                          [编程资料]C语言库函数大全及应用实例十三 函数名: stat 功 能: 读取打 ...

  4. C语言库函数大全及应用实例十一

    原文:C语言库函数大全及应用实例十一                                         [编程资料]C语言库函数大全及应用实例十一 函数名: setbkcolor 功 能 ...

  5. C语言库函数大全及应用实例九

    原文:C语言库函数大全及应用实例九                                                [编程资料]C语言库函数大全及应用实例九 函数名: mktemp 功 ...

  6. C语言库函数大全及应用实例八

    原文:C语言库函数大全及应用实例八                                            [编程资料]C语言库函数大全及应用实例八 函数名: kbhit 功 能: 检查 ...

  7. C语言库函数大全及应用实例七

    原文:C语言库函数大全及应用实例七 [编程资料]C语言库函数大全及应用实例七 函数名: getw 功 能: 从流中取一整数 用 法: int getw(FILE *strem); 程序例: #i nc ...

  8. C语言库函数大全及应用实例五

    原文:C语言库函数大全及应用实例五                                                 [编程资料]C语言库函数大全及应用实例五 函数名: getcurdi ...

  9. C语言库函数大全及应用实例六

    原文:C语言库函数大全及应用实例六                                              [编程资料]C语言库函数大全及应用实例六 函数名: getlinesett ...

随机推荐

  1. 第十九章——使用资源调控器管理资源(2)——使用T-SQL配置资源调控器

    原文:第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 前言: 在前一章已经演示了如何使用SSMS来配置资源调控器.但是作为DBA,总有需要写脚本的时候,因为它可以重用及扩展. ...

  2. HSV 量化

    function L=hsvquan(hsv) %对HSV量化,该3维特征矢量: h=hsv(:,:,1); s=hsv(:,:,2); v=hsv(:,:,3); % 假设对HSV 空间进行适当的量 ...

  3. JAVA 统计字符串中中文,英文,数字,空格的个数

    面试题:输入一行字符,分别统计出其中英文字母.中文字符.空格.数字和其它字符的个数 可以根据各种字符在Unicode字符编码表中的区间来进行判断,如数字为'0'~'9'之间,英文字母为'a'~'z'或 ...

  4. HDU 5095 Linearization of the kernel functions in SVM(模拟)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5095 Problem Description SVM(Support Vector Machine) ...

  5. SharePoint 2010 Form Authentication (SQL) based on existing database

    SharePoint 2010 表单认证,基于现有数据库的用户信息表 本文主要描写叙述本人配置过程中涉及到的步骤,仅作为參考,不要仅限于此步骤. 另外本文通俗易懂,适合大众口味儿. I. 开启并配置基 ...

  6. Design Pattern Command 命令设计模式

    这种设计模式是使用不同类的包裹不同的命令,达到什么样的命令执行什么操作. 有可能进一步利用map您最喜欢的对接命令字. 正在运行的类实际上已经包含了操作的所有需求,例如: class SuperMak ...

  7. 移动端 延迟加载echo.js的使用

    浏览器支持ie8+   <img src="img/blank.gif" alt="" data-echo="img/album-1.jpg&q ...

  8. WebService之CXF注解报错(一)

    WebService之CXF注解 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] ...

  9. Web 服务器 (IIS) 角色

    原文:Web 服务器 (IIS) 角色 1. 对于默认安装,请在命令行提示符下键入以下命令,然后按 Enter: start /w pkgmgr /iu:IIS-WebServerRole;WAS-W ...

  10. opengl 扳回一球

    本文介绍了使用两种方法opengl画一个球体,一个是一个球形点位置计算,然后绘制出,还有一个glut套件自带功能. 一.直接绘制法 直接贴代码,解释都写在凝视里了.绘制时先移动好坐标系.然后调用这方法 ...