📄 stdio.part2.h
字号:
@函数名称: getc
函数原型: int getc(FILE *fp);
函数功能: 从fp所指向的文件中读入一个字符
函数返回: 返回所读的字符,若文件结束或出错,返回EOF
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
char ch;
printf("Input a character:");
ch=getc(stdin);
printf("The character input was: '%c'",ch);
return 0;
}
@函数名称: getchar
函数原型: int getchar(void);
函数功能: 从标准输入设备读取一个字符
函数返回: 所读字符.若文件结束或出错,则返回-1
参数说明:
所属文件: <stdio.h>
#include<stdio.h>
int main()
{
char c;
c=getchar();
putchar(c);
return 0;
}
@函数名称: getche
函数原型: int getche(void)
函数功能: 从控制台读取一个字符并回显
函数返回: 读取的字符
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("Input a character:");
ch=getche();
printf("You input a '%c'",ch);
return 0;
}
@函数名称: getw
函数原型: int getw(FILE * fp);
函数功能: 从fp所指向的文件读取一个字(整数)
函数返回: 输入的整数.如果文件结束或出错,返回-1
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#define FNAME "test.dat"
int main()
{
FILE *fp;
int word;
fp=fopen(FNAME,"wb");
if (fp==NULL)
{
printf("Error opening file %s",FNAME);
exit(1);
}
word=94;
putw(word,fp);
if (ferror(fp))
printf("Error writing to file!");
else
printf("Successful write!");
fclose(fp);
fp=fopen(FNAME,"rb");
if (fp==NULL)
{
printf("Error opening file %s",FNAME);
exit(1);
}
word=getw(fp);
if (ferror(fp))
printf("Error reading file!");
else
printf("Successful read: word=%d",word);
fclose(fp);
unlink(FNAME);
return 0;
}
@函数名称: putc
函数原型: int putc(int ch,FILE * fp);
函数功能: 把一个字符ch输出到fp所指定的文件中
函数返回: 输出字符ch,若出错,返回EOF
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
char msg[]="Hello world";
int i=0;
while (msg[i])
putc(msg[i++],stdout);
return 0;
}
@函数名称: putchar
函数原型: int putchar(char ch);
函数功能: 把字符ch输出到标准输出设备
函数返回: 输出字符ch,若出错,返回EOF
参数说明:
所属文件: <stdio.h>
#include<stdio.h>
int main()
{
char a,b,c;
a='B';
b='O';
c='Y';
putchar(a);putchar(b);putchar(c);
return 0;
}
@函数名称: puts
函数原型: int puts(char * str);
函数功能: 把str指向的字符串输出到标准输出设备,将'\0'转换为回车换行
函数返回: 返回换行符,若失败,返回EOF
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
char string[]="This is an example output string";
puts(string);
return 0;
}
@函数名称: gets
函数原型: char * gets(char *str)
函数功能: 从终端输入一个字符串到字符数组,并且得到一个函数值.该函数值是字符数组的起始地址
函数返回: 读取的字符指针str,操作错误返回NULL
参数说明: str-保存读取的字符串
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
char buffer[80];
while(gets(buffer)!=NULL)
puts(buffer);
return 0;
}
@函数名称: putw
函数原型: int putw(int w,FILE * fp);
函数功能: 将一个整数w(即一个字)写到fp指向的文件中
函数返回: 返回输出的整数,若出错,返回EOF
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#define FNAME "test.dat"
int main()
{
FILE *fp;
int word;
fp=fopen(FNAME,"wb");
if (fp==NULL)
{
printf("Error opening file %s",FNAME);
exit(1);
}
word=94;
putw(word,fp);
if (ferror(fp))
printf("Error writing to file");
else
printf("Successful write");
fclose(fp);
fp=fopen(FNAME,"rb");
if (fp==NULL)
{
printf("Error opening file %s",FNAME);
exit(1);
}
word=getw(fp);
if (ferror(fp))
printf("Error reading file");
else
printf("Successful read: word=%d",word);
fclose(fp);
unlink(FNAME);
return 0;
}
@函数名称: rename
函数原型: int rename(char * oldname,char * newname);
函数功能: 把由oldname所指的文件名,改为由newname所指的文件名
函数返回: 成功返回0,出错反回-1
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
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.",oldname,newname);
else
perror("rename");
return 0;
}
@函数名称: remove
函数原型: int remove(char *fname)
函数功能: 删除一个文件
函数返回: 0:操作成功,-1:操作失败
参数说明: fname-要删除的文件名称
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
char file[80];
printf("File to delete: ");
gets(file);
if(remove(file)==0)
printf("Removed %s.",file);
else
perror("remove");
return 0;
}
@函数名称: rewind
函数原型: void rewind(FILE * fp);
函数功能: 将fp指示的文件中的位置指针置于文件头位置,并清除文件结束标志和错识标志
函数返回:
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <dir.h>
int main()
{
FILE *fp;
char *fname ="Test.dat",*newname,first;
newname=mktemp(fname);
fp=fopen(newname,"w+");
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
rewind(fp);
fscanf(fp,"%c",&first);
printf("The first character is: %c",first);
fclose(fp);
remove(newname);
return 0;
}
@函数名称: perror
函数原型: void perror(const char *s)
函数功能: 将表示错误的全程变量errno所对应解释输出到流stderr中
函数返回:
参数说明: s-表示错误的附加信息字符串
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
FILE *fp;
fp=fopen("perror.dat","r");
if (!fp)
perror("Unable to open file for reading");
return 0;
}
@函数名称: setbuf
函数原型: void setbuf(FILE *stream,char *buf)
函数功能: 指定某个流文件操作所使用的缓冲区
函数返回:
参数说明: fp-使用fopen打开的文件流指针,buf-大小必须为BUFSIZ宏长度的内存指针
所属文件: <stdio.h>
#include <stdio.h>
char outbuf[BUFSIZ];
int main()
{
setbuf(stdout,outbuf);
puts("This is a test of buffered output.");
puts("This output will go into outbuf,");
puts("and won't appear until the buffer,");
puts("fills up or we flush the stream.");
fflush(stdout);
return 0;
}
@函数名称: setvbuf
函数原型: int setvbuf(FILE *fp,char *buf,int type,size_t size)
函数功能: 对指定文件fp指定操作缓冲区
函数返回: 0-操作成功,非0-操作失败
参数说明: fp-文件指针,buf-缓冲区指针,size-缓冲区大小 ,type-模式,取值含义如下:
_IOFBF 0 满缓冲区后刷新缓冲区
_IOLBF 1 在缓冲区写入或读出一行字符后刷新缓冲区
_IONBF 2 满不缓冲
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
FILE *input,*output;
char bufr[512];
input=fopen("file.in","rb");
output=fopen("file.out","w");
if (setvbuf(input,bufr,_IOFBF,512)!=0)
printf("failed to set up buffer for input file");
else
printf("buffer set up for input file");
if (setvbuf(output,NULL,_IOLBF,132)!=0)
printf("failed to set up buffer for output file");
else
printf("buffer set up for output file");
fclose(input);
fclose(output);
return 0;
}
@函数名称: sprintf
函数原型: int sprintf (char *buffer,const char *format,… )
函数功能: buffer写入的缓冲区
函数返回: 写入的字符数(不包括终止符)
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <math.h>
int main()
{
char buffer[80];
sprintf(buffer,"An approximation of Pi is %f",M_PI);
puts(buffer);
return 0;
}
@函数名称: sscanf
函数原型: int sscanf(const char *in_str,const char *format,… )
函数功能: 从缓冲区中按指定格式输入字符,该函数用法同scanf
函数返回:
参数说明: in_str-数据源缓冲区
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
int day,year;
char weekday[10],month[10];
sscanf("Friday August 0014 1987","%s %s %d %d",weekday,month,&day,&year);
printf("%s %s %d %d\n",weekday,month,day,year);
return 0;
}
@函数名称: tmpfile
函数原型: FILE *tmpfile(void)
函数功能: 自动产生一个临时文件,并返回其流指针
函数返回: 临时文件流指针
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <process.h>
int main()
{
FILE *tempfp;
tempfp=tmpfile();
if (tempfp)
printf("Temporary file created");
else
{
printf("Unable to create temporary file");
exit(1);
}
return 0;
}
@函数名称: tmpnam
函数原型: char *tmpnam(char *s)
函数功能: 自动产生唯一的文件名称
函数返回:
参数说明: s-用于存放临时文件的字符指针
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
char name[13];
tmpnam(name);
printf("Temporary name: %s",name);
return 0;
}
@函数名称: ungetc
函数原型: int ungetc(int c,FILE *fp)
函数功能: 将字符c放到文件流fp的首部
函数返回: 字符c-操作成功,EOF-操作失败
参数说明: c-要写入的字符,fp-文件流指针
所属文件: <stdio.h>
#include <stdio.h>
#include <ctype.h>
int main()
{
int i=0;
char ch;
puts("Input an integer followed by a char:");
while((ch=getchar())!=EOF && isdigit(ch))
i=10*i+ch-48;
if (ch!=EOF)
ungetc(ch,stdin);
printf("i=%d,next char in buffer=%c",i,getchar());
return 0;
}
@函数名称: ungetch
函数原型: int ungetch(int c)
函数功能: 把一个字符退回到键盘缓冲区中
函数返回:
参数说明:
所属文件: <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int main()
{
int i=0;
char ch;
puts("Input an integer followed by a char:");
while((ch=getche())!=EOF&&isdigit(ch))
i=10*i+ch-48;
if(ch!=EOF)
ungetch(ch);
printf("i=%d,next char in buffer=%c",i,getch());
return 0;
}
@函数名称: fgetpos,fsetpos
函数原型: int fgetpos(FILE *fp,fpos_t *pos);
int fsetpos(FILE *stream,const fpos_t *pos);
函数功能: 获取、设置文件位置
函数返回: 如果操作成功,数返回零值,否则返回一个非零值
参数说明: fp-文件指针,pos-对象指针
所属文件: <stdio.h>
#include <stdio.h>
int main()
{
FILE *fp;
fpos_t position;
auto char buffer[80];
fp=fopen("file","r");
if(fp!=NULL){
fgetpos(fp,&position); /* get position */
fgets(buffer,80,fp); /* read record */
fsetpos(fp,&position); /* set position */
fgets(buffer,80,fp); /* read same record */
fclose(fp);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -