免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9983 | 回复: 0
打印 上一主题 下一主题

UNIX环境C函数大全 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-13 15:10 |只看该作者 |倒序浏览
UNIX环境C函数大全
函数名: abort
功 能: 异常终止一个进程
用 法: void abort(void);
程序例:
#include  
#include  
int main(void)
{
printf("Calling abort()n");
abort();
return 0; /* This is never reached */
}
函数名: abs
功 能: 求整数的绝对值
用 法: int abs(int i);
程序例:
#include  
#include  
int main(void)
{
int number = -1234;
printf("number: %d absolute value: %dn", number, abs(number));
return 0;
}
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
程序例:
#include  
#include  
int file_exists(char *filename);
int main(void)
{
printf("Does NOTEXIST.FIL exist: %sn",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
函数名: acos
功 能: 反余弦函数
用 法: double acos(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 0.5;
result = acos(x);
printf("The arc cosine of %lf is %lfn", x, result);
return 0;
}
函数名: asctime
功 能: 转换日期和时间为ASCII码
用 法: char *asctime(const struct tm *tblock);
程序例:
#include  
#include  
#include  
int main(void)
{
struct tm t;
char str[80];
/* sample loading of tm structure */
t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated
string */
strcpy(str, asctime(&t));
printf("%sn", str);
return 0;
}

函数名: asin
功 能: 反正弦函数
用 法: double asin(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 0.5;
result = asin(x);
printf("The arc sin of %lf is %lfn", x, result);
return(0);
}

函数名: assert
功 能: 测试一个条件并可能使程序终止
用 法: void assert(int test);
程序例:
#include  
#include  
#include  
struct ITEM {
int key;
int value;
};
/* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
}
int main(void)
{
additem(NULL);
return 0;
}

函数名: atan
功 能: 反正切函数
用 法: double atan(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 0.5;
result = atan(x);
printf("The arc tangent of %lf is %lfn", x, result);
return(0);
}
函数名: atan2
功 能: 计算Y/X的反正切值
用 法: double atan2(double y, double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 90.0, y = 45.0;
result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lfn", (y / x), result);
return 0;
}
函数名: atexit
功 能: 注册终止函数
用 法: int atexit(atexit_t func);
程序例:
#include  
#include  
void exit_fn1(void)
{
printf("Exit function #1 calledn");
}
void exit_fn2(void)
{
printf("Exit function #2 calledn");
}
int main(void)
{
/* post exit function #1 */
atexit(exit_fn1);
/* post exit function #2 */
atexit(exit_fn2);
return 0;
}

函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
程序例:
#include  
#include  
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %fn", str, f);
return 0;
}
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
#include  
#include  
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %dn", str, n);
return 0;
}
函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);
程序例:
#include  
#include  
int main(void)
{
long l;
char *str = "98765432";
l = atol(lstr);
printf("string = %s integer = %ldn", str, l);
return(0);
}
函数名: bioscom
功 能: 串行I/O通信
用 法: int bioscom(int cmd, char abyte, int port);
程序例:
#include  
#include  
#define COM1 0
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0
#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
int main(void)
{
int in, out, status, DONE = FALSE;
bioscom(0, SETTINGS, COM1);
cprintf("... BIOSCOM [ESC] to exit ...n");
while (!DONE)
{
status = bioscom(3, 0, COM1);
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
putch(out);
if (kbhit())
{
if ((in = getch()) == 'x1B')
DONE = TRUE;
bioscom(1, in, COM1);
}
}
return 0;
}

函数名: brk
功 能: 改变数据段空间分配
用 法: int brk(void *endds);
程序例:
#include  
#include  
int main(void)
{
char *ptr;
printf("Changing allocation with brk()n");
ptr = malloc(1);
printf("Before brk() call: %lu bytes freen", coreleft());
brk(ptr+1000);
printf(" After brk() call: %lu bytes freen", coreleft());
return 0;
}

函数名: bsearch
功 能: 二分法搜索
用 法: void *bsearch(const void *key, const void *base, size_t *nelem,
size_t width, int(*fcmp)(const void *, const *));
程序例:
#include  
#include  
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
int numarray[] = {123, 145, 512, 627, 800, 933};
int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
}
int lookup(int key)
{
int *itemptr;
/* The cast of (int(*)(const void *,const void*))
is needed to avoid a type mismatch error at
compile time */
itemptr = bsearch (&key, numarray, NELEMS(numarray),
sizeof(int), (int(*)(const void *,const void *))numeric);
return (itemptr != NULL);
}
int main(void)
{
if (lookup(512))
printf("512 is in the table.n");
else
printf("512 isn't in the table.n");
return 0;
}
函数名: cabs
功 能: 计算复数的绝对值
用 法: double cabs(struct complex z);
程序例:
#include  
#include  
int main(void)
{
struct complex z;
double val;
z.x = 2.0;
z.y = 1.0;
val = cabs(z);
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
return 0;
}

函数名: calloc
功 能: 分配主存储器
用 法: void *calloc(size_t nelem, size_t elsize);
程序例:
#include  
#include  
int main(void)
{
char *str = NULL;
/* allocate memory for string */
str = calloc(10, sizeof(char));
/* copy "Hello" into string */
strcpy(str, "Hello");
/* display string */
printf("String is %sn", str);
/* free memory */
free(str);
return 0;
}

函数名: ceil
功 能: 向上舍入
用 法: double ceil(double x);
程序例:
#include  
#include  
int main(void)
{
double number = 123.54;
double down, up;
down = floor(number);
up = ceil(number);
printf("original number %5.2lfn", number);
printf("number rounded down %5.2lfn", down);
printf("number rounded up %5.2lfn", up);
return 0;
}

函数名: chdir
功 能: 改变工作目录
用 法: int chdir(const char *path);
程序例:
#include  
#include  
#include  
char old_dir[MAXDIR];
char new_dir[MAXDIR];
int main(void)
{
if (getcurdir(0, old_dir))
{
perror("getcurdir()");
exit(1);
}
printf("Current directory is: \%sn", old_dir);
if (chdir("\"))
{
perror("chdir()");
exit(1);
}
if (getcurdir(0, new_dir))
{
perror("getcurdir()");
exit(1);
}
printf("Current directory is now: \%sn", new_dir);
printf("nChanging back to orignal directory: \%sn", old_dir);
if (chdir(old_dir))
{
perror("chdir()");
exit(1);
}
return 0;
}
函数名: _chmod, chmod
功 能: 改变文件的访问方式
用 法: int chmod(const char *filename, int permiss);
程序例:
#include  
#include  
#include  
void make_read_only(char *filename);
int main(void)
{
make_read_only("NOTEXIST.FIL");
make_read_only("MYFILE.FIL");
return 0;
}
void make_read_only(char *filename)
{
int stat;
stat = chmod(filename, S_IREAD);
if (stat)
printf("Couldn't make %s read-onlyn", filename);
else
printf("Made %s read-onlyn", filename);
}

函数名: clearerr
功 能: 复位错误标志
用 法:void clearerr(FILE *stream);
程序例:
#include  
int main(void)
{
FILE *fp;
char ch;
/* open a file for writing */
fp = fopen("DUMMY.FIL", "w");
/* force an error condition by attempting to read */
ch = fgetc(fp);
printf("%cn",ch);
if (ferror(fp))
{
/* display an error message */
printf("Error reading from DUMMY.FILn");
/* reset the error and EOF indicators */
clearerr(fp);
}
fclose(fp);
return 0;
}

函数名: _close, close
功 能: 关闭文件句柄
用 法: int close(int handle);
程序例:
#include  
#include  
#include  
#include  
main()
{
int handle;
char buf[11] = "0123456789";
/* create a file containing 10 bytes */
handle = open("NEW.FIL", O_CREAT);
if (handle > -1)
{
write(handle, buf, strlen(buf));
/* close the file */
close(handle);
}
else
{
printf("Error opening filen");
}
return 0;
}

函数名: clock
功 能: 确定处理器时间
用 法: clock_t clock(void);
程序例:
#include  
#include  
#include  
int main(void)
{
clock_t start, end;
start = clock();
delay(2000);
end = clock();
printf("The time was: %fn", (end - start) / CLK_TCK);
return 0;
}

函数名: cos
功 能: 余弦函数
用 法: double cos(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 0.5;
result = cos(x);
printf("The cosine of %lf is %lfn", x, result);
return 0;
}

函数名: cosh
功 能: 双曲余弦函数
用 法: dluble cosh(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 0.5;
result = cosh(x);
printf("The hyperboic cosine of %lf is %lfn", x, result);
return 0;
}

函数名: _creat creat
功 能: 创建一个新文件或重写一个已存在的文件
用 法: int creat (const char *filename, int permiss);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
int handle;
char buf[11] = "0123456789";
/* change the default file mode from text to binary */
_fmode = O_BINARY;
/* create a binary file for reading and writing */
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
/* write 10 bytes to the file */
write(handle, buf, strlen(buf));
/* close the file */
close(handle);
return 0;
}
函数名: ctime
功 能: 把日期和时间转换为字符串
用 法: char *ctime(const time_t *time);
程序例:
#include  
#include  
int main(void)
{
time_t t;
time(&t);
printf("Today's date and time: %sn", ctime(&t));
return 0;
}
函数名: difftime
功 能: 计算两个时刻之间的时间差
用 法: double difftime(time_t time2, time_t time1);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
time_t first, second;
clrscr();
first = time(NULL); /* Gets system
time */
delay(2000); /* Waits 2 secs */
second = time(NULL); /* Gets system time
again */
printf("The difference is: %f
secondsn",difftime(second,first));
getch();
return 0;
}
函数名: div
功 能: 将两个整数相除, 返回商和余数
用 法: div_t (int number, int denom);
程序例:
#include  
#include  
div_t x;
int main(void)
{
x = div(10,3);
printf("10 div 3 = %d remainder %dn", x.quot, x.rem);
return 0;
}
函数名: dup
功 能: 复制一个文件句柄
用 法: int dup(int handle);
程序例:
#include  
#include  
#include  
#include  
void flush(FILE *stream);
int main(void)
{
FILE *fp;
char msg[] = "This is a test";
/* create a file */
fp = fopen("DUMMY.FIL", "w");
/* write some data to the file */
fwrite(msg, strlen(msg), 1, fp);
clrscr();
printf("Press any key to flush
DUMMY.FIL:");
getch();
/* flush the data to DUMMY.FIL without
closing it */
flush(fp);
printf("nFile was flushed, Press any
key to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* flush TC's internal buffer */
fflush(stream);
/* make a duplicate file handle */
duphandle = dup(fileno(stream));
/* close the duplicate handle to flush the
DOS buffer */
close(duphandle);
}
函数名: dup2
功 能: 复制文件句柄
用 法: int dup2(int oldhandle, int newhandle);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
#define STDOUT 1
int nul, oldstdout;
char msg[] = "This is a test";
/* create a file */
nul = open("DUMMY.FIL", O_CREAT | O_RDWR,
S_IREAD | S_IWRITE);
/* create a duplicate handle for standard
output */
oldstdout = dup(STDOUT);
/*
redirect standard output to DUMMY.FIL
by duplicating the file handle onto the
file handle for standard output.
*/
dup2(nul, STDOUT);
/* close the handle for DUMMY.FIL */
close(nul);
/* will be redirected into DUMMY.FIL */
write(STDOUT, msg, strlen(msg));
/* restore original standard output
handle */
dup2(oldstdout, STDOUT);
/* close duplicate handle for STDOUT */
close(oldstdout);
return 0;
}
函数名: eof
功 能: 检测文件结束
用 法: int eof(int *handle);
程序例:
#include  
#include  
#include  
#include  
#include  
int main(void)
{
int handle;
char msg[] = "This is a test";
char ch;
/* create a file */
handle = open("DUMMY.FIL",
O_CREAT | O_RDWR,
S_IREAD | S_IWRITE);
/* write some data to the file */
write(handle, msg, strlen(msg));
/* seek to the beginning of the file */
lseek(handle, 0L, SEEK_SET);
/*
reads chars from the file until hit EOF
*/
do
{
read(handle, &ch, 1);
printf("%c", ch);
} while (!eof(handle));
close(handle);
return 0;
}
函数名: exec...
功 能: 装入并运行其它程序的函数
用 法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL);
int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,
char *envp[]);
int execlp(char *pathname, char *arg0, arg1, .., NULL);
int execple(char *pathname, char *arg0, arg1, ..., NULL,
char *envp[]);
int execv(char *pathname, char *argv[]);
int execve(char *pathname, char *argv[], char *envp[]);
int execvp(char *pathname, char *argv[]);
int execvpe(char *pathname, char *argv[], char *envp[]);
程序例:
/* execv example */
#include  
#include  
#include  
void main(int argc, char *argv[])
{
int i;
printf("Command line arguments:n");
for (i=0; i
printf("About to exec child with arg1 arg2 ...n");
execv("CHILD.EXE", argv);
perror("exec error");
exit(1);
}
函数名: exit
功 能: 终止程序
用 法: void exit(int status);
程序例:
#include  
#include  
#include  
int main(void)
{
int status;
printf("Enter either 1 or 2n");
status = getch();
/* Sets DOS errorlevel */
exit(status - '0');
/* Note: this line is never reached */
return 0;
}
函数名: exp
功 能: 指数函数
用 法: double exp(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 4.0;
result = exp(x);
printf("'e' raised to the power
of %lf (e ^ %lf) = %lfn",
x, x, result);
return 0;
}
函数名: fabs
功 能: 返回浮点数的绝对值
用 法: double fabs(double x);
程序例:
#include  
#include  
int main(void)
{
float number = -1234.0;
printf("number: %f absolute value: %fn",
number, fabs(number));
return 0;
}

函数名: fclose
功 能: 关闭一个流
用 法: int fclose(FILE *stream);
程序例:
#include  
#include  
int main(void)
{
FILE *fp;
char buf[11] = "0123456789";
/* create a file containing 10 bytes */
fp = fopen("DUMMY.FIL", "w");
fwrite(&buf, strlen(buf), 1, fp);
/* close the file */
fclose(fp);
return 0;
}

函数名: fdopen
功 能: 把流与一个文件句柄相接
用 法: FILE *fdopen(int handle, char *type);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
int handle;
FILE *stream;
/* open a file */
handle = open("DUMMY.FIL", O_CREAT,
S_IREAD | S_IWRITE);
/* now turn the handle into a stream */
stream = fdopen(handle, "w");
if (stream == NULL)
printf("fdopen failedn");
else
{
fprintf(stream, "Hello worldn");
fclose(stream);
}
return 0;
}
函数名: feof
功 能: 检测流上的文件结束符
用 法: int feof(FILE *stream);
程序例:
#include  
int main(void)
{
FILE *stream;
/* open a file for reading */
stream = fopen("DUMMY.FIL", "r");
/* read a character from the file */
fgetc(stream);
/* check for EOF */
if (feof(stream))
printf("We have reached end-of-filen");
/* close the file */
fclose(stream);
return 0;
}
函数名: ferror
功 能: 检测流上的错误
用 法: int ferror(FILE *stream);
程序例:
#include  
int main(void)
{
FILE *stream;
/* open a file for writing */
stream = fopen("DUMMY.FIL", "w");
/* force an error condition by attempting to read */
(void) getc(stream);
if (ferror(stream)) /* test for an error on the stream */
{
/* display an error message */
printf("Error reading from DUMMY.FILn");
/* reset the error and EOF indicators */
clearerr(stream);
}
fclose(stream);
return 0;
}

函数名: fflush
功 能: 清除一个流
用 法: int fflush(FILE *stream);
程序例:
#include  
#include  
#include  
#include  
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
/* create a file */
stream = fopen("DUMMY.FIL", "w");
/* write some data to the file */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush
DUMMY.FIL:");
getch();
/* flush the data to DUMMY.FIL without
closing it */
flush(stream);
printf("nFile was flushed, Press any key
to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* flush the stream's internal buffer */
fflush(stream);
/* make a duplicate file handle */
duphandle = dup(fileno(stream));
/* close the duplicate handle to flush
the DOS buffer */
close(duphandle);
}

函数名: fgetc
功 能: 从流中读取字符
用 法: int fgetc(FILE *stream);
程序例:
#include  
#include  
#include  
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char ch;
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* write a string into the file */
fwrite(string, strlen(string), 1, stream);
/* seek to the beginning of the file */
fseek(stream, 0, SEEK_SET);
do
{
/* read a char from the file */
ch = fgetc(stream);
/* display the character */
putch(ch);
} while (ch != EOF);
fclose(stream);
return 0;
}

函数名: fgetpos
功 能: 取得当前文件的句柄
用 法: int fgetpos(FILE *stream);
程序例:
#include  
#include  
int main(void)
{
FILE *stream;
char string[] = "This is a test";
fpos_t filepos;
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* write a string into the file */
fwrite(string, strlen(string), 1, stream);
/* report the file pointer position */
fgetpos(stream, &filepos);
printf("The file pointer is at byte
%ldn", filepos);
fclose(stream);
return 0;
}

函数名: fgets
功 能: 从流中读取一字符串
用 法: char *fgets(char *string, int n, FILE *stream);
程序例:
#include  
#include  
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char msg[20];
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* write a string into the file */
fwrite(string, strlen(string), 1, stream);
/* seek to the start of the file */
fseek(stream, 0, SEEK_SET);
/* read a string from the file */
fgets(msg, strlen(string)+1, stream);
/* display the string */
printf("%s", msg);
fclose(stream);
return 0;
}

函数名: floor
功 能: 向下舍入
用 法: double floor(double x);
程序例:
#include  
#include  
int main(void)
{
double number = 123.54;
double down, up;
down = floor(number);
up = ceil(number);
printf("original number %10.2lfn",
number);
printf("number rounded down %10.2lfn",
down);
printf("number rounded up %10.2lfn",
up);
return 0;
}

函数名: fmod
功 能: 计算x对y的模, 即x/y的余数
用 法: double fmod(double x, double y);
程序例:
#include  
#include  
int main(void)
{
double x = 5.0, y = 2.0;
double result;
result = fmod(x,y);
printf("The remainder of (%lf / %lf) is
%lfn", x, y, result);
return 0;
}

函数名: fopen
功 能: 打开一个流
用 法: FILE *fopen(char *filename, char *type);
程序例:
#include  
#include  
#include  
int main(void)
{
char *s;
char drive[MAXDRIVE];
char dir[MAXDIR];
char file[MAXFILE];
char ext[MAXEXT];
int flags;
s=getenv("COMSPEC"); /* get the comspec environment parameter */
flags=fnsplit(s,drive,dir,file,ext);
printf("Command processor info:n");
if(flags & DRIVE)
printf("tdrive: %sn",drive);
if(flags & DIRECTORY)
printf("tdirectory: %sn",dir);
if(flags & FILENAME)
printf("tfile: %sn",file);
if(flags & EXTENSION)
printf("textension: %sn",ext);
return 0;
}
函数名: fprintf
功 能: 传送格式化输出到一个流中
用 法: int fprintf(FILE *stream, char *format[, argument,...]);
程序例:
/* Program to create backup of the
AUTOEXEC.BAT file */
#include  
int main(void)
{
FILE *in, *out;
if ((in = fopen("\AUTOEXEC.BAT", "rt"))
== NULL)
{
fprintf(stderr, "Cannot open input
file.n");
return 1;
}
if ((out = fopen("\AUTOEXEC.BAK", "wt"))
== NULL)
{
fprintf(stderr, "Cannot open output
file.n");
return 1;
}
while (!feof(in))
fputc(fgetc(in), out);
fclose(in);
fclose(out);
return 0;
}

函数名: fputc
功 能: 送一个字符到一个流中
用 法: int fputc(int ch, FILE *stream);
程序例:
#include  
int main(void)
{
char msg[] = "Hello world";
int i = 0;
while (msg)
{
fputc(msg, stdout);
i++;
}
return 0;
}

函数名: fputchar
功 能: 送一个字符到标准输出流(stdout)中
用 法: int fputchar(char ch);
程序例:
#include  
int main(void)
{
char msg[] = "This is a test";
int i = 0;
while (msg)
{
fputchar(msg);
i++;
}
return 0;
}

函数名: fputs
功 能: 送一个字符到一个流中
用 法: int fputs(char *string, FILE *stream);
程序例:
#include  
int main(void)
{
/* write a string to standard output */
fputs("Hello worldn", stdout);
return 0;
}

函数名: fread
功 能: 从一个流中读数据
用 法: int fread(void *ptr, int size, int nitems, FILE *stream);
程序例:
#include  
#include  
int main(void)
{
FILE *stream;
char msg[] = "this is a test";
char buf[20];
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr,
"Cannot open output file.n");
return 1;
}
/* write some data to the file */
fwrite(msg, strlen(msg)+1, 1, stream);
/* seek to the beginning of the file */
fseek(stream, SEEK_SET, 0);
/* read the data and display it */
fread(buf, strlen(msg)+1, 1, stream);
printf("%sn", buf);
fclose(stream);
return 0;
}
函数名: free
功 能: 释放已分配的块
用 法: void free(void *ptr);
程序例:
#include  
#include  
#include  
int main(void)
{
char *str;
/* allocate memory for string */
str = malloc(10);
/* copy "Hello" to string */
strcpy(str, "Hello");
/* display string */
printf("String is %sn", str);
/* free memory */
free(str);
return 0;
}
函数名: freopen
功 能: 替换一个流
用 法: FILE *freopen(char *filename, char *type, FILE *stream);
程序例:
#include  
int main(void)
{
/* redirect standard output to a file */
if (freopen("OUTPUT.FIL", "w", stdout)
== NULL)
fprintf(stderr, "error redirecting
stdoutn");
/* this output will go to a file */
printf("This will go into a file.");
/* close the standard output stream */
fclose(stdout);
return 0;
}

函数名: frexp
功 能: 把一个双精度数分解为尾数的指数
用 法: double frexp(double value, int *eptr);
程序例:
#include  
#include  
int main(void)
{
double mantissa, number;
int exponent;
number = 8.0;
mantissa = frexp(number, &exponent);
printf("The number %lf is ", number);
printf("%lf times two to the ", mantissa);
printf("power of %dn", exponent);
return 0;
}
函数名: fscanf
功 能: 从一个流中执行格式化输入
用 法: int fscanf(FILE *stream, char *format[,argument...]);
程序例:
#include  
#include  
int main(void)
{
int i;
printf("Input an integer: ");
/* read an integer from the
standard input stream */
if (fscanf(stdin, "%d", &i))
printf("The integer read was: %in",
i);
else
{
fprintf(stderr, "Error reading an
integer from stdin.n");
exit(1);
}
return 0;
}

函数名: fseek
功 能: 重定位流上的文件指针
用 法: int fseek(FILE *stream, long offset, int fromwhere);
程序例:
#include  
long filesize(FILE *stream);
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytesn", filesize(stream));
fclose(stream);
return 0;
}
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}

函数名: fsetpos
功 能: 定位流上的文件指针
用 法: int fsetpos(FILE *stream, const fpos_t *pos);
程序例:
#include  
#include  
void showpos(FILE *stream);
int main(void)
{
FILE *stream;
fpos_t filepos;
/* open a file for update */
stream = fopen("DUMMY.FIL", "w+");
/* save the file pointer position */
fgetpos(stream, &filepos);
/* write some data to the file */
fprintf(stream, "This is a test");
/* show the current file position */
showpos(stream);
/* set a new file position, display it */
if (fsetpos(stream, &filepos) == 0)
showpos(stream);
else
{
fprintf(stderr, "Error setting file
pointer.n");
exit(1);
}
/* close the file */
fclose(stream);
return 0;
}
void showpos(FILE *stream)
{
fpos_t pos;
/* display the current file pointer
position of a stream */
fgetpos(stream, &pos);
printf("File position: %ldn", pos);
}
函数名: fstat
功 能: 获取打开文件信息
用 法: int fstat(char *handle, struct stat *buff);
程序例:
#include  
#include  
#include  
int main(void)
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr, "Cannot open output
file.n");
return(1);
}
fprintf(stream, "This is a test");
fflush(stream);
/* get information about the file */
fstat(fileno(stream), &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary
file.n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on
file.n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on
file.n");
printf("Drive letter of file: %cn",
'A'+statbuf.st_dev);
printf("Size of file in bytes: %ldn",
statbuf.st_size);
printf("Time file last opened: %sn",
ctime(&statbuf.st_ctime));
return 0;
}

函数名: ftell
功 能: 返回当前文件指针
用 法: long ftell(FILE *stream);
程序例:
#include  
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("The file pointer is at byte
%ldn", ftell(stream));
fclose(stream);
return 0;
}

函数名: fwrite
功 能: 写内容到流中
用 法: int fwrite(void *ptr, int size, int nitems, FILE *stream);
程序例:
#include  
struct mystruct
{
int i;
char ch;
};
int main(void)
{
FILE *stream;
struct mystruct s;
if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
{
fprintf(stderr, "Cannot open output file.n");
return 1;
}
s.i = 0;
s.ch = 'A';
fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
fclose(stream); /* close file */
return 0;
}
函数名: getc
功 能: 从流中取字符
用 法: int getc(FILE *stream);
程序例:
#include  
int main(void)
{
char ch;
printf("Input a character:");
/* read a character from the
standard input stream */
ch = getc(stdin);
printf("The character input was: '%c'n",
ch);
return 0;
}
函数名: getch
功 能: 从控制台无回显地取一个字符
用 法: int getch(void);
程序例:
#include  
#include  
int main(void)
{
char ch;
printf("Input a character:");
ch = getche();
printf("nYou input a '%c'n", ch);
return 0;
}
函数名: getchar
功 能: 从stdin流中读字符
用 法: int getchar(void);
程序例:
#include  
int main(void)
{
int c;
/* Note that getchar reads from stdin and
is line buffered; this means it will
not return until you press ENTER. */
while ((c = getchar()) != 'n')
printf("%c", c);
return 0;
}
函数名: getcwd
功 能: 取当前工作目录
用 法: char *getcwd(char *buf, int n);
程序例:
#include  
#include  
int main(void)
{
char buffer[MAXPATH];
getcwd(buffer, MAXPATH);
printf("The current directory is: %sn", buffer);
return 0;
}
函数名: getenv
功 能: 从环境中取字符串
用 法: char *getenv(char *envvar);
程序例:
#include  
#include  
int main(void)
{
char *s;
s=getenv("COMSPEC"); /* get the comspec environment parameter */
printf("Command processor: %sn",s); /* display comspec parameter */
return 0;
}
函数名: getpass
功 能: 读一个口令
用 法: char *getpass(char *prompt);
程序例:
#include  
int main(void)
{
char *password;
password = getpass("Input a password:");
cprintf("The password is: %srn",
password);
return 0;
}
函数名: gets
功 能: 从流中取一字符串
用 法: char *gets(char *string);
程序例:
#include  
int main(void)
{
char string[80];
printf("Input a string:");
gets(string);
printf("The string input was: %sn",
string);
return 0;
}
函数名: getw
功 能: 从流中取一整数
用 法: int getw(FILE *strem);
程序例:
#include  
#include  
#define FNAME "test.$$$"
int main(void)
{
FILE *fp;
int word;
/* place the word in a file */
fp = fopen(FNAME, "wb");
if (fp == NULL)
{
printf("Error opening file %sn", FNAME);
exit(1);
}
word = 94;
putw(word,fp);
if (ferror(fp))
printf("Error writing to filen");
else
printf("Successful writen");
fclose(fp);
/* reopen the file */
fp = fopen(FNAME, "rb");
if (fp == NULL)
{
printf("Error opening file %sn", FNAME);
exit(1);
}
/* extract the word */
word = getw(fp);
if (ferror(fp))
printf("Error reading filen");
else
printf("Successful read: word = %dn", word);
/* clean up */
fclose(fp);
unlink(FNAME);
return 0;
}
函数名: gmtime
功 能: 把日期和时间转换为格林尼治标准时间(GMT)
用 法: struct tm *gmtime(long *clock);
程序例:
#include  
#include  
#include  
#include  
/* Pacific Standard Time & Daylight Savings */
char *tzstr = "TZ=PST8PDT";
int main(void)
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t = time(NULL);
area = localtime(&t);
printf("Local time is: %s", asctime(area));
gmt = gmtime(&t);
printf("GMT is: %s", asctime(gmt));
return 0;
}
函数名: ioctl
功 能: 控制I/O设备
用 法: int ioctl(int handle, int cmd[,int *argdx, int argcx]);
程序例:
#include  
#include  
#include  
int main(void)
{
int stat;
/* use func 8 to determine if the default drive is removable */
stat = ioctl(0, 8, 0, 0);
if (!stat)
printf("Drive %c is removable.n", getdisk() + 'A');
else
printf("Drive %c is not removable.n", getdisk() + 'A');
return 0;
}

函数名: isatty
功 能: 检查设备类型
用 法: int isatty(int handle);
程序例:
#include  
#include  
int main(void)
{
int handle;
handle = fileno(stdprn);
if (isatty(handle))
printf("Handle %d is a device typen", handle);
else
printf("Handle %d isn't a device typen", handle);
return 0;
}

函数名: itoa
功 能: 把一整数转换为字符串
用 法: char *itoa(int value, char *string, int radix);
程序例:
#include  
#include  
int main(void)
{
int number = 12345;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %sn", number, string);
return 0;
}
函数名: labs
功 能: 取长整型绝对值
用 法: long labs(long n);
程序例:
#include  
#include  
int main(void)
{
long result;
long x = -12345678L;
result= labs(x);
printf("number: %ld abs value: %ldn",
x, result);
return 0;
}

函数名: ldexp
功 能: 计算value*2的幂
用 法: double ldexp(double value, int exp);
程序例:
#include  
#include  
int main(void)
{
double value;
double x = 2;
/* ldexp raises 2 by a power of 3
then multiplies the result by 2 */
value = ldexp(x,3);
printf("The ldexp value is: %lfn",
value);
return 0;
}
函数名: ldiv
功 能: 两个长整型数相除, 返回商和余数
用 法: ldiv_t ldiv(long lnumer, long ldenom);
程序例:
/* ldiv example */
#include  
#include  
int main(void)
{
ldiv_t lx;
lx = ldiv(100000L, 30000L);
printf("100000 div 30000 = %ld remainder %ldn", lx.quot, lx.rem);
return 0;
}

函数名: lfind
功 能: 执行线性搜索
用 法: void *lfind(void *key, void *base, int *nelem, int width,
int (*fcmp)());
程序例:
#include  
#include  
int compare(int *x, int *y)
{
return( *x - *y );
}
int main(void)
{
int array[5] = {35, 87, 46, 99, 12};
size_t nelem = 5;
int key;
int *result;
key = 99;
result = lfind(&key, array, &nelem,
sizeof(int), (int(*)(const void *,const void *))compare);
if (result)
printf("Number %d foundn",key);
else
printf("Number %d not foundn",key);
return 0;
}
函数名: localtime
功 能: 把日期和时间转变为结构
用 法: struct tm *localtime(long *clock);
程序例:
#include  
#include  
#include  
int main(void)
{
time_t timer;
struct tm *tblock;
/* gets time of day */
timer = time(NULL);
/* converts date/time to a structure */
tblock = localtime(&timer);
printf("Local time is: %s", asctime(tblock));
return 0;
}

函数名: lock
功 能: 设置文件共享锁
用 法: int lock(int handle, long offset, long length);
程序例:
#include  
#include  
#include  
#include  
#include  
#include  
int main(void)
{
int handle, status;
long length;
/* Must have DOS Share.exe loaded for */
/* file locking to function properly */
handle = sopen("c:\autoexec.bat",
O_RDONLY,SH_DENYNO,S_IREAD);
if (handle
length = filelength(handle);
status = lock(handle,0L,length/2);
if (status == 0)
printf("lock succeededn");
else
printf("lock failedn");
status = unlock(handle,0L,length/2);
if (status == 0)
printf("unlock succeededn");
else
printf("unlock failedn");
close(handle);
return 0;
}
函数名: log
功 能: 对数函数ln(x)
用 法: double log(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 8.6872;
result = log(x);
printf("The natural log of %lf is %lfn", x, result);
return 0;
}
函数名: log10
功 能: 对数函数log
用 法: double log10(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 800.6872;
result = log10(x);
printf("The common log of %lf is %lfn", x, result);
return 0;
}
函数名: localtime
功 能: 把日期和时间转变为结构
用 法: struct tm *localtime(long *clock);
程序例:
#include  
#include  
#include  
int main(void)
{
time_t timer;
struct tm *tblock;
/* gets time of day */
timer = time(NULL);
/* converts date/time to a structure */
tblock = localtime(&timer);
printf("Local time is: %s", asctime(tblock));
return 0;
}

函数名: lock
功 能: 设置文件共享锁
用 法: int lock(int handle, long offset, long length);
程序例:
#include  
#include  
#include  
#include  
#include  
#include  
int main(void)
{
int handle, status;
long length;
/* Must have DOS Share.exe loaded for */
/* file locking to function properly */
handle = sopen("c:\autoexec.bat",
O_RDONLY,SH_DENYNO,S_IREAD);
if (handle
length = filelength(handle);
status = lock(handle,0L,length/2);
if (status == 0)
printf("lock succeededn");
else
printf("lock failedn");
status = unlock(handle,0L,length/2);
if (status == 0)
printf("unlock succeededn");
else
printf("unlock failedn");
close(handle);
return 0;
}
函数名: log
功 能: 对数函数ln(x)
用 法: double log(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 8.6872;
result = log(x);
printf("The natural log of %lf is %lfn", x, result);
return 0;
}
函数名: log10
功 能: 对数函数log
用 法: double log10(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 800.6872;
result = log10(x);
printf("The common log of %lf is %lfn", x, result);
return 0;
}
函数名: localtime
功 能: 把日期和时间转变为结构
用 法: struct tm *localtime(long *clock);
程序例:
#include  
#include  
#include  
int main(void)
{
time_t timer;
struct tm *tblock;
/* gets time of day */
timer = time(NULL);
/* converts date/time to a structure */
tblock = localtime(&timer);
printf("Local time is: %s", asctime(tblock));
return 0;
}

函数名: lock
功 能: 设置文件共享锁
用 法: int lock(int handle, long offset, long length);
程序例:
#include  
#include  
#include  
#include  
#include  
#include  
int main(void)
{
int handle, status;
long length;
/* Must have DOS Share.exe loaded for */
/* file locking to function properly */
handle = sopen("c:\autoexec.bat",
O_RDONLY,SH_DENYNO,S_IREAD);
if (handle
length = filelength(handle);
status = lock(handle,0L,length/2);
if (status == 0)
printf("lock succeededn");
else
printf("lock failedn");
status = unlock(handle,0L,length/2);
if (status == 0)
printf("unlock succeededn");
else
printf("unlock failedn");
close(handle);
return 0;
}
函数名: log
功 能: 对数函数ln(x)
用 法: double log(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 8.6872;
result = log(x);
printf("The natural log of %lf is %lfn", x, result);
return 0;
}
函数名: log10
功 能: 对数函数log
用 法: double log10(double x);
程序例:
#include  
#include  
int main(void)
{
double result;
double x = 800.6872;
result = log10(x);
printf("The common log of %lf is %lfn", x, result);
return 0;
}
函数名: lsearch
功 能: 线性搜索
用 法: void *lsearch(const void *key, void *base, size_t *nelem,
size_t width, int (*fcmp)(const void *, const void *));
程序例:
#include  
#include  
int compare(int *x, int *y)
{
return( *x - *y );
}
int main(void)
{
int array[5] = {35, 87, 46, 99, 12};
size_t nelem = 5;
int key;
int *result;
key = 99;
result = lfind(&key, array, &nelem,
sizeof(int), (int(*)(const void *,const void *))compare);
if (result)
printf("Number %d foundn",key);
else
printf("Number %d not foundn",key);
return 0;
}

函数名: lseek
功 能: 移动文件读/写指针
用 法: long lseek(int handle, long offset, int fromwhere);
程序例:
#include  
#include  
#include  
#include  
#include  
int main(void)
{
int handle;
char msg[] = "This is a test";
char ch;
/* create a file */
handle = open("TEST.$$$", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
/* write some data to the file */
write(handle, msg, strlen(msg));
/* seek to the begining of the file */
lseek(handle, 0L, SEEK_SET);
/* reads chars from the file until we hit EOF */
do
{
read(handle, &ch, 1);
printf("%c", ch);
} while (!eof(handle));
close(handle);
return 0;
}
函数名: memccpy
功 能: 从源source中拷贝n个字节到目标destin中
用 法: void *memccpy(void *destin, void *source, unsigned char ch,
unsigned n);
程序例:
#include  
#include  
int main(void)
{
char *src = "This is the source string";
char dest[50];
char *ptr;
ptr = memccpy(dest, src, 'c', strlen(src));
if (ptr)
{
*ptr = '0';
printf("The character was found: %sn", dest);
}
else
printf("The character wasn't foundn");
return 0;
}
函数名: malloc
功 能: 内存分配函数
用 法: void *malloc(unsigned size);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
char *str;
/* allocate memory for string */
/* This will generate an error when compiling */
/* with C++, use the new operator instead. */
if ((str = malloc(10)) == NULL)
{
printf("Not enough memory to allocate buffern");
exit(1); /* terminate program if out of memory */
}
/* copy "Hello" into string */
strcpy(str, "Hello");
/* display string */
printf("String is %sn", str);
/* free memory */
free(str);
return 0;
}
函数名: memchr
功 能: 在数组的前n个字节中搜索字符
用 法: void *memchr(void *s, char ch, unsigned n);
程序例:
#include  
#include  
int main(void)
{
char str[17];
char *ptr;
strcpy(str, "This is a string");
ptr = memchr(str, 'r', strlen(str));
if (ptr)
printf("The character 'r' is at position: %dn", ptr - str);
else
printf("The character was not foundn");
return 0;
}
函数名: memcpy
功 能: 从源source中拷贝n个字节到目标destin中
用 法: void *memcpy(void *destin, void *source, unsigned n);
程序例:
#include  
#include  
int main(void)
{
char src[] = "******************************";
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
char *ptr;
printf("destination before memcpy: %sn", dest);
ptr = memcpy(dest, src, strlen(src));
if (ptr)
printf("destination after memcpy: %sn", dest);
else
printf("memcpy failedn");
return 0;
}
函数名: memmove
功 能: 移动一块字节
用 法: void *memmove(void *destin, void *source, unsigned n);
程序例:
#include  
#include  
int main(void)
{
char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
char *src = "******************************";
printf("destination prior to memmove: %sn", dest);
memmove(dest, src, 26);
printf("destination after memmove: %sn", dest);
return 0;
}

函数名: memset
功 能: 设置s中的所有字节为ch, s数组的大小由n给定
用 法: void *memset(void *s, char ch, unsigned n);
程序例:
#include  
#include  
#include  
int main(void)
{
char buffer[] = "Hello worldn";
printf("Buffer before memset: %sn", buffer);
memset(buffer, '*', strlen(buffer) - 1);
printf("Buffer after memset: %sn", buffer);
return 0;
}
函数名: mkdir
功 能: 建立一个目录
用 法: int mkdir(char *pathname);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
int status;
clrscr();
status = mkdir("asdfjklm");
(!status) ? (printf("Directory createdn")) :
(printf("Unable to create directoryn"));
getch();
system("dir");
getch();
status = rmdir("asdfjklm");
(!status) ? (printf("Directory deletedn")) :
(perror("Unable to delete directory"));
return 0;
}
函数名: mktemp
功 能: 建立唯一的文件名
用 法: char *mktemp(char *template);
程序例:
#include  
#include  
int main(void)
{
/* fname defines the template for the
temporary file. */
char *fname = "TXXXXXX", *ptr;
ptr = mktemp(fname);
printf("%sn",ptr);
return 0;
}
函数名: modf
功 能: 把数分为指数和尾数
用 法: double modf(double value, double *iptr);
程序例:
#include  
#include  
int main(void)
{
double fraction, integer;
double number = 100000.567;
fraction = modf(number, &integer);
printf("The whole and fractional parts of %lf are %lf and %lfn",
number, integer, fraction);
return 0;
}
函数名: open
功 能: 打开一个文件用于读或写
用 法: int open(char *pathname, int access[, int permiss]);
程序例:
#include  
#include  
#include  
#include  
int main(void)
{
int handle;
char msg[] = "Hello world";
if ((handle = open("TEST.$$$", O_CREAT | O_TEXT)) == -1)
{
perror("Error:");
return 1;
}
write(handle, msg, strlen(msg));
close(handle);
return 0;
}
函数名: perror
功 能: 系统错误信息
用 法: void perror(char *string);
程序例:
#include  
int main(void)
{
FILE *fp;
fp = fopen("perror.dat", "r");
if (!fp)
perror("Unable to open file for reading");
return 0;
}
函数名: pow
功 能: 指数函数(x的y次方)
用 法: double pow(double x, double y);
程序例:
#include  
#include  
int main(void)
{
double x = 2.0, y = 3.0;
printf("%lf raised to %lf is %lfn", x, y, pow(x, y));
return 0;
}
函数名: pow10
功 能: 指数函数(10的p次方)
用 法: double pow10(int p);
程序例:
#include  
#include  
int main(void)
{
double p = 3.0;
printf("Ten raised to %lf is %lfn", p, pow10(p));
return 0;
}
函数名: printf
功 能: 产生格式化输出的函数
用 法: int printf(char *format...);
程序例:
#include  
#include  
#define I 555
#define R 5.5
int main(void)
{
int i,j,k,l;
char buf[7];
char *prefix = buf;
char tp[20];
printf("prefix 6d 6o 8x 10.2e "
"10.2fn");
strcpy(prefix,"%");
for (i = 0; i
函数名: putc
功 能: 输出一字符到指定流中
用 法: int putc(int ch, FILE *stream);
程序例:
#include  
int main(void)
{
char msg[] = "Hello worldn";
int i = 0;
while (msg)
putc(msg[i++], stdout);
return 0;
}
函数名: putchar
功 能: 在stdout上输出字符
用 法: int putchar(int ch);
程序例:
#include  
/* define some box-drawing characters */
#define LEFT_TOP 0xDA
#define RIGHT_TOP 0xBF
#define HORIZ 0xC4
#define VERT 0xB3
#define LEFT_BOT 0xC0
#define RIGHT_BOT 0xD9
int main(void)
{
char i, j;
/* draw the top of the box */
putchar(LEFT_TOP);
for (i=0; i
/* draw the middle */
for (i=0; i
/* draw the bottom */
putchar(LEFT_BOT);
for (i=0; i
return 0;
}
函数名: putenv
功 能: 把字符串加到当前环境中
用 法: int putenv(char *envvar);
程序例:
#include  
#include  
#include  
#include  
#include  
int main(void)
{
char *path, *ptr;
int i = 0;
/* get the current path environment */
ptr = getenv("PATH");
/* set up new path */
path = malloc(strlen(ptr)+15);
strcpy(path,"PATH=");
strcat(path,ptr);
strcat(path,";c:\temp");
/* replace the current path and display current environment */
putenv(path);
while (environ)
printf("%sn",environ[i++]);
return 0;
}
函数名: putenv
功 能: 把字符串加到当前环境中
用 法: int putenv(char *envvar);
程序例:
#include  
#include  
#include  
#include  
#include  
int main(void)
{
char *path, *ptr;
int i = 0;
/* get the current path environment */
ptr = getenv("PATH");
/* set up new path */
path = malloc(strlen(ptr)+15);
strcpy(path,"PATH=");
strcat(path,ptr);
strcat(path,";c:\temp");
/* replace the current path and display current environment */
putenv(path);
while (environ)
printf("%sn",environ[i++]);
return 0;
}
函数名: puts
功 能: 送一字符串到流中
用 法: int puts(char *string);
程序例:
#include  
int main(void)
{
char string[] = "This is an example output stringn";
puts(string);
return 0;
}
函数名: putw
功 能: 把一字符或字送到流中
用 法: int putw(int w, FILE *stream);
程序例:
#include  
#include  
#define FNAME "test.$$$"
int main(void)
{
FILE *fp;
int word;
/* place the word in a file */
fp = fopen(FNAME, "wb");
if (fp == NULL)
{
printf("Error opening file %sn", FNAME);
exit(1);
}
word = 94;
putw(word,fp);
if (ferror(fp))
printf("Error writing to filen");
else
printf("Successful writen");
fclose(fp);
/* reopen the file */
fp = fopen(FNAME, "rb");
if (fp == NULL)
{
printf("Error opening file %sn", FNAME);
exit(1);
}
/* extract the word */
word = getw(fp);
if (ferror(fp))
printf("Error reading filen");
else
printf("Successful read: word = %dn", word);
/* clean up */
fclose(fp);
unlink(FNAME);
return 0;
}
函数名: 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
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
功 能: 随机数发生器
用 法: void rand(void);
程序例:
#include  
#include  
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99nn");
for(i=0; i
函数名: random
功 能: 随机数发生器
用 法: int random(int num);
程序例:
#include  
#include  
#include  
/* prints a random number in the range 0 to 99 */
int main(void)
{
randomize();
printf("Random number in the 0-99 range: %dn", random (100));
return 0;
}
函数名: read
功 能: 从文件中读
用 法: int read(int handle, void *buf, int nbyte);
程序例:
#include  
#include  
#include  
#include  
#include  
#include  
int main(void)
{
void *buf;
int handle, bytes;
buf = malloc(10);
/*
Looks for a file in the current directory named TEST.$$$ and attempts
to read 10 bytes from it. To use this example you should create the
file TEST.$$$
*/
if ((handle =
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
{
printf("Error Opening Filen");
exit(1);
}
if ((bytes = read(handle, buf, 10)) == -1) {
printf("Read Failed.n");
exit(1);
}
else {
printf("Read: %d bytes read.n", bytes);
}
return 0;
}
函数名: realloc
功 能: 重新分配主存
用 法: void *realloc(void *ptr, unsigned newsize);
程序例:
#include  
#include  
#include  
int main(void)
{
char *str;
/* allocate memory for string */
str = malloc(10);
/* copy "Hello" into string */
strcpy(str, "Hello");
printf("String is %sn Address is %pn", str, str);
str = realloc(str, 20);
printf("String is %sn New address is %pn", str, str);
/* free memory */
free(str);
return 0;
}
函数名: remove
功 能: 删除一个文件
用 法: int remove(char *filename);
程序例:
#include  
int main(void)
{
char file[80];
/* prompt for file name to delete */
printf("File to delete: ");
gets(file);
/* delete the file */
if (remove(file) == 0)
printf("Removed %s.n",file);
else
perror("remove");
return 0;
}
函数名: rename
功 能: 重命名文件
用 法: int rename(char *oldname, char *newname);
程序例:
#include  
int main(void)
{
char oldname[80], newname[80];
/* prompt for file to rename and new name */
printf("File to rename: ");
gets(oldname);
printf("New name: ");
gets(newname);
/* Rename the file */
if (rename(oldname, newname) == 0)
printf("Renamed %s to %s.n", oldname, newname);
else
perror("rename");
return 0;
}
函数名: rewind
功 能: 将文件指针重新指向一个流的开头
用 法: int rewind(FILE *stream);
程序例:
#include  
#include  
int main(void)
{
FILE *fp;
char *fname = "TXXXXXX", *newname, first;
newname = mktemp(fname);
fp = fopen(newname,"w+");
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
rewind(fp);
fscanf(fp,"%c",&first);
printf("The first character is: %cn",first);
fclose(fp);
remove(newname);
return 0;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/84425/showart_2069413.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP