?? 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 + -