本文目录一览:
求C语言函数大全
函数名: abort
功能: 异常终止一个进程
用法: void abort(void);
程序例:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Calling abort()\n");
abort();
return 0; /* This is never reached */
}
函数名: abs
功能: 求整数的绝对值
用法: int abs(int i);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
函数名: absread, abswrite
功能: 绝对磁盘扇区读、写数据
用法:
int absread(int drive, int nsects, int sectno, void *buffer);
int abswrite(int drive, int nsects, int sectno, void *buffer);
程序例:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>
int main(void)
{
int i, strt, ch_out, sector;
char buf[512];
printf("Insert a diskette into drive A and press any key\n");
getch();
sector = 0;
if (absread(0, 1, sector, buf) != 0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK\n");
strt = 3;
for (i=0; i<80; i++)
{
ch_out = buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}
函数名: access
功能: 确定文件的访问权限
用法: int access(const char *filename, int amode);
程序例:
#include <stdio.h>
#include <io.h>
int file_exists(char *filename);
int main(void)
{
printf("Does NOTEXIST.FIL exist: %s\n",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
函数名: acos
功能: 反余弦函数
用法: double acos(double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}
函数名: allocmem
功能: 分配DOS存储段
用法: int allocmem(unsigned size, unsigned *seg);
程序例:
#include <dos.h>
#include <alloc.h>
#include <stdio.h>
int main(void)
{
unsigned int size, segp;
int stat;
size = 64; /* (64 x 16) = 1024 bytes */
stat = allocmem(size, &segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",
stat);
return 0;
}
函数名: arc
功能: 画一弧线
用法: void far arc(int x, int y, int stangle, int endangle, int radius);
程序例:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
arc(midx, midy, stangle, endangle, radius);
getch();
closegraph();
return 0;
}
函数名: asctime
功能: 转换日期和时间为ASCII码
用法: char *asctime(const struct tm *tblock);
程序例:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void)
{
struct tm t;
char str[80];
t.tm_sec = 1;
t.tm_min = 30;
t.tm_hour = 9;
t.tm_mday = 22;
t.tm_mon = 11;
t.tm_year = 56;
t.tm_wday = 4;
t.tm_yday = 0;
t.tm_isdst = 0;
strcpy(str, asctime(&t));
printf("%s\n", str);
return 0;
}
函数名: asin
功能: 反正弦函数
用法: double asin(double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}
函数名: assert
功能: 测试一个条件并可能使程序终止
用法: void assert(int test);
程序例:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct ITEM {
int key;
int value;
};
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
}
int main(void)
{
additem(NULL);
return 0;
}
函数名: atan
功能: 反正切函数
用法: double atan(double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}
函数名: atan2
功能: 计算Y/X的反正切值
用法: double atan2(double y, double x);
程序例:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 90.0, y = 45.0;
result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}
函数名: atexit
功能: 注册终止函数
用法: int atexit(atexit_t func);
程序例:
#include <stdio.h>
#include <stdlib.h>
void exit_fn1(void)
{
printf("Exit function #1 called\n");
}
void exit_fn2(void)
{
printf("Exit function #2 called\n");
}
int main(void)
{
atexit(exit_fn1);
atexit(exit_fn2);
return 0;
}
函数名: atof
功能: 把字符串转换成浮点数
用法: double atof(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
函数名: atoi
功能: 把字符串转换成长整型数
用法: int atoi(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
函数名: atol
功能: 把字符串转换成长整型数
用法: long atol(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long l;
char *str = "98765432";
l = atol(str);
printf("string = %s integer = %ld\n", str, l);
return(0);
}
有谁能帮我将C语言中所有的函数及其功能一一列举一下,不胜感激
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[]="abcde",s2[]="scasasa";
strcpy(s1,s2);
return 0;
}
如果 s2
长度大于 s1
则会覆盖掉。如果小于的话只是将 s2
的 \0
放在 s1
中 \0
的前面罢了,而后面处理字符串的函数是遇到 \0
就收手。
函数名: strncpy
功能: 串拷贝
用法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strncpy(string, str1, 3);
string[3] = '\0';
printf("%s\n", string);
return 0;
}
函数名: remove
功能: 删除一个文件
用法: int remove(char *filename);
程序例:
#include <stdio.h>
int main(void)
{
char file[80];
printf("file to delete: ");
gets(file);
if (remove(file) == 0)
printf("removed %s.\n", file);
else
perror("remove");
return 0;
}
函数名: rename
功能: 重命名文件
用法: int rename(char *oldname, char *newname);
程序例:
#include <stdio.h>
int main(void)
{
char oldname[80], newname[80];
printf("file to rename: ");
gets(oldname);
printf("new name: ");
gets(newname);
if (rename(oldname, newname) == 0)
printf("renamed %s to %s.\n", oldname, newname);
else
perror("rename");
return 0;
}
c语言常用函数有哪些?主要掌握的要点是什么
函数1. absread()
功能: 读磁盘绝对扇区函数
原形: int absread(int drive, int num, int sectnum, void *buf)
参数:
drive=0
对应 A 盘,drive=1
对应 B 盘
返回值:0
: 成功-1
: 失败
头文件:dos.h
函数2. abswrite()
功能: 写磁盘绝对扇区函数
原形: int abswrite(int drive, int nsects, int lsect, void *buffer)
参数:
drive=0(A驱动器)
,1(B驱动器)
nsects=要写的扇区数(最多64K个)
lsect=起始逻辑扇区号
buffer=要写入数据的内存起始地址
返回值:0
: 成功-1
: 失败
头文件:dos.h
函数3. atof()
功能: 将字符串转换成浮点数的函数
原形: double atof(const char *s)
返回值: 字符串的转换值
头文件: math.h
, stdlib.h
函数4. atoi()
功能: 将字符串转换成整型数的函数
原形: int atoi(const char *s)
返回值: 字符串的转换值。若出错则返回 0
头文件: stdlib.h
函数5. atol()
功能: 将字符串转换成长整型数的函数
原形: long atol(const char *s)
返回值: 字符串的转换值。若出错则返回 0
头文件: stdlib.h
函数6. bcd()
功能: 把一个数转换成对应的BCD码的函数
原形: bcd bcd(int x)
注意: BCD码的精度可达17位
头文件: bcd.h
函数7. bdos()
功能: DOS系统调用
原形: int bdos(int fnum, unsigned dx, unsigned al)
返回值: AX
中的值
函数8. biosdisk()
功能: 调用BIOS磁盘驱动程序函数
原形: char biosdisk(int cmd, int drive, int head, int track, int sector, int nsects, void *buffer)
参数:
cmd
:2
: 数据以每扇区512字节的格式读入buffer3
: 将buffer中的数据按每扇区512字节写入磁盘4
: 对扇区进行数据校验6
: 格式化一个磁盘并对化扇区设置化标志8
: 返回当前驱动器参数在buffer的前4个字节中10
: 长读,每扇区读入512字节加上额外的4个字节11
: 长写,每扇区写入512字节加上额外的4个字节
drive
:0
: 第一个软驱1
: 第二个软驱0x80
: 第一个硬驱
head
: 磁头track
: 磁道sector
: 扇区nsects
: 扇区数buffer
: 数据缓冲区,定义为unsigned char buffer[];
返回值:0x00
: 操作成功0x01
: 错误命令0x03
: 企图写具有写保护的磁盘0x07
: 驱动器参数设置错误0x10
: 磁盘读/CRC/ECC错误
头文件:bios.h
注: 对于硬盘主引导记录扇区head=0
,track=0
,sector=1
函数9. biosprint()
功能: 调用BIOS打印机I/O接口的函数
原形: int biosprint(int cmd, int abyte, int port)
参数:
port=0(LPT1)
,1(LPT2)
cmd=0(打印字符abyte)
,1(初始化打印机端口)
,2(读打印机状态)
abyte=0-255
返回值: 打印机状态- 位0: 设备超时
- 位3: I/O出错
- 位4: 打印机已选择
- 位5: 没纸
- 位6: 打印机确认
- 位7: 不忙
头文件:bios.h
函数10. calloc()
功能: 在堆中分配一块内存,将该内存的内容全部清0
原形: void *calloc(size_t nitems, size_t size)
返回值: 返回指向新分配内存的指针。空间不够则返回 NULL
头文件: stdlib.h
, calloc.h
函数11. ceil()
功能: 求不小于num的最小双精度整数
原形: double ceil(double num)
头文件: math.h
函数12. cgets()
功能: 从控制台读入一个字符串,并将该字符串(和字符串长度)存入有str所指向的地址中
原形: char *cgets(char *str)
返回值: 指向 str[2]
的指针
头文件: conio.h
函数13. chdir()
功能: 把由path指定的目录改为当前目录
原形: int chdir(const char *path)
返回值:
0
: 成功-1
: 失败
头文件:dir.h
函数14. _chmod()
功能: 读取或设置DOS文件属性
原形: int _chmod(const char *path, int func[, int attrib])
返回值:
- 失败时返回
-1
- 成功时返回文件的属性字节
头文件:io.h
,dos.h
函数15. chmod()
功能: 设置由path所指文件的存取权限
原形: int chmod(const char *path, int amode)
返回值:
0
: 成功-1
: 失败
头文件:io.h
,sys/stat.h
函数16. clock()
功能: 测得从程序开始到调用处处理机所用的时间
原形: long clock(void)
头文件: time.h
函数17. close()
功能: 关闭由文件句柄所指向的文件
原形: int close(int handle)
返回值:
0
: 成功-1
: 失败
头文件:io.h
函数18. closegraph()
功能: 释放图形系统分配的所有内存,将屏幕恢复到调用initgraph之前的模式
原形: void far closegraph(void)
头文件: graphics.h
函数19. cos()
功能: 计算arg(弧度)的余弦值
原形: double cos(double arg)
头文件: math.h
函数20. ctrlbrk()
功能: 修改中断向量0x23, 使用新的ctrl-break中断处理函数
原形: void ctrlbrk(int(*handle)(void))
头文件: dos.h
函数21. delay()
功能: 暂停当前所执行的程序milliseconds毫秒
原形: void delay(unsigned milliseconds)
头文件: dos.h
函数22. disable()
功能: 屏蔽中断,只允许从外部设备来的不可屏蔽中断(NMI)
原形: void disble(void)
头文件: dos.h
函数23. enable()
功能: 开放中断,允许接受任何设备产生的中断
原形: void enable(void)
头文件: dos.h
函数24. exec()
功能: 加载并运行其它程序
原形:
int execl(char *path, char *arg0, *arg1, ..., *argn, NULL)
int execle(char *path, char *arg0, *arg1, ..., *argn, NULL, char **env)
int execlp(char *path, char *arg0, *arg1, ..., *argn, NULL)
int execlpe(char *path, char *arg0, *arg1, ..., *argn, NULL, char **env)
int execv(char *path, char *argv[])
int execve(char *path, char *argv[], char **env)
int execvp(char *path, char *argv[])
int execvpe(char *path, char *argv[], char **env)
头文件: process.h
函数25. farcalloc()
功能: 从远堆中为包含nunits个元素的数组分配内存
原形: void far *farcalloc(unsigned long nunits, unsigned long unitsz)
返回值: 返回指向新分配块的指针,若内存不够,则返回 NULL
头文件: alloc.h
函数26. farfree()
功能: 从远堆中释放一块已分配内存
原形: void farfree(void far *block)
头文件: alloc.h
函数27. farmalloc()
功能: 从远堆中分配长nbytes字节的内存
原形: void far *farmalloc(unsigned long nbytes)
返回值: 返回指向新分配内存的指针,若内存不够,则返回 NULL
头文件: alloc.h
函数28. farrealloc()
功能: 调整远堆中已分配块的大小
原形: void far *farrealloc(void far *oldblock, unsigned long nbytes)
返回值: 返回调整后的新内存地址。若不能重新分配,则返回 NULL
头文件: alloc.h
函数29. fclose()
功能: 关闭指定的流
原形: int fclose(FILE *stream)
返回值:
0
: 成功EOF
: 失败
头文件:stdio.h
函数30. fcloseall()
功能: 关闭所有打开的流
原形: int fcloseall(void)
返回值:
- 关闭流的总数
- 如果发现错误则返回
EOF
头文件:stdio.h
函数31. feof()
功能: 检查文件是否结束
原形: int feof(FILE *fp)
返回值:
- 文件结束返回非0值
- 否则返回
0
头文件:stdio.h
函数32. fgets()
功能: 从输入流stream中读入字符存到s串中
原形: char *fgets(char s[], int n, FILE *stream)
返回值:
- 成功时返回字符串参数s
- 出错或遇到文件结束时,返回
NULL
头文件:stdio.h
函数33. findfirst() 和 findnext()
功能: 检索由path和attr指定的文件,把结果返回到buffer
原形:
int findfirst(char *path, struct ffblk *buffer, int attr)
int findnext(struct ffblk *buffer)
结构体定义:
struct ffblk {
char ff_reserved[21];
char ff_attrib;
unsigned ff_ftime;
unsigned ff_fdate;
long ff_fsize;
char ff_name[13];
};
返回值:
0
: 检索成功-1
: 没有找到指定的文件
头文件:dir.h
(dos.h)
函数34. floodfill()
功能: 在图形设备上用颜色border围起来的区域将用当前填充颜色填充
原形: void far floodfill(int x, int y, int border)
头文件: graphics.h
函数35. floor()
功能: 求不大于num的最大双精度整数
原形: double floor(double num)
头文件: math.h
函数36. fnmerge()
功能: 合成drive:\dir\name.ext,放在path
原形: void fnmerge(char *path, const char *drive, const char *dir, const char *name, const char *ext)
头文件: dir.h
函数37. fnsplit()
功能: 把文件名路径path分成4个部分存放
原形: int fnsplit(char *path, const char *drive, const char *dir, const char *name, const char *ext)
返回值:
- 如果有扩展名,则
EXTENSION != 0
- 如果有文件名,则
FILENAME != 0
- 如果有目录名,则
DIRECTORY != 0
- 如果有驱动器号,则
DIRVE != 0
头文件:dir.h
函数38. fopen()
功能: 打开用filename指定的文件,并使其与一个流相联
原形: FILE *fopen(const char *filename, const char *mode)
处理方式:
"rt"
: 打开一个文本文件,只能读"wt"
: 生成一个文本文件,只能写"at"
: 打开一个文本文件,只能在文件尾部添加"rb"
: 打开一个二进制文件,只能读"wb"
: 生成一个二进制文件,只能写"ab"
: 打开一个二进制文件,只能在文件尾部添加"rt+"
: 打开一个文本文件,可读可写"wt+"
: 生成一个文本文件,可读可写"at+"
: 打开一个文本文件,可读可添加"rb+"
: 打开一个二进制文件,可读可写"wb+"
: 生成一个二进制文件,可读可写"ab+"
: 打开一个二进制文件,可读可添加
返回值:- 指明流的指针(成功时)或
NULL
(失败时)
头文件:stdio.h
函数39. FP_OFF()
功能: 取得和设置远指针*p的偏移量
原形: unsigned FP_OFF(void far *p)
返回值: 偏移量
头文件: dos.h
函数40. fprintf()
功能: 向文件指针指向的文件输出ASCII代码
原形: int fprintf(FILE *stream, const char *format[, argument, ...])
返回值:
- 成功则返回输出的字节数
- 错误则返回
EOF
头文件:stdio.h
函数41. FP_SEG()
功能: 取得和设置远指针*p段地址值
原形: unsigned FP_SEG(void far *p)
返回值: 段地址值
头文件: dos.h
函数42. free()
功能: 释放由calloc、malloc、realloc函数调用所分配的内存
原形: void free(void *block)
头文件: stdlib.h
, alloc.h
函数43. fscanf()
功能: 从一个流中扫描输入字段
原形: int fscanf(FILE *stream, const char *format[, address, ...])
头文件: stdio.h
函数44. fseek()
功能: 移动文件指针
原形: int fseek(FILE *stream, long offset, int whence)
返回值:
0
: 成功- 非0值: 失败
头文件:stdio.h
函数45. fwrite()
功能: 附加n个数据项到指定的输出文件后
原形: size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream)
返回值:
- 调用成功时返回实际写的数据项数
- 出错时返回一短整形数值
头文件:stdio.h
函数46. gcvt()
功能: 把双精度数转化成字符串
原形: char *gcvt(double value, int ndigit, char *buf)
头文件: stdlib.h
函数47. geninterrupt()
功能: 产生一个8086软中断
原形: void geninterrupt(int n)
头文件: dos.h
函数48. getc()
功能: 返回输入流stream中一个字符
原形: int getc(FILE *stream)
头文件: stdio.h
函数49. getch()
功能: 从键盘无回显地读取一字符
原形: int getch(void)
头文件: conio.h
函数50. getchar()
功能: 从stdin流中读取一个字符
原形: int getchar(void)
头文件: stdio.h
函数51. getcurdir()
功能: 读取指定驱动器的当前目录
原形: int getcurdir(int drive, char directory)
返回值:
0
: 调用成功1
: 出错
头文件:dir.h
函数52. getcwd()
功能: 读取当前目录的完整路径名
原形: char *getcwd(char *buf, int buflen)
返回值:
- 若buf非空,调用成功返回buf,出错返回
NULL
- 若buf为
NULL
,返回指向已经分配的内存缓冲区地址
头文件:dir.h
函数53. getdate()
功能: 读取系统日期
原形: void getdate(struct date *pdate)
结构体定义:
struct date {
int da_year;
int da_day;
int da_mon;
};
头文件: dos.h
函数54. getdfree()
功能: 读取磁盘空闲空间
原形: void getdfree(unsigned char drive, struct dfree *dtable)
结构体定义:
struct dfree {
unsigned df_avail;
unsigned df_total;
unsigned df_bsec;
unsigned df_sclus;
};
头文件: dos.h
函数55. getdisk()
功能: 读取当前磁盘驱动器号
原形: int getdisk(void)
头文件: dir.h
函数56. getenv()
功能: 返回一给定的环境变量值
原形: char *getenv(const char *name)
头文件: stdlib.h
函数57. getimage()
功能: 将图像(矩形区域)从屏幕拷贝到内存
原形: void far getimage(int left, int top, int right, int bottom, void far *bitmap)
头文件: graphics.h
函数58. getmaxx()
功能: 返回当前图形驱动程序和图形模式下最大的X坐标值
原形: int far getmaxx(void)
头文件: graphics.h
函数59. getmaxy()
功能: 返回当前图形驱动程序和图形模式下最大的Y坐标值
原形: int far getmaxy(void)
头文件: graphics.h
函数60. getpixel()
功能: 读取像素颜色
原形: unsigned far getpixel(int x, int y)
头文件: graphics.h
函数61. getpsp()
功能: 返回程序段前缀(PSP)的段地址
原形: unsigned getpsp(void)
头文件: dos.h
函数62. gets()
功能: 从标准输入流stdio中读取一字符串
原形: char *gets(char *s)
头文件: stdio.h
函数63. gettime()
功能: 读取系统时间
原形: void gettime(struct time *ptime)
结构体定义:
struct time {
unsigned char ti_min;
unsigned char ti_hour;
unsigned char ti_hund;
unsigned char ti_sec;
};
头文件: dos.h
函数64. getvect()
功能: 读入由interruptno所指定的中断向量值
原形: void interrupt(*getvect(int interruptno))
返回值: 指定中断向量的当前4字节值
头文件: dos.h
函数65. getx()
功能: 返回当前图形方式下位置的X坐标值
原形: int far getx(void);
头文件: graphics.h
函数66. gety()
功能: 返回当前图形方式下位置的Y坐标值
原形: int far gety(void);
头文件: graphics.h
函数67. imagesize()
功能: 确定保存图像所需的存储区大小
原形: unsigned far imagesize(int left, int top, int right, int bottom)
返回值: 存储区的大小(字节),若所需内存大于等于64K-1字节,则返回 0xffff(-1)
头文件: graphics.h
函数68. initgraph()
功能: 把图形适配器设置为一种图形模式
原形: void far initgraph(int far *driver, int far *mode, char far *path)
头文件: graphics.h
C语言中的标准函数有哪些
在最新的 C99 标准中,只有以下两种定义方式是正确的:
int main(void)
{
return 0;
}
int main(int argc, char *argv[])
{
return 0;
}
int
指明了 main()
函数的返回类型,函数名后面的圆括号一般包含传递给函数的信息。void
表示没有给函数传递参数。关于带参数的形式,我们等会讨论。
浏览老版本的 C 代码,将会发现程序常常以:
main()
这种形式开始。C90 标准允许这种形式,但是 C99 标准不允许。因此即使你当前的编译器允许,也不要这么写。 你还可能看到过另一种形式:
void main()
有些编译器允许这种形式,但是还没有任何标准考虑接受它。C++ 之父 Bjarne Stroustrup 在他的主页上的 FAQ 中明确地表示:void main()
的定义从来就不存在于 C++ 或者 C。所以,编译器不必接受这种形式,并且很多编译器也不允许这么写。
c语言中有哪些函数
C语言输入输出函数有很多,标准I/O函数中包含了如下几个常用的函数:
scanf
, printf
, getc
, putc
, getchar
, putchar
, gets
, puts
, fgets
, fputs
, fgetc
, fputc
, fscanf
, fprintf
等.
函数名: getc
功能: 从文件中读出一个字符
用法: int getc(FILE *fp)
说明:
- 常用的判断文件是否读取结束的语句为
(ch = getc(fp)) != EOF
EOF
为文件结束标志,定义在stdio.h
中- 当
fp
为stdin
时,getc(stdin)
就等同于getchar()
了
函数名: putc
功能: 把字符 ch
写到文件 fp
中去
用法: int putc(int ch, FILE *fp)
说明:
- 当
fp
为stdout
时,putc
就等同于putchar()
了
函数名: getchar
功能: 从标准输入流读取一个字符
用法: int getchar(void)
说明:
- 默认的标准输入流即
stdio.h
中定义的stdin
- 从输入流中读取字符时又涉及到缓冲的问题
- 一般是通过在屏幕上敲上回车键,然后将回车前的字符串放在缓冲区中,
getchar
就在缓冲区中一个一个的读字符
函数名: putchar
功能: 把字符 ch
写到标准流 stdout
中去
用法: int putchar(int ch)
函数名: gets
功能: 从标准输入流读取字符串并回显,读到换行符时退出,并会将换行符省去
用法: char *gets(char *str)
函数名: puts
功能: 把字符串 str
写到标准流 stdout
中去,并会在输出到最后时添加一个换行符
用法: int puts(char *str)
函数名: fgets
功能: 读一行字符,该行的字符数不大于 num-1
用法: char *fgets(char *str, int num, FILE *fp)
说明:
str
是存放读入的字符数组指针num
是最大允许的读入字符数fp
是文件指针fgets
函数会在末尾加上一个空字符以构成一个字符串fgets
在读取到换行符后不会将其省略
函数名: fputs
功能: 将 str
写入 fp
用法: int fputs(char *str, FILE *fp)
说明:
fputs
与puts
的不同之处是fputs
在打印时并不添加换行符
函数名: fgetc
功能: 从 fp
的当前位置读取一个字符
用法: int fgetc(FILE *fp)
函数名: fputc
功能: 将 ch
写入 fp
当前指定位置
用法: int fputc(int ch, FILE *fp)
函数名: fscanf
功能: 按照指定格式从文件中出读出数据,并赋值到参数列表中
用法: int fscanf(FILE *fp, char *format, 输入列表)
函数名: fprintf
功能: 将格式化数据写入流式文件中
用法: int fprintf(FILE *fp, char *format, 输出列表)
数据块读写函数
函数名: fread
功能: 从文件中读出数据
用法: fread(buffer, size, count, fp);
函数名: fwrite
功能: 将数据写入文件
用法: fwrite(buffer, size, count, fp);
参数说明:
buffer
: 是一个指针。对fread
来说,它是读入数据的存放地址;对fwrite
来说,是要输出数据的地址(均指起始地址)。size
: 要读写的字节数。count
: 要进行读写多少个size
字节的数据项。fp
: 文件型指针。