雖說大部分場合能用c的都可以用c++代替,但是對於開發庫,還是用c效率高。下面介紹c標準IO庫:
文件操作標準I/O庫函數:
fopen、fread、fwrite、fclose、fflush、fseek、fgetc、getc、getchar、fputc、putc、
putchar、fgets、gets、printf、fprintf、sprintf、scanf、fscanf、sscanf、fgetops、
fsetops、ftell、rewind、freopen、setvbuf、remove、fileno、fdopen
目錄操作標準I/O庫函數:
opendir、readdir、telldir、seekdir、closedir
具體的介紹,這裏就不多說了,請移步參看:
https://www.cnblogs.com/hiflex/archive/2012/08/20/stdio-lib.html
這裏這要介紹如何利用文件操作庫,讀取下面txt文件中的內容:
pptv is good
welcome join us!
代碼如下:
int main() {
FILE *fp;
fp = fopen("e:/test.txt", "r");
char ss[100];
int cc = 0;
while (1) {
if ((ss[cc] = fgetc(fp)) >= 0)
cc++;
else
break;
}
ss[cc] = '\0';
printf("%s\n", ss);
return 0;
}
將txt文件以文件流讀入的形式傳入fp中,再通過fgetc的函數獲取每一字節的數據,當遇到’\0’時,循環結束,最後別忘記在變量數組的尾部加入截止符’\0’。
如果大家覺得實用,請加波關注,我會陸續出一些新文章!