C语言文件操作函数大全

合集下载

C语言中的输入输出函数总结.doc

C语言中的输入输出函数总结.doc

C语言中的输入输出函数总结.docC语言中的输入输出函数是程序中非常重要的部分,主要负责对数据的输入和输出进行处理。

在输入时,我们需要将外部数据读取到程序中进行内部处理,而输出则需要将程序内部处理过的结果输出到外部。

1. scanf函数:用于从标准输入设备(键盘)中读取输入数据并格式化。

2. printf函数:用于将输出数据按照设置的格式输出到标准输出设备(屏幕)上。

5. puts函数:用于将一个字符串输出到标准输出设备上,并自动写入换行符。

8. fgets函数:用于从指定文件中读取一行字符串。

这些函数都需要包含stdio.h头文件才能使用。

下面我们对这些函数进行简要介绍:1. scanf函数scanf函数的语法为:scanf(“格式字符串”,变量名1,变量名2,……);其中格式字符串用来指定输入的数据类型和格式,变量名用来接收输入的数据。

例如,要输入一个整数可以使用如下代码:int num;scanf("%d",&num);2. printf函数3. getchar函数getchar函数的语法为:getchar(),用于从标准输入设备中读取一个字符。

例如,要读取一个字符并输出可以使用如下代码:char c = getchar();printf("c=%c",c);5. puts函数char str[] = "hello world";puts(str);fputs函数的语法为:fputs(字符串,文件指针),用于将一个字符串输出到指定文件中。

FILE *fp;char str[]="hello world";fp=fopen("test.txt","w");fputs(str,fp);fclose(fp);总结:C语言中的输入输出函数可以处理各种数据类型,方便快捷地进行输入输出操作。

C语言函数大全-c开头-完整版

C语言函数大全-c开头-完整版

C语言函数大全(c开头)函数名: cabs功能: 计算复数的绝对值用法: double cabs(struct complex z);程序例:#include#includeint 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#includeint 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 %s\n", str);/* free memory */free(str);return 0;}函数名: ceil功能: 向上舍入用法: double ceil(double x);程序例:#include#includeint main(void){double number = 123.54;double down, up;down = floor(number);up = ceil(number);printf("original number %5.2lf\n", number);printf("number rounded down %5.2lf\n", down);printf("number rounded up %5.2lf\n", up);return 0;}函数名: cgets功能: 从控制台读字符串用法: char *cgets(char *str);程序例:#include#includeint main(void){char buffer[83];char *p;/* There's space for 80 characters plus the NULL terminator */ buffer[0] = 81;printf("Input some chars:");p = cgets(buffer);printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);/* Leave room for 5 characters plus the NULL terminator */buffer[0] = 6;printf("Input some chars:");p = cgets(buffer);printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); return 0;}函数名: chdir功能: 改变工作目录用法: int chdir(const char *path);程序例:#include#include#includechar old_dir[MAXDIR];char new_dir[MAXDIR];int main(void){if (getcurdir(0, old_dir)){perror("getcurdir()");exit(1);}printf("Current directory is: \\%s\n", old_dir);if (chdir("\\")){perror("chdir()");exit(1);}if (getcurdir(0, new_dir)){perror("getcurdir()");exit(1);}printf("Current directory is now: \\%s\n", new_dir);printf("\nChanging back to orignal directory: \\%s\n", old_dir);if (chdir(old_dir)){perror("chdir()");exit(1);}return 0;}函数名: _chmod, chmod功能: 改变文件的访问方式用法: int chmod(const char *filename, int permiss); 程序例:#include#include#includevoid 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-only\n", filename); elseprintf("Made %s read-only\n", filename);}函数名: chsize功能: 改变文件大小用法: int chsize(int handle, long size);程序例:#include#include#includeint main(void){int handle;char buf[11] = "0123456789";/* create text file containing 10 bytes */handle = open("DUMMY.FIL", O_CREAT);write(handle, buf, strlen(buf));/* truncate the file to 5 bytes in size */chsize(handle, 5);/* close the file */close(handle);return 0;}函数名: circle功能: 在给定半径以(x, y)为圆心画圆用法: void far circle(int x, int y, int radius);程序例:#include#include#include#includeint main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;int midx, midy;int radius = 100;/* 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 */}midx = getmaxx() / 2;midy = getmaxy() / 2;setcolor(getmaxcolor());circle(midx, midy, radius);/* clean up */getch();closegraph();return 0;}函数名: cleardevice功能: 清除图形屏幕用法: void far cleardevice(void);程序例:#include#include#include#includeint main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;int midx, midy;/* 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 */}midx = getmaxx() / 2;midy = getmaxy() / 2;setcolor(getmaxcolor());/* for centering screen messages */settextjustify(CENTER_TEXT, CENTER_TEXT);/* output a message to the screen */outtextxy(midx, midy, "press any key to clear the screen:"); /* wait for a key */getch();cleardevice();/* output another message */outtextxy(midx, midy, "press any key to quit:");/* clean up */getch();closegraph();return 0;}函数名: clearerr功能: 复位错误标志用法:void clearerr(FILE *stream);程序例:#includeint 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("%c\n",ch);if (ferror(fp)){/* display an error message */printf("Error reading from DUMMY.FIL\n");/* reset the error and EOF indicators */clearerr(fp);}fclose(fp);return 0;}函数名: clearviewport功能: 清除图形视区用法: void far clearviewport(void);程序例:#include#include#include#include#define CLIP_ON 1 /* activates clipping in viewport */int main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;int ht;/* 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 */}setcolor(getmaxcolor());ht = textheight("W");/* message in default full-screen viewport */outtextxy(0, 0, "* <-- (0, 0) in default viewport");/* create a smaller viewport */setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); /* display some messages */outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); outtextxy(0, 2*ht, "Press any key to clear viewport:");/* wait for a key */getch();/* clear the viewport */clearviewport();/* output another message */outtextxy(0, 0, "Press any key to quit:");/* clean up */getch();closegraph();return 0;}函数名: _close, close功能: 关闭文件句柄用法: int close(int handle);程序例:#include#include#include#includemain(){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 file\n");}return 0;}函数名: clock功能: 确定处理器时间用法: clock_t clock(void);QQ291911320程序例:#include#include#includeint main(void){clock_t start, end;start = clock();delay(2000);end = clock();printf("The time was: %f\n", (end - start) / CLK_TCK); return 0;}函数名: closegraph功能: 关闭图形系统用法: void far closegraph(void);程序例:#include#include#include#includeint main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;int x, y;/* initialize graphics mode */initgraph(&gdriver, &gmode, "");/* read result of initialization */errorcode = graphresult();if (errorcode != grOk) /* an erroroccurred */{printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");getch();exit(1); /* terminate with an error code */}x = getmaxx() / 2;y = getmaxy() / 2;/* output a message */settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(x, y, "Press a key to close the graphics system:"); /* wait for a key */getch();/* closes down the graphics system */closegraph();printf("We're now back in text mode.\n");printf("Press any key to halt:");getch();return 0;}函数名: clreol功能: 在文本窗口中清除字符到行末用法: void clreol(void);程序例:#includeint main(void){clrscr();cprintf("The function CLREOL clears all characters from the\r\n"); cprintf("cursor position to the end of the line within the\r\n"); cprintf("current text window, without moving the cursor.\r\n"); cprintf("Press any key to continue . . .");gotoxy(14, 4);getch();clreol();getch();return 0;}函数名: clrscr功能: 清除文本模式窗口用法: void clrscr(void);程序例:#includeint main(void){int i;clrscr();for (i = 0; i < 20; i++)cprintf("%d\r\n", i);cprintf("\r\nPress any key to clear screen");getch();clrscr();cprintf("The screen has been cleared!");getch();return 0;}函数名: coreleft功能: 返回未使用内存的大小用法: unsigned coreleft(void);程序例:#include#includeint main(void){printf("The difference between the highest allocated block and\n"); printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); return 0;}函数名: cos功能: 余弦函数用法: double cos(double x);程序例:#include#includeint main(void){double result;double x = 0.5;result = cos(x);printf("The cosine of %lf is %lf\n", x, result);return 0;}函数名: cosh功能: 双曲余弦函数用法: dluble cosh(double x);程序例:#include#includeint main(void){double result;double x = 0.5;result = cosh(x);printf("The hyperboic cosine of %lf is %lf\n", x, result);return 0;}函数名: country功能: 返回与国家有关的信息用法: struct COUNTRY *country(int countrycode, struct country *country); 程序例:#include#include#define USA 0int main(void){struct COUNTRY country_info;country(USA, &country_info);printf("The currency symbol for the USA is: %s\n",country_info.co_curr);return 0;}函数名: cprintf功能: 送格式化输出至屏幕用法: int cprintf(const char *format[, argument, ...]);程序例:#includeint main(void){/* clear the screen */clrscr();/* create a text window */window(10, 10, 80, 25);/* output some text in the window */cprintf("Hello world\r\n");/* wait for a key */getch();return 0;}函数名: cputs功能: 写字符到屏幕用法: void cputs(const char *string);程序例:#includeint main(void){/* clear the screen */clrscr();/* create a text window */window(10, 10, 80, 25);/* output some text in the window */cputs("This is within the window\r\n");/* wait for a key */getch();return 0;}函数名: _creat creat功能: 创建一个新文件或重写一个已存在的文件用法: int creat (const char *filename, int permiss); 程序例:#include#include#include#includeint 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;}函数名: creatnew功能: 创建一个新文件用法: int creatnew(const char *filename, int attrib);程序例:#include#include#include#include#includeint main(void){int handle;char buf[11] = "0123456789";/* attempt to create a file that doesn't already exist */ handle = creatnew("DUMMY.FIL", 0);if (handle == -1)printf("DUMMY.FIL already exists.\n");else{printf("DUMMY.FIL successfully created.\n");write(handle, buf, strlen(buf));close(handle);}return 0;}函数名: creattemp功能: 创建一个新文件或重写一个已存在的文件用法: int creattemp(const char *filename, int attrib);程序例:#include#include#includeint main(void){int handle;char pathname[128];strcpy(pathname, "\\");/* create a unique file in the root directory */handle = creattemp(pathname, 0);printf("%s was the unique file created.\n", pathname); close(handle);return 0;}函数名: cscanf功能: 从控制台执行格式化输入用法: int cscanf(char *format[,argument, ...]);程序例:#includeint main(void){char string[80];/* clear the screen */clrscr();/* Prompt the user for input */cprintf("Enter a string with no spaces:");/* read the input */cscanf("%s", string);/* display what was read */cprintf("\r\nThe string entered is: %s", string);return 0;}函数名: ctime功能: 把日期和时间转换为字符串用法: char *ctime(const time_t *time);程序例:#include#includeint main(void){time_t t;time(&t);printf("Today's date and time: %s\n", ctime(&t)); return 0;}函数名: ctrlbrk功能: 设置Ctrl-Break处理程序用法: void ctrlbrk(*fptr)(void);程序例:#include#include#define ABORT 0int c_break(void){printf("Control-Break pressed. Program aborting ...\n"); return (ABORT);}int main(void){ctrlbrk(c_break);for(;;){printf("Looping... Press to quit:\n");}return 0;}。

C语言操作函数ftell

C语言操作函数ftell

ftell目录函数简介调用示例程序示例展开函数简介调用示例程序示例展开编辑本段函数简介函数名: ftell头文件: <stdio.h>功能: 返回当前文件位置,也就是说返回FILE指针当前位置。

函数原型: long ftell(FILE *stream);函数功能:函数 ftell() 用于得到文件位置指针当前位置相对于文件首的偏移字节数。

在随机方式存取文件时,由于文件位置频繁的前后移动,程序不容易确定文件的当前位置。

使用fseek函数后再调用函数ftell()就能非常容易地确定文件的当前位置。

调用示例ftell(fp);利用函数 ftell() 也能方便地知道一个文件的长。

如以下语句序列:fseek(fp, 0L,SEEK_END); len =ftell(fp)+1; 首先将文件的当前位置移到文件的末尾,然后调用函数ftell()获得当前位置相对于文件首的位移,该位移值等于文件所含字节数。

程序示例举例1:#include <stdio.h>int main(void){FILE *stream;stream = fopen("MYFILE.TXT", "w+");fprintf(stream, "This is a test");printf("The file pointer is at byte \%ld\n", ftell(stream));fclose(stream);return 0;}举例2:ftell一般用于读取文件的长度,下面补充一个例子,读取文本文件中的内容:#include <stdio.h>#include <stdlib.h>int main(){FILE *fp;int flen;char *p;/* 以只读方式打开文件 */if((fp = fopen ("1.txt","r"))==NULL){printf("\nfile open error\n");exit(0);}fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */flen=ftell(fp); /* 得到文件大小 */p=(char *)malloc(flen+1); /* 根据文件大小动态分配内存空间 */ if(p==NULL){fclose(fp);return 0;}fseek(fp,0L,SEEK_SET); /* 定位到文件开头 */fread(p,flen,1,fp); /* 一次性读取全部文件内容 */p[flen]=0; /* 字符串结束标志 */printf("%s",p);fclose(fp);free(p);return 0;}程序改进#include <stdio.h>main(){FILE *myf;long f1;myf=fopen("1.txt","rb");fseek(myf,0,SEEK_END);f1=ftell(myf);fclose(myf);printf(“%d\n”,f1);}。

visual studio 文件操作函数

visual studio 文件操作函数

visual studio 文件操作函数Visual Studio是一款功能强大的集成开发环境(Integrated Development Environment,简称IDE),广泛用于开发各种类型的应用程序。

在Visual Studio中,文件操作是开发过程中非常常见和重要的一部分。

本文将介绍一些常用的Visual Studio文件操作函数,帮助开发者更加高效地处理文件相关操作。

一、文件创建与打开1. `CreateFile`函数:用于创建或打开一个文件。

该函数可以指定文件名、访问权限、共享模式等参数,并返回一个表示文件句柄的值。

2. `fopen`函数:用于打开一个文件,并返回一个指向该文件的指针。

通过该指针,可以进行文件读写操作。

二、文件读写操作1. `ReadFile`函数:用于从文件中读取数据。

该函数可以指定文件句柄、读取的字节数、读取数据的缓冲区等参数,并返回读取的字节数和读取结果。

2. `WriteFile`函数:用于向文件中写入数据。

该函数可以指定文件句柄、要写入的数据、写入的字节数等参数,并返回写入的字节数和写入结果。

3. `fgets`函数:用于从文件中读取一行数据。

该函数可以指定要读取的字节数、读取数据的缓冲区和文件指针,并返回指向读取数据的指针。

三、文件定位与指针操作1. `fseek`函数:用于定位文件指针的位置。

该函数可以指定文件指针、偏移量和起始位置,并返回定位结果。

2. `ftell`函数:用于获取文件指针的当前位置。

该函数可以指定文件指针,并返回当前位置的偏移量。

3. `rewind`函数:用于将文件指针重新定位到文件的起始位置。

该函数可以指定文件指针,并无返回值。

四、文件关闭与删除1. `fclose`函数:用于关闭一个已打开的文件。

该函数可以指定要关闭的文件指针,并返回关闭结果。

2. `remove`函数:用于删除一个文件。

该函数可以指定要删除的文件名,并返回删除结果。

C语言函数大全

C语言函数大全

函数名:abort欧阳学文功能: 异常终止一个进程用法: void abort(void)函数名: abs功能: 求整数的绝对值用法: int abs(int i)函数名: absread, abswirte功能: 绝对磁盘扇区读、写数据用法: int absread(int drive, int nsects, int sectno, void *buffer) int abswrite(int drive, int nsects, in tsectno, void *buffer函数名: access功能: 确定文件的访问权限用法: int access(const char *filename, int amode)函数名: acos功能:反余弦函数用法: double acos(double x)函数名: allocmem功能: 分配DOS存储段用法:int allocmem(unsigned size, unsigned *seg)函数名: arc功能: 画一弧线用法:void far arc(int x, int y, int stangle, int endangle, int radius)函数名: asctime功能: 转换日期和时间为ASCII码用法:char *asctime(const struct tm *tblock)函数名: asin功能:反正弦函数用法: double asin(double x)函数名: assert功能: 测试一个条件并可能使程序终止用法:void assert(int test)函数名: atan功能: 反正切函数用法: double atan(double x)函数名: atan2功能: 计算Y/X的反正切值用法: double atan2(double y, double x)函数名:atexit功能: 注册终止函数用法: int atexit(atexit_t func)函数名: atof功能: 把字符串转换成浮点数用法:double atof(const char *nptr)函数名: atoi功能: 把字符串转换成长整型数用法: int atoi(const char *nptr)函数名: atol功能: 把字符串转换成长整型数用法: long atol(const char *nptr)函数名: bar功能: 画一个二维条形图用法: void far bar(int left, int top, int right, int bottom)函数名: bar3d功能: 画一个三维条形图用法:void far bar3d(int left, int top, int right, int bottom,int depth, int topflag)函数名: bdos功能: DOS系统调用用法: int bdos(int dosfun, unsigned dosdx, unsigned dosal)函数名:bdosptr功能:DOS系统调用用法: int bdosptr(int dosfun, void *argument, unsigned dosal)函数名:bioscom功能: 串行I/O通信用法:int bioscom(int cmd, char abyte, int port)函数名:biosdisk功能: 软硬盘I/O用法:int biosdisk(int cmd, int drive, int head, int track, int sector int nsects, void *buffer)函数名:biosequip功能: 检查设备用法:int biosequip(void)函数名:bioskey功能: 直接使用BIOS服务的键盘接口用法:int bioskey(int cmd)函数名:biosmemory功能: 返回存储块大小用法:int biosmemory(void)函数名:biosprint功能: 直接使用BIOS服务的打印机I/O 用法:int biosprint(int cmd, int byte, int port)函数名:biostime功能: 读取或设置BIOS时间用法: long biostime(int cmd, long newtime)函数名: brk功能: 改变数据段空间分配用法:int brk(void *endds)函数名:bsearch功能: 二分法搜索用法:void *bsearch(const void *key, const void *base, size_t *nelem,size_t width, int(*fcmp)(const void *, const *))函数名: cabs功能: 计算复数的绝对值用法: double cabs(struct complex z);函数名:calloc功能:分配主存储器用法:void *calloc(size_t nelem, size_t elsize);函数名: ceil功能: 向上舍入用法: double ceil(double x);函数名: cgets功能: 从控制台读字符串用法: char *cgets(char *str)函数名:chdir功能: 改变工作目录用法: int chdir(const char *path);函数名:_chmod, chmod功能: 改变文件的访问方式用法: int chmod(const char *filename, int permiss);函数名:chsize功能: 改变文件大小用法: int chsize(int handle, long size);函数名: circle功能: 在给定半径以(x, y)为圆心画圆用法: void far circle(int x, int y, int radius);函数名: cleardevice功能: 清除图形屏幕用法: void far cleardevice(void);函数名:clearerr功能: 复位错误标志用法:void clearerr(FILE *stream);函数名: clearviewport功能: 清除图形视区用法: void far clearviewport(void);函数名:_close, close功能: 关闭文件句柄用法:int close(int handle);函数名: clock功能:确定处理器时间用法: clock_t clock(void);函数名:closegraph功能: 关闭图形系统用法: void far closegraph(void);函数名:clreol功能: 在文本窗口中清除字符到行末用法:void clreol(void)函数名:clrscr功能: 清除文本模式窗口用法:void clrscr(void);函数名: coreleft功能: 返回未使用内存的大小用法:unsigned coreleft(void);函数名: cos功能: 余弦函数用法:double cos(double x);函数名:cosh功能: 双曲余弦函数用法: dluble cosh(double x);函数名: country功能: 返回与国家有关的信息用法: struct COUNTRY *country(int countrycode, struct country *country);函数名: cprintf功能: 送格式化输出至屏幕用法:int cprintf(const char *format[, argument, ...]);函数名: cputs功能: 写字符到屏幕用法: void cputs(const char *string);函数名: _creat creat功能: 创建一个新文件或重写一个已存在的文件用法: int creat (const char *filename, int permiss)函数名:creatnew功能: 创建一个新文件用法:int creatnew(const char *filename, int attrib);函数名: cscanf功能: 从控制台执行格式化输入用法:int cscanf(char *format[,argument, ...]);函数名: ctime功能: 把日期和时间转换为字符串用法:char *ctime(const time_t *time);函数名: ctrlbrk功能: 设置CtrlBreak处理程序用法: void ctrlbrk(*fptr)(void);函数名: delay功能: 将程序的执行暂停一段时间(毫秒)用法: void delay(unsigned milliseconds);函数名: delline功能: 在文本窗口中删去一行用法: void delline(void);函数名:detectgraph功能: 通过检测硬件确定图形驱动程序和模式用法: void far detectgraph(int far *graphdriver, int far *graphmode);函数名: difftime功能: 计算两个时刻之间的时间差用法: double difftime(time_t time2, time_t time1); 函数名: disable功能: 屏蔽中断用法:void disable(void);函数名: div功能: 将两个整数相除, 返回商和余数用法:div_t (int number, int denom);函数名: dosexterr功能: 获取扩展DOS错误信息用法:int dosexterr(struct DOSERR *dblkp);函数名: dostounix功能: 转换日期和时间为UNIX时间格式用法: long dostounix(struct date *dateptr, struct time *timeptr); 函数名: drawpoly功能: 画多边形用法: void far drawpoly(int numpoints, int far *polypoints);函数名:dup功能: 复制一个文件句柄用法: int dup(int handle);函数名:dup2功能: 复制文件句柄用法: int dup2(int oldhandle, int newhandle);函数名: ecvt功能: 把一个浮点数转换为字符串用法: char ecvt(double value, int ndigit, int *decpt, int *sign);函数名: ellipse功能: 画一椭圆用法:void far ellipse(int x, int y, int stangle, int endangle,int xradius, int yradius);函数名: enable功能: 开放硬件中断用法: void enable(void);函数名: eof功能: 检测文件结束用法: int eof(int *handle);函数名: 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[]);函数名:exit功能: 终止程序用法: void exit(int status);函数名: exp功能: 指数函数用法: double exp(double x);函数名: gcvt功能: 把浮点数转换成字符串用法: char *gcvt(double value, int ndigit, char *buf);函数名: geninterrupt功能: 产生一个软中断用法: void geninterrupt(int intr_num);函数名: getarccoords功能: 取得最后一次调用arc的坐标用法: void far getarccoords(struct arccoordstype far *arccoords); 函数名: getaspectratio功能: 返回当前图形模式的纵横比用法: void far getaspectratio(int far *xasp, int far *yasp); 函数名: getbkcolor功能: 返回当前背景颜色用法: int far getbkcolor(void);函数名: getc功能: 从流中取字符用法: int getc(FILE *stream);函数名: getcbrk功能: 获取Control_break设置用法: int getcbrk(void);函数名: getch功能: 从控制台无回显地取一个字符用法: int getch(void);函数名: getchar功能: 从stdin流中读字符用法: int getchar(void);函数名: getche功能: 从控制台取字符(带回显)用法: int getche(void);函数名: getcolor功能: 返回当前画线颜色用法: int far getcolor(void);函数名: getcurdir功能: 取指定驱动器的当前目录用法: int getcurdir(int drive, char *direc);函数名: getcwd功能: 取当前工作目录用法: char *getcwd(char *buf, int n);函数名: getdate功能: 取DOS日期用法: void getdate(struct *dateblk);函数名: getdefaultpalette功能: 返回调色板定义结构用法: struct palettetype *far getdefaultpalette(void); 函数名: getdisk功能: 取当前磁盘驱动器号用法: int getdisk(void);函数名: getdrivername功能: 返回指向包含当前图形驱动程序名字的字符串指针用法: char *getdrivename(void);函数名: getdta功能: 取磁盘传输地址用法: char far *getdta(void);函数名: getenv功能: 从环境中取字符串用法: char *getenv(char *envvar);函数名: getfat, getfatd功能: 取文件分配表信息用法: void getfat(int drive, struct fatinfo *fatblkp);函数名: getfillpattern功能: 将用户定义的填充模式拷贝到内存中用法: void far getfillpattern(char far *upattern);函数名: getfillsettings功能: 取得有关当前填充模式和填充颜色的信息用法: void far getfillsettings(struct fillsettingstype far *fillinfo); 函数名: getftime功能: 取文件日期和时间用法: int getftime(int handle, struct ftime *ftimep);函数名: getgraphmode功能: 返回当前图形模式用法: int far getgraphmode(void);函数名: getftime功能: 取文件日期和时间用法: int getftime(int handle, struct ftime *ftimep);函数名: getgraphmode功能: 返回当前图形模式用法: int far getgraphmode(void);函数名: getimage功能: 将指定区域的一个位图存到主存中用法: void far getimage(int left, int top, int right, int bottom, void far *bitmap);函数名: getlinesettings功能: 取当前线型、模式和宽度用法: void far getlinesettings(struct linesettingstype far *lininfo): 函数名: getmaxx功能: 返回屏幕的最大x坐标用法: int far getmaxx(void);函数名: getmaxy功能: 返回屏幕的最大y坐标用法: int far getmaxy(void);函数名: getmodename功能: 返回含有指定图形模式名的字符串指针用法: char *far getmodename(int mode_name);函数名: getmoderange功能: 取给定图形驱动程序的模式范围用法: void far getmoderange(int graphdriver, int far *lomode, int far *himode);函数名: getpalette功能: 返回有关当前调色板的信息用法: void far getpalette(struct palettetype far *palette);函数名: getpass功能: 读一个口令用法: char *getpass(char *prompt);函数名: getpixel功能: 取得指定像素的颜色用法: int far getpixel(int x, int y);函数名: gets功能: 从流中取一字符串用法: char *gets(char *string);函数名: gettext功能: 将文本方式屏幕上的文本拷贝到存储区用法: int gettext(int left, int top, int right, int bottom, void *destin);函数名: gettextinfo功能: 取得文本模式的显示信息用法: void gettextinfo(struct text_info *inforec);函数名: gettextsettings功能: 返回有关当前图形文本字体的信息用法: void far gettextsettings(struct textsettingstype far *textinfo); 函数名: gettime功能: 取得系统时间用法: void gettime(struct time *timep);函数名: getvect功能: 取得中断向量入口用法: void interrupt(*getvect(int intr_num));函数名: getverify功能: 返回DOS校验标志状态用法: int getverify(void);函数名: getviewsetting功能: 返回有关当前视区的信息用法: void far getviewsettings(struct viewporttype far *viewport); 函数名: getw功能: 从流中取一整数用法: int getw(FILE *strem);函数名: getx功能: 返回当前图形位置的x坐标用法: int far getx(void);函数名: gety功能: 返回当前图形位置的y坐标用法: int far gety(void);函数名: gmtime功能: 把日期和时间转换为格林尼治标准时间(GMT) 用法: struct tm *gmtime(long *clock);函数名: gotoxy功能: 在文本窗口中设置光标用法: void gotoxy(int x, int y);函数名: gotoxy功能: 在文本窗口中设置光标用法: void gotoxy(int x, int y);函数名: graphdefaults功能: 将所有图形设置复位为它们的缺省值用法: void far graphdefaults(void);函数名: grapherrormsg功能: 返回一个错误信息串的指针用法: char *far grapherrormsg(int errorcode);函数名: graphresult功能: 返回最后一次不成功的图形操作的错误代码用法: int far graphresult(void);函数名: _graphfreemem功能: 用户可修改的图形存储区释放函数用法: void far _graphfreemem(void far *ptr, unsigned size); 函数名: _graphgetmem功能: 用户可修改的图形存储区分配函数用法: void far *far _graphgetmem(unsigned size);函数名: harderr功能: 建立一个硬件错误处理程序用法: void harderr(int (*fptr)());函数名: hardresume功能: 硬件错误处理函数用法: void hardresume(int rescode);函数名: highvideo功能: 选择高亮度文本字符用法: void highvideo(void);函数名: hypot功能: 计算直角三角形的斜边长用法: double hypot(double x, double y);函数名: imagesize功能: 返回保存位图像所需的字节数用法: unsigned far imagesize(int left, int top, int right, int bottom); 函数名: initgraph功能: 初始化图形系统用法: void far initgraph(int far *graphdriver, int far *graphmode 函数名: inport功能: 从硬件端口中输入用法: int inp(int protid);函数名: insline功能: 在文本窗口中插入一个空行用法: void insline(void);函数名: installuserdriver功能: 安装设备驱动程序到BGI设备驱动程序表中用法: int far installuserdriver(char far *name, int (*detect)(void)); 函数名: installuserfont功能: 安装未嵌入BGI系统的字体文件(CHR)用法: int far installuserfont(char far *name);函数名: int86功能: 通用8086软中断接口用法: int int86(int intr_num, union REGS *inregs, union REGS *outregs)函数名: int86x功能: 通用8086软中断接口用法: int int86x(int intr_num, union REGS *insegs, union REGS *outregs,函数名: intdos功能: 通用DOS接口用法: int intdos(union REGS *inregs, union REGS *outregs);函数名: intdosx功能: 通用DOS中断接口用法: int intdosx(union REGS *inregs, union REGS *outregs,struct SREGS *segregs);函数名: intr功能: 改变软中断接口用法: void intr(int intr_num, struct REGPACK *preg); 函数名: ioctl功能: 控制I/O设备用法: int ioctl(int handle, int cmd[,int *argdx, int argcx]); 函数名: isatty功能: 检查设备类型用法: int isatty(int handle);函数名: itoa功能: 把一整数转换为字符串用法: char *itoa(int value, char *string, int radix);函数名: kbhit功能: 检查当前按下的键用法: int kbhit(void);函数名: keep功能: 退出并继续驻留用法: void keep(int status, int size);函数名: kbhit功能: 检查当前按下的键用法: int kbhit(void);函数名: keep功能: 退出并继续驻留用法: void keep(int status, int size);函数名: labs用法: long labs(long n);函数名: ldexp功能: 计算value*2的幂用法: double ldexp(double value, int exp);函数名: ldiv功能: 两个长整型数相除, 返回商和余数用法: ldiv_t ldiv(long lnumer, long ldenom);函数名: lfind功能: 执行线性搜索用法: void *lfind(void *key, void *base, int *nelem, int width,int (*fcmp)());函数名: line功能: 在指定两点间画一直线用法: void far line(int x0, int y0, int x1, int y1);函数名: linerel功能: 从当前位置点(CP)到与CP有一给定相对距离的点画一直线用法: void far linerel(int dx, int dy);函数名: localtime功能: 把日期和时间转变为结构用法: struct tm *localtime(long *clock);函数名: lock功能: 设置文件共享锁用法: int lock(int handle, long offset, long length);函数名: log功能: 对数函数ln(x)用法: double log(double x);函数名: log10功能: 对数函数log用法: double log10(double x);函数名: longjump功能: 执行非局部转移用法: void longjump(jmp_buf env, int val);函数名: lowvideo功能: 选择低亮度字符用法: void lowvideo(void);函数名: lrotl, _lrotl功能: 将无符号长整型数向左循环移位用法: unsigned long lrotl(unsigned long lvalue, int count); unsigned long _lrotl(unsigned long lvalue, int count);函数名: lsearch功能: 线性搜索用法: void *lsearch(const void *key, void *base, size_t *nelem, size_t width, int (*fcmp)(const void *, const void *));函数名: lseek功能: 移动文件读/写指针用法: long lseek(int handle, long offset, int fromwhere);main()主函数每一C 程序都必须有一 main() 函数, 可以根据自己的爱好把它放在程序的某个地方。

C语言~所有的字符串操作函数

C语言~所有的字符串操作函数
strcpy(string, str1); printf("%s\n", string); return 0; }
7.函数名: strcspn 功 能: 在串中查找第一个给定字符集内容的段 用 法: int strcspn(char *str1, char *str2); 程序例: #i nclude <stdio.h> #i nclude <string.h> #i nclude <alloc.h> int main(void) { char *string1 = "1234567890"; char *string2 = "747DC8"; int length;
printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)
printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)
printf("buffer 2 equals buffer 1\n"); return 0; }
printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)
printf("buffer 2 equals buffer 1\n"); return 0; }
14.函数名: strncpy 功 能: 串拷贝 用 法: char *strncpy(char *destin, char *source, int maxlen); 程序例: #i nclude <stdio.h> #i nclude <string.h> int main(void) {

c 标准库函数手册

c 标准库函数手册C 标准库函数手册。

C 标准库函数是 C 语言提供的一组函数库,它包含了一系列常用的函数,可以帮助程序员更高效地完成各种任务。

本手册将介绍 C 标准库函数的常用函数及其用法,帮助读者更加深入地理解和运用这些函数。

一、stdio.h。

stdio.h 是 C 语言中用来进行输入输出操作的头文件,它包含了一系列与标准输入输出相关的函数。

其中,最常用的函数包括 printf、scanf、fopen、fclose 等。

这些函数可以帮助程序员进行屏幕输出、键盘输入、文件读写等操作。

例如,printf 函数可以用来向屏幕输出格式化的字符串,而 scanf 函数则可以用来从键盘接收输入并存储到变量中。

二、stdlib.h。

stdlib.h 是 C 语言中的标准库头文件之一,它包含了一系列与内存分配、随机数生成、字符串转换等功能相关的函数。

其中,最常用的函数包括 malloc、free、rand、atoi 等。

这些函数可以帮助程序员进行动态内存分配、随机数生成、字符串转换等操作。

例如,malloc 函数可以用来动态分配指定大小的内存空间,而 rand 函数则可以用来生成一个指定范围内的随机数。

三、math.h。

math.h 是 C 语言中的标准数学库头文件,它包含了一系列与数学运算相关的函数。

其中,最常用的函数包括 sin、cos、sqrt、pow 等。

这些函数可以帮助程序员进行各种数学运算,如三角函数计算、平方根计算、幂运算等。

例如,sin 函数可以用来计算给定角度的正弦值,而 sqrt 函数则可以用来计算一个数的平方根。

四、string.h。

string.h 是 C 语言中的标准字符串库头文件,它包含了一系列与字符串操作相关的函数。

其中,最常用的函数包括 strlen、strcpy、strcat、strcmp 等。

这些函数可以帮助程序员进行字符串的长度计算、复制、连接、比较等操作。

例如,strlen 函数可以用来计算一个字符串的长度,而 strcpy 函数则可以用来将一个字符串复制到另一个字符串中。

c语言中字符串操作函数及功能

c语言中字符串操作函数及功能C语言中字符串操作函数及功能在C语言中,字符串是一组由字符组成的数据,用于存储和操作文本信息。

C语言提供了许多字符串操作函数,以便开发者能够方便地处理字符串。

本文将介绍一些常用的字符串操作函数及其功能。

1. strlen函数:用于获取字符串的长度。

它接受一个字符串作为参数,并返回该字符串的长度,即字符的个数。

例如,strlen("hello")将返回5。

2. strcpy函数:用于将一个字符串复制到另一个字符串中。

它接受两个参数,第一个参数是目标字符串,第二个参数是源字符串。

例如,strcpy(dest, src)将源字符串src复制到目标字符串dest中。

3. strcat函数:用于将一个字符串连接到另一个字符串的末尾。

它接受两个参数,第一个参数是目标字符串,第二个参数是要追加的字符串。

例如,strcat(dest, src)将字符串src追加到字符串dest的末尾。

4. strcmp函数:用于比较两个字符串。

它接受两个参数,分别是要比较的两个字符串。

如果两个字符串相等,返回0;如果第一个字符串小于第二个字符串,返回负数;如果第一个字符串大于第二个字符串,返回正数。

例如,strcmp("hello", "world")将返回负数。

5. strchr函数:用于在字符串中查找指定字符的第一次出现的位置。

它接受两个参数,第一个参数是要查找的字符串,第二个参数是要查找的字符。

如果找到了指定字符,返回该字符在字符串中的位置;如果未找到,返回NULL。

6. strstr函数:用于在字符串中查找指定子串的第一次出现的位置。

它接受两个参数,第一个参数是要查找的字符串,第二个参数是要查找的子串。

如果找到了指定子串,返回该子串在字符串中的位置;如果未找到,返回NULL。

7. strtok函数:用于将字符串分割为多个子串。

它接受两个参数,第一个参数是要分割的字符串,第二个参数是分割的分隔符。

C语言函数大全-p开头-完整版

C语言函数大全(p开头)函数名: parsfnm功能: 分析文件名用法: char *parsfnm (char *cmdline, struct fcb *fcbptr, int option);程序例:#include#include#include#includeint main(void){char line[80];struct fcb blk;/* get file name */printf("Enter drive and file name (no path - ie. a:file.dat)\n"); gets(line);/* put file name in fcb */if (parsfnm(line, &blk, 1) == NULL)printf("Error in parsfm call\n");elseprintf("Drive #%d Name: %11s\n", blk.fcb_drive, blk.fcb_name); return 0;}函数名: peek功能: 检查存储单元用法: int peek(int segment, unsigned offset);程序例:#include#include#includeint main(void){int value = 0;printf("The current status of your keyboard is:\n");value = peek(0x0040, 0x0017);if (value & 1)printf("Right shift on\n");elseprintf("Right shift off\n");if (value & 2)printf("Left shift on\n");elseprintf("Left shift off\n");if (value & 4)printf("Control key on\n");elseprintf("Control key off\n");if (value & 8)printf("Alt key on\n");elseprintf("Alt key off\n");if (value & 16)printf("Scroll lock on\n");elseprintf("Scroll lock off\n");if (value & 32)printf("Num lock on\n");elseprintf("Num lock off\n");if (value & 64)printf("Caps lock on\n");elseprintf("Caps lock off\n");return 0;}函数名: peekb功能: 检查存储单元用法: char peekb (int segment, unsigned offset);程序例:#include#include#includeint main(void){int value = 0;printf("The current status of your keyboard is:\n"); value = peekb(0x0040, 0x0017);if (value & 1)printf("Right shift on\n");elseprintf("Right shift off\n");if (value & 2)printf("Left shift on\n");elseprintf("Left shift off\n");if (value & 4)printf("Control key on\n");elseprintf("Control key off\n");if (value & 8)printf("Alt key on\n");elseprintf("Alt key off\n");if (value & 16)printf("Scroll lock on\n");elseprintf("Scroll lock off\n");if (value & 32)printf("Num lock on\n");elseprintf("Num lock off\n");if (value & 64)printf("Caps lock on\n");elseprintf("Caps lock off\n");return 0;}函数名: perror功能: 系统错误信息用法: void perror(char *string);程序例:#includeint main(void){FILE *fp;fp = fopen("perror.dat", "r");if (!fp)perror("Unable to open file for reading");return 0;}函数名: pieslice功能: 绘制并填充一个扇形用法: void far pieslice(int x, int stanle, int endangle, int radius);#include#include#include#includeint main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;int midx, midy;int stangle = 45, endangle = 135, radius = 100;/* 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 */}midx = getmaxx() / 2;midy = getmaxy() / 2;/* set fill style and draw a pie slice */setfillstyle(EMPTY_FILL, getmaxcolor());pieslice(midx, midy, stangle, endangle, radius);/* clean up */getch();closegraph();return 0;}函数名: poke功能: 存值到一个给定存储单元用法: void poke(int segment, int offset, int value);程序例:#include#includeint main(void){clrscr();cprintf("Make sure the scroll lock key is off and press any key\r\n");poke(0x0000,0x0417,16);cprintf("The scroll lock is now on\r\n");return 0;}函数名: pokeb功能: 存值到一个给定存储单元用法: void pokeb(int segment, int offset, char value);程序例:#include#includeint main(void){clrscr();cprintf("Make sure the scroll lock key is off and press any key\r\n"); getch();pokeb(0x0000,0x0417,16);cprintf("The scroll lock is now on\r\n");return 0;}函数名: poly功能: 根据参数产生一个多项式用法: double poly(double x, int n, double c[]);程序例:#include#include/* polynomial: x**3 - 2x**2 + 5x - 1 */int main(void){double array[] = { -1.0, 5.0, -2.0, 1.0 };double result;result = poly(2.0, 3, array);printf("The polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 is %lf\n", result);return 0;}函数名: pow功能: 指数函数(x的y次方)用法: double pow(double x, double y);程序例:#includeint main(void){double x = 2.0, y = 3.0;printf("%lf raised to %lf is %lf\n", x, y, pow(x, y)); return 0;}函数名: pow10功能: 指数函数(10的p次方)用法: double pow10(int p);程序例:#include#includeint main(void){double p = 3.0;printf("Ten raised to %lf is %lf\n", p, pow10(p)); return 0;}函数名: printf功能: 产生格式化输出的函数用法: int printf(char *format...);程序例:#include#include#define I 555#define R 5.5int main(void){int i,j,k,l;char buf[7];char *prefix = buf;char tp[20];printf("prefix 6d 6o 8x 10.2e ""10.2f\n");strcpy(prefix,"%");for (i = 0; i < 2; i++){for (j = 0; j < 2; j++)for (k = 0; k < 2; k++)for (l = 0; l < 2; l++){if (i==0) strcat(prefix,"-");if (j==0) strcat(prefix,"+");if (k==0) strcat(prefix,"#");if (l==0) strcat(prefix,"0");printf("%5s |",prefix);strcpy(tp,prefix);strcat(tp,"6d |");printf(tp,I);strcpy(tp,"");strcpy(tp,prefix);strcat(tp,"6o |");printf(tp,I);strcpy(tp,"");strcpy(tp,prefix);strcat(tp,"8x |");printf(tp,I);strcpy(tp,"");strcpy(tp,prefix);strcat(tp,"10.2e |");printf(tp,R);strcpy(tp,prefix);strcat(tp,"10.2f |");printf(tp,R);printf(" \n");strcpy(prefix,"%");}}return 0;}函数名: putc功能: 输出一字符到指定流中用法: int putc(int ch, FILE *stream);程序例:#includeint main(void){char msg[] = "Hello world\n";int i = 0;while (msg[i])putc(msg[i++], stdout);return 0;函数名: putch功能: 输出字符到控制台用法: int putch(int ch);程序例:#include#includeint main(void){char ch = 0;printf("Input a string:");while ((ch != '\r')){ch = getch();putch(ch);}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 0xD9int main(void){char i, j;/* draw the top of the box */putchar(LEFT_TOP);for (i=0; i<10; i++)putchar(HORIZ);putchar(RIGHT_TOP);putchar('\n');/* draw the middle */for (i=0; i<4; i++)putchar(VERT);for (j=0; j<10; j++)putchar(' ');putchar(VERT);putchar('\n');}QQ291911320/* draw the bottom */putchar(LEFT_BOT);for (i=0; i<10; i++)putchar(HORIZ);putchar(RIGHT_BOT);putchar('\n');return 0;}函数名: putenv功能: 把字符串加到当前环境中用法: int putenv(char *envvar);程序例:#include#include#include#include#includeint 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[i])printf("%s\n",environ[i++]);return 0;}函数名: putimage功能: 在屏幕上输出一个位图用法: void far putimage(int x, int y, void far *bitmap, int op);程序例:#include#include#include#include#define ARROW_SIZE 10void draw_arrow(int x, int y);int main(void){/* request autodetection */int gdriver = DETECT, gmode, errorcode;void *arrow;int x, y, maxx;unsigned int size;/* 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 */}maxx = getmaxx();x = 0;y = getmaxy() / 2;/* draw the image to be grabbed */draw_arrow(x, y);/* calculate the size of the image */size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE); /* allocate memory to hold the image */arrow = malloc(size);/* grab the image */getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow); /* repeat until a key is pressed */while (!kbhit()){/* erase old image */putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);x += ARROW_SIZE;if (x >= maxx)x = 0;/* plot new image */putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);}/* clean up */free(arrow);closegraph();return 0;}void draw_arrow(int x, int y){/* draw an arrow on the screen */moveto(x, y);linerel(4*ARROW_SIZE, 0);linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);linerel(0, 2*ARROW_SIZE);linerel(2*ARROW_SIZE, -1*ARROW_SIZE);}函数名: putpixel功能: 在指定位置画一像素用法: void far putpixel (int x, int y, int pixelcolor);程序例:#include#include#include#include#include#define PIXEL_COUNT 1000#define DELAY_TIME 100 /* in milliseconds */int main(void){/* request autodetection */int gdriver = DETECT, gmode, errorcode;int i, x, y, color, maxx, maxy, maxcolor, seed;/* 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 */}maxx = getmaxx() + 1;maxy = getmaxy() + 1;maxcolor = getmaxcolor() + 1;while (!kbhit()){/* seed the random number generator */seed = random(32767);srand(seed);for (i=0; i{x = random(maxx);y = random(maxy);color = random(maxcolor);putpixel(x, y, color);}delay(DELAY_TIME);srand(seed);for (i=0; i{x = random(maxx);y = random(maxy);color = random(maxcolor);if (color == getpixel(x, y))putpixel(x, y, 0);}}/* clean up */getch();closegraph();return 0;}函数名: puts功能: 送一字符串到流中用法: int puts(char *string);程序例:#includeint main(void){char string[] = "This is an example output string\n";puts(string);return 0;}函数名: puttext功能: 将文本从存储区拷贝到屏幕用法: int puttext(int left, int top, int right, int bottom, void *source);程序例:#includeint main(void){char buffer[512];/* put some text to the console */clrscr();gotoxy(20, 12);cprintf("This is a test. Press any key to continue ...");getch();/* grab screen contents */gettext(20, 12, 36, 21,buffer);clrscr();/* put selected characters back to the screen */gotoxy(20, 12);puttext(20, 12, 36, 21, buffer);getch();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 %s\n", FNAME);exit(1);}word = 94;putw(word,fp);if (ferror(fp))printf("Error writing to file\n");elseprintf("Successful write\n");fclose(fp);/* reopen the file */fp = fopen(FNAME, "rb");if (fp == NULL){printf("Error opening file %s\n", FNAME);exit(1);}/* extract the word */word = getw(fp);if (ferror(fp))printf("Error reading file\n");elseprintf("Successful read: word = %d\n", word); /* clean up */fclose(fp);unlink(FNAME);return 0;}。

C语言字符串操作函数

strtok()—字符串分割函数相关函数:index, memchr, rindex, strpbrk, strsep, strspn, strstr头文件:#include <string.h>定义函数:char * strtok(char *s, const char *delim);函数说明:strtok()用来将字符串分割成一个个片段. 参数s 指向欲分割的字符串, 参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符. 在第一次调用时,strtok()必需给予参数s 字符串, 往后的调用则将参数s 设置成NULL. 每次调用成功则返回下一个分割后的字符串指针.返回值:返回下一个分割后的字符串指针, 如果已无从分割则返回NULL.范例#include <string.h>main(){char s[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z";char *delim = "-: ";char *p;printf("%s ", strtok(s, delim));while((p = strtok(NULL, delim)))printf("%s ", p);printf("\n");}执行结果:ab cd ef;gh i jkl;mnop;qrs tu vwx y;z //-与:字符已经被\0 字符取代strstr()—字符串查找函数相关函数:index, memchr, rindex, strchr, strpbrk, strsep, strspn, strtok头文件:#include <string.h>定义函数:char *strstr(const char *haystack, const char * needle);函数说明:strstr()会从字符串haystack 中搜寻字符串needle, 并将第一次出现的地址返回.返回值:返回指定字符串第一次出现的地址, 否则返回0.范例#include <string.h>main(){char * s = "012345678901234567890123456789";char *p;p = strstr(s, "901");printf("%s\n", p);}执行结果:9.01E+21strspn()—字符查找函数相关函数:strcspn, strchr, strpbrk, strsep, strstr头文件:#include <string.h>定义函数:size_t strspn(const char *s, const char * accept);函数说明:strspn()从参数s 字符串的开头计算连续的字符, 而这些字符都完全是accept 所指字符串中的字符.简单的说, 若strspn()返回的数值为n, 则代表字符串s 开头连续有n 个字符都是属于字符串accept 内的字符.返回值:返回字符串s 开头连续包含字符串accept 内的字符数目.范例#include <string.h>main(){char *str = "Linux was first developed for 386/486-based PCs. ";char *t1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";printf("%d\n", strspn(str, t1));}执行结果:5 //计算大小写字母. 不包含" ", 所以返回Linux 的长度.strrchr()—定位字符串中最后出现的指定字符相关函数:index, memchr, rindex, strpbrk, strsep, strspn, strstr, strtok头文件:#include <string.h>定义函数:char * strrchr(const char *s, int c);函数说明:strrchr()用来找出参数s 字符串中最后一个出现的参数c 地址, 然后将该字符出现的地址返回.返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.范例#include <string.h>main(){char *s = "0123456789012345678901234567890";char *p;p = strrchr(s, '5');printf("%s\n", p);}执行结果:567890strpbrk()—定位字符串中第一个出现的指定字符相关函数:index, memchr, rindex, strpbrk, strsep, strspn, strstr, strtok头文件:#include <include.h>定义函数:char *strpbrk(const char *s, const char *accept);函数说明:strpbrk()用来找出参数s 字符串中最先出现存在参数accept 字符串中的任意字符.返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.范例#include <string.h>main(){char *s = "0123456789012345678901234567890";char *p; p = strpbrk(s, "a1 839"); //1 会最先在s 字符串中找到printf("%s\n", p);p = strprk(s, "4398"); //3 会最先在s 字符串中找到printf("%s\n", p);}执行结果:1.23E+29strncat()—字符串连接函数相关函数:bcopy, memccpy, memecpy, strcpy, strncpy头文件:#inclue <string.h>定义函数:char * strncat(char *dest, const char *src, size_t n);函数说明:strncat()会将参数src 字符串拷贝n 个字符到参数dest 所指的字符串尾. 第一个参数dest 要有足够的空间来容纳要拷贝的字符串.返回值:返回参数dest 的字符串起始地址.范例#include <string.h>main(){char a[30] = "string(1)";char b[] = "string(2)";printf("before strncat() :%s\n", a);printf("after strncat() :%s\n", strncat(a, b, 6));}执行结果:before strncat() : string(1)after strncat() : string(1) stringstrncpy()—复制字符串相关函数:bcopy, memccpy, memcpy, memmove头文件:#include <string.h>定义函数:char * strncpy(char *dest, const char *src, size_t n);函数说明:strncpy()会将参数src 字符串拷贝前n 个字符至参数dest 所指的地址.返回值:返回参数dest 的字符串起始地址.范例#inclue <string.h>main(){char a[30] = "string(1)";char b[] = "string(2)";printf("before strncpy() : %s\n", a);printf("after strncpy() : %s\n", strncpy(a, b, 6));}执行结果:before strncpy() : string(1)after strncpy() : string(1)strncasecmp()—字符串比较函数(忽略大小写)相关函数:bcmp, memcmp, strcmp, strcoll, strncmp头文件:#include <string.h>定义函数:int strncasecmp(const char *s1, const char *s2, size_t n);函数说明:strncasecmp()用来比较参数s1 和s2 字符串前n 个字符, 比较时会自动忽略大小写的差异.返回值:若参数s1 和s2 字符串相同则返回0. s1 若大于s2 则返回大于0 的值, s1 若小于s2 则返回小于0 的值.范例#include <string.h>main(){char *a = "aBcDeF";char *b = "AbCdEf";if(!strncasecmp(a, b))printf("%s =%s\n", a, b);}执行结果:aBcDef=AbCdEfstrlen()—字符串长度计算函数相关函数:无头文件:#include <string.h>定义函数:size_t strlen (const char *s);函数说明:strlen()用来计算指定的字符串s 的长度, 不包括结束字符"\0".返回值:返回字符串s 的字符数.范例:/*取得字符串str 的长度*/#include <string.h>main(){char *str = "12345678";printf("str length = %d\n", strlen(str));}执行结果:str length = 8strdup()—复制字符串相关函数:calloc, malloc, realloc, free头文件:#include <string.h>定义函数:char * strdup(const char *s);函数说明:strdup()会先用maolloc()配置与参数s 字符串相同的空间大小, 然后将参数s 字符串的内容复制到该内存地址, 然后把该地址返回. 该地址最后可以利用free()来释放.返回值:返回一字符串指针, 该指针指向复制后的新字符串地址. 若返回NULL 表示内存不足.范例#include <string.h>main(){char a[] = "strdup";char *b;b = strdup(a);printf("b[]=\"%s\"\n", b);}执行结果:b[]="strdup"strcspn()—查找字符串相关函数:strspn头文件:#inclued<string.h>定义函数:size_t strcspn(const char *s, const char * reject);函数说明:strcspn()从参数s 字符串的开头计算连续的字符, 而这些字符都完全不在参数reject 所指的字符串中. 简单地说, 若strcspn()返回的数值为n, 则代表字符串s 开头连续有n 个字符都不含字符串reject 内的字符.返回值:返回字符串s 开头连续不含字符串reject 内的字符数目.范例#include <string.h>main(){char *str = "Linux was first developed for 386/486-based pcs. ";printf("%d\n", strcspn(str, " "));printf("%d\n", strcspn(str, "/-"));printf("%d\n", strcspn(str, "1234567890"));}执行结果:5 //只计算到" "的出现, 所以返回"Linux"的长度33 //计算到出现"/"或"-", 所以返回到"6"的长度30 // 计算到出现数字字符为止, 所以返回"3"出现前的长度strcpy()—复制字符串相关函数:bcopy, memcpy, memccpy, memmove头文件:#include <string.h>定义函数:char *strcpy(char *dest, const char *src);函数说明:strcpy()会将参数src 字符串拷贝至参数dest 所指的地址.返回值:返回参数dest 的字符串起始地址.附加说明:如果参数dest 所指的内存空间不够大, 可能会造成缓冲溢出(buffer Overflow)的错误情况, 在编写程序时请特别留意, 或者用strncpy()来取代.范例#include <string.h>main(){char a[30] = "string(1)";char b[] = "string(2)";printf("before strcpy() :%s\n", a);printf("after strcpy() :%s\n", strcpy(a, b));}执行结果:before strcpy() :string(1)after strcpy() :string(2)strcoll()—字符串比较函数(按字符排列次序)相关函数:strcmp, bcmp, memcmp, strcasecmp, strncasecmp头文件:#include <string.h>定义函数:int strcoll(const char *s1, const char *s2);函数说明:strcoll()会依环境变量LC_COLLATE 所指定的文字排列次序来比较s1 和s2 字符串.返回值:若参数s1 和s2 字符串相同则返回0. s1 若大于s2 则返回大于0 的值. s1 若小于s2 则返回小于0 的值.附加说明:若LC_COLLATE 为"POSIX"或"C", 则strcoll()与strcmp()作用完全相同.范例参考strcmp().strcmp()—字符串比较函数(比较字符串)相关函数:bcmp, memcmp, strcasecmp, strncasecmp, strcoll头文件:#include <string.h>定义函数:int strcmp(const char *s1, const char *s2);函数说明:strcmp()用来比较参数s1 和s2 字符串. 字符串大小的比较是以ASCII 码表上的顺序来决定, 此顺序亦为字符的值. strcmp()首先将s1 第一个字符值减去s2 第一个字符值, 若差值为0 则再继续比较下个字符, 若差值不为0 则将差值返回. 例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33).返回值:若参数s1 和s2 字符串相同则返回0. s1 若大于s2 则返回大于0 的值. s1 若小于s2 则返回小于0 的值.范例#include <string.h>main(){char *a = "aBcDeF";char *b = "AbCdEf";char *c = "aacdef";char *d = "aBcDeF";printf("strcmp(a, b) : %d\n", strcmp(a, b));printf("strcmp(a, c) : %d\n", strcmp(a, c));printf("strcmp(a, d) : %d\n", strcmp(a, d));}执行结果:strcmp(a, b) : 32strcmp(a, c) :-31strcmp(a, d) : 0strchr()—字符串查找函数(返回首次出现字符的位置) 相关函数:index, memchr, rinex, strbrk, strsep, strspn, strstr, strtok头文件:#include <string.h>定义函数:char * strchr (const char *s, int c);函数说明:strchr()用来找出参数s 字符串中第一个出现的参数c 地址, 然后将该字符出现的地址返回.返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.范例#include <string.h>main(){char *s = "0123456789012345678901234567890";char *p;p = strchr(s, '5');printf("%s\n", p);}执行结果:5.68E+25strcat()—连接字符串相关函数:bcopy, memccpy, memcpy, strcpy, strncpy头文件:#include <string.h>定义函数:char *strcat(char *dest, const char *src);函数说明:strcat()会将参数src 字符串拷贝到参数dest 所指的字符串尾. 第一个参数dest 要有足够的空间来容纳要拷贝的字符串.返回值:返回参数dest 的字符串起始地址范例#include <string.h>main(){char a[30] = "string(1)";char b[] = "string(2)";printf("before strcat() : %s\n", a);printf("after strcat() : %s\n", strcat(a, b));}执行结果:before strcat() : string(1)after strcat() : string(1)string(2)strcasecmp()—字符串比较函数(忽略大小写比较字符串) 相关函数:bcmp, memcmp, strcmp, strcoll, strncmp头文件:#include <string.h>定义函数:int strcasecmp (const char *s1, const char *s2);函数说明:strcasecmp()用来比较参数s1 和s2 字符串, 比较时会自动忽略大小写的差异.返回值:若参数s1 和s2 字符串相同则返回0. s1 长度大于s2 长度则返回大于0 的值, s1 长度若小于s2 长度则返回小于0 的值.范例#include <string.h>main(){char *a = "aBcDeF";char *b = "AbCdEf";if(!strcasecmp(a, b))printf("%s=%s\n", a, b);}执行结果:aBcDeF=AbCdEfrindex()—字符串查找函数(返回最后一次出现的位置)相关函数:index, memchr, strchr, strrchr头文件:#include <string.h>定义函数:char * rindex(const char *s, int c);函数说明:rindex()用来找出参数s 字符串中最后一个出现的参数c 地址, 然后将该字符出现的地址返回. 字符串结束字符(NULL)也视为字符串一部分.返回值:如果找到指定的字符则返回该字符所在的地址, 否则返回0.范例#include <string.h>main(){char *s = "0123456789012345678901234567890";char *p;p = rindex(s, '5');printf("%s\n", p);}执行结果:567890index()—字符串查找函数(返回首次出现的位置)相关函数:rindex, srechr, strrchr头文件:#include <string.h>定义函数:char * index(const char *s, int c);函数说明:index()用来找出参数s 字符串中第一个出现的参数c 地址, 然后将该字符出现的地址返回. 字符串结束字符(NULL)也视为字符串一部分.返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.范例#include <string.h>main(){char *s = "0123456789012345678901234567890";char *p;p = index(s, '5');printf("%s\n", p);}执行结果:5.68E+25toupper()—字符串转换函数(小写转大写)相关函数:isalpha, tolower头文件:#include <ctype.h>定义函数:int toupper(int c);函数说明:若参数 c 为小写字母则将该对应的大写字母返回.返回值:返回转换后的大写字母, 若不须转换则将参数c 值返回.范例/* 将s 字符串内的小写字母转换成大写字母*/#include <ctype.h>main(){char s[] = "aBcDeFgH12345;!#$";int i;printf("before toupper() : %s\n", s);for(i = 0; I < sizeof(s); i++)s[i] = toupper(s[i]);printf("after toupper() : %s\n", s);}执行结果:before toupper() : aBcDeFgH12345;!#$after toupper() : ABCDEFGH12345;!#$tolower()—字符串转换函数(大写转小写)作相关函数:isalpha, toupper头文件:#include <stdlib.h>定义函数:int tolower(int c);函数说明:若参数 c 为大写字母则将该对应的小写字母返回.返回值:返回转换后的小写字母, 若不须转换则将参数c 值返回.范例/* 将s 字符串内的大写字母转换成小写字母*/#include <ctype.h>main(){char s[] = "aBcDeFgH12345;!#$";int i;printf("before tolower() : %s\n", s);for(i = 0; I < sizeof(s); i++)s[i] = tolower(s[i]);printf("after tolower() : %s\n", s);}执行结果:before tolower() : aBcDeFgH12345;!#$after tolower() : abcdefgh12345;!#$toascii()—将整数转换成合法的ASCII码字符相关函数:isascii, toupper, tolower头文件:#include <ctype.h>定义函数:int toascii(int c);函数说明:toascii()会将参数c 转换成7 位的unsigned char 值, 第八位则会被清除, 此字符即会被转成ASCII码字符.返回值:将转换成功的ASCII 码字符值返回.范例/* 将int 型a 转换成ASSII 码字符*/#include <stdlib.h>main(){int a = 217;char b;printf("before toascii() : a value =%d(%c)\n", a, a);b = toascii(a);printf("after toascii(): a value =%d(%c)\n", b, b);}执行结果:before toascii() : a value =217()after toascii() : a value =89(Y)strtoul()—将字符串转换成无符号长整型数相关函数:atof, atoi, atol, strtod, strtol头文件:#include <stdlib.h>定义函数:unsigned long int strtoul(const char *nptr, char **endptr, int base);函数说明:strtoul()会将参数nptr 字符串根据参数base 来转换成无符号的长整型数。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C语言文件操作函数大全 2009-05-27 10:52:06.0 来源:e800技术客 关键词: C语言 文件操作 函数

赤峰  赤峰的文章

 Visual C++ tr1中的shared_ptr和随机数

关于C语言中的无符号数和有符号数

 各种函数说明

个人页面 登陆交谈

clearerr(清除文件流的错误旗标) 相关函数 feof 表头文件 #include 定义函数 void clearerr(FILE * stream); 函数说明 clearerr()清除参数stream指定的文件流所使用的错误旗标。 返回值

fclose(关闭文件) 相关函数 close,fflush,fopen,setbuf 表头文件 #include 定义函数 int fclose(FILE * stream); 函数说明 fclose()用来关闭先前fopen()打开的文件。此动作会让缓冲区内的数据写 入文件中,并释放系统所提供的文件资源。 返回值 若关文件动作成功则返回0,有错误发生时则返回EOF并把错误代码存 到errno。 错误代码 EBADF表示参数stream非已打开的文件。 范例 请参考fopen()。

fdopen(将文件描述词(句柄)转为文件指针) 相关函数 fopen,open,fclose 表头文件 #include 定义函数 FILE * fdopen(int fildes,const char * mode); 函数说明 fdopen()会将参数fildes 的文件描述词,转换为对应的文件指针后返回。 参数mode 字符串则代表着文件指针的流形态,此形态必须和原先文件 描述词读写模式相同。关于mode 字符串格式请参考fopen()。 返回值 转换成功时返回指向该流的文件指针。失败则返回NULL,并把错误代 码存在errno中。 范例 #include main() { FILE * fp =fdopen(0,”w+”); fprintf(fp,”%s\n”,”hello!”); fclose(fp); } 执行 hello!

feof(检查文件流是否读到了文件尾) 相关函数 fopen,fgetc,fgets,fread 表头文件 #include 定义函数 int feof(FILE * stream); 函数说明 feof()用来侦测是否读取到了文件尾,尾数stream为fopen()所返回之文 件指针。如果已到文件尾则返回非零值,其他情况返回0。 返回值 返回非零值代表已到达文件尾。

fflush(更新缓冲区) 相关函数 write,fopen,fclose,setbuf 表头文件 #include 定义函数 int fflush(FILE* stream); 函数说明 fflush()会强迫将缓冲区内的数据写回参数stream指定的文件中。如果参 数stream为NULL,fflush()会将所有打开的文件数据更新。 返回值 成功返回0,失败返回EOF,错误代码存于errno中。 错误代码 EBADF 参数stream 指定的文件未被打开,或打开状态为只读。其它错 误代码 范例 参考write()

fgetc(由文件中读取一个字符) 相关函数 open,fread,fscanf,getc 表头文件 include 定义函数 nt fgetc(FILE * stream); 函数说明 fgetc()从参数stream所指的文件中读取一个字符。若读到文件尾而无数 据时便返回EOF。 返回值 getc()会返回读取到的字符,若返回EOF则表示到了文件尾。 范例 #include main() { FILE *fp; int c; fp=fopen(“exist”,”r”); while((c=fgetc(fp))!=EOF) printf(“%c”,c); fclose(fp); } fgets(由文件中读取一字符串) 相关函数 open,fread,fscanf,getc 表头文件 include 定义函数 har * fgets(char * s,int size,FILE * stream); 函数说明 fgets()用来从参数stream所指的文件内读入字符并存到参数s所指的内存 空间,直到出现换行字符、读到文件尾或是已读了size-1个字符为止, 最后会加上NULL作为字符串结束。 返回值 gets()若成功则返回s指针,返回NULL则表示有错误发生。 范例 #include main() { char s[80]; fputs(fgets(s,80,stdin),stdout); } 执行 this is a test /*输入*/ this is a test /*输出*/

fileno(返回文件流所使用的文件描述词) 相关函数 open,fopen 表头文件 #include 定义函数 int fileno(FILE * stream); 函数说明 fileno()用来取得参数stream指定的文件流所使用的文件描述词。 返回值 返回文件描述词。 范例 #include main() { FILE * fp; int fd; fp=fopen(“/etc/passwd”,”r”); fd=fileno(fp); printf(“fd=%d\n”,fd); fclose(fp); } 执行 fd=3

fopen(打开文件) 相关函数 open,fclose 表头文件 #include 定义函数 FILE * fopen(const char * path,const char * mode); 函数说明 参数path字符串包含欲打开的文件路径及文件名,参数mode字符串则代 表着流形态。 mode有下列几种形态字符串: r 打开只读文件,该文件必须存在。 r+ 打开可读写的文件,该文件必须存在。 w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消 失。若文件不存在则建立该文件。 w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会 消失。若文件不存在则建立该文件。 a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果 文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。 a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如 果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保 留。 上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b 字 符用来告诉函数库打开的文件为二进制文件,而非纯文字文件。不过在POSIX 统,包含Linux都会忽略该字符。由fopen()所建立的新文件会具有 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666)权限,此文件 限也会参考umask值。 返回值 文件顺利打开后,指向该流的文件指针就会被返回。若果文件打开失败 则返回NULL,并把错误代码存在errno 中。 附加说明 一般而言,开文件后会作一些文件读取或写入的动作,若开文件失败, 接下来的读写动作也无法顺利进行,所以在fopen()后请作错误判断及处理。 范例 #include main() { FILE * fp; fp=fopen(“noexist”,”a+”); if(fp= =NULL) return; fclose(fp); }

fputc(将一指定字符写入文件流中) 相关函数 fopen,fwrite,fscanf,putc 表头文件 #include 定义函数 int fputc(int c,FILE * stream); 函数说明 fputc 会将参数c 转为unsigned char 后写入参数stream 指定的文件中。 返回值 fputc()会返回写入成功的字符,即参数c。若返回EOF则代表写入失败。 范例 #include main() { FILE * fp; char a[26]=”abcdefghijklmnopqrstuvwxyz”; int i; fp= fopen(“noexist”,”w”); for(i=0;i<26;i++) fputc(a,fp); fclose(fp); }

fputs(将一指定的字符串写入文件内) 相关函数 fopen,fwrite,fscanf,fputc,putc 表头文件 #include 定义函数 int fputs(const char * s,FILE * stream); 函数说明 fputs()用来将参数s所指的字符串写入到参数stream所指的文件内。 返回值 若成功则返回写出的字符个数,返回EOF则表示有错误发生。 范例 请参考fgets()。

fread(从文件流读取数据) 相关函数 fopen,fwrite,fseek,fscanf 表头文件 #include 定义函数 size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream); 函数说明 fread()用来从文件流中读取数据。参数stream为已打开的文件指针,参 数ptr 指向欲存放读取进来的数据空间,读取的字符数以参数 size*nmemb来决定。Fread()会返回实际读取到的nmemb数目,如果此值 比参数nmemb 来得小,则代表可能读到了文件尾或有错误发生,这时 必须用feof()或ferror()来决定发生什么情况。 返回值 返回实际读取到的nmemb数目。 附加说明 范例 #include #define nmemb 3 struct test { char name[20]; int size; }s[nmemb]; int main(){ FILE * stream;

相关文档
最新文档