日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

stdio.h: C++输入输出操作

發布時間:2025/6/17 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stdio.h: C++输入输出操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

頭文件:<stdio.h>、<cstdio>

  • 1、通過“streams”來操作物理設備:如鍵盤、打印機、終端等。

  • 2、流通過指針來連接“FILE”對象,三個自動創建的標準流:stdin, stdout 和 stderr。

指示信息:

  • 1、錯誤指示:當流操作發生錯誤是置位,可以通過ferror函數檢查,通過 clearerr, freopen 或者 rewind重置。

  • 2、文件結束:讀/寫到文件結束時置位,可通過feof檢查,通過 clearerr/freopen 或者重定位函數(rewind, fseek and fsetpos)置位。

  • 3、位置指示:在讀/寫操作中,指向流的下一個字符的內部指針??梢酝ㄟ^ ftell and fgetpos獲得其值,通過 rewind, fseek and fsetpos 改變其值。

一、文件操作

1、int remove ( const char * filename );

param:filename,文件名(可以包含文件路徑) return:若成功,返回0;否則返回非零值 /* remove example: remove myfile.txt */ #include <stdio.h>int main () {if( remove( "myfile.txt" ) != 0 )perror( "Error deleting file" );elseputs( "File successfully deleted" );return 0; }

2、int rename ( const char * oldname, const char * newname );

param:oldname,原文件名(可以包含文件路徑); newname,新文件名(可以包含文件路徑) return:成功重命名,返回0;否則返回非零。 /* rename example */ #include <stdio.h>int main () {int result;char oldname[] ="oldname.txt";char newname[] ="newname.txt";result= rename( oldname , newname );if ( result == 0 )puts ( "File successfully renamed" );elseperror( "Error renaming file" );return 0; }

3、FILE * tmpfile ( void );

創建一個臨時的二進制文件。自動創建,當流關閉時刪除。 ret:指向臨時文件的流指針;失敗返回NULL。 /* tmpfile example */ #include <stdio.h> #include <string.h>int main () {char buffer [256];FILE * pFile;pFile = tmpfile ();do {if (!fgets(buffer,256,stdin)) break; //臨時緩沖文件流fputs (buffer,pFile);} while (strlen(buffer)>1);rewind(pFile);while (!feof(pFile)) {if (fgets (buffer,256,pFile) == NULL) break;fputs (buffer,stdout);}fclose (pFile);return 0; }

4、char * tmpnam ( char * str );

  • 返回一個文件名(與原來所有文件不同)。

  • 如果str是空指針,返回的字符串存儲在靜態數組中,可用返回值取得。

  • 如果str不是空指針,將指向至少 L_tmpnam字符的數組,次數組用提到的臨時文件名填充

    para:指向字符數組的指針,此字符以C-字符串存儲臨時文件名。 ret:成功,返回指向字符串的指針;如果str為NULL,返回指向內部緩存的指針。失敗,返回NULL。
/* tmpnam example */ #include <stdio.h>int main () {char buffer [L_tmpnam];char * pointer;tmpnam (buffer);printf ("Tempname #1: %s\n",buffer);pointer = tmpnam (NULL);printf ("Tempname #2: %s\n",pointer);return 0; }

二、文件訪問

1、int fclose ( FILE * stream );

描述:關閉文件。成功,返回0;失敗,返回EOF。

2、int fflush ( FILE * stream );

描述:文件等待更新(讀/寫),文件將在輸出操作后、輸入操作前更新。成功,返回0;失敗,返回EOF。

/* fflush example */ #include <stdio.h> char mybuffer[80]; int main() {FILE * pFile;pFile = fopen ("example.txt","r+");if (pFile == NULL) perror ("Error opening file");else {fputs ("test",pFile);fflush (pFile); // flushing or repositioning requiredfgets (mybuffer,80,pFile);puts (mybuffer);fclose (pFile);return 0;} }

3、FILE * fopen ( const char * filename, const char * mode );

描述:

文件名(C-風格字符串),mode(r,w,a,r+,w+,a+)。

成功,返回指向文件的指針;失敗,返回NULL。

4、FILE * freopen ( const char * filename, const char * mode, FILE * stream );

描述:

重新以新的文件名或模式打開流文件。

文件名改變,先關閉原文件,斷開流定向;然后重定向流到新的文件。如果文件名為空,改變文件的模式。

如果文件成功重新打開,返回新文件流指針;否則,返回NULL。

/* freopen example: redirecting stdout */ #include <stdio.h>int main () {freopen ("myfile.txt","w",stdout);printf ("This sentence is redirected to a file.");fclose (stdout);return 0; }

5、void setbuf ( FILE * stream, char * buffer );

描述:

流:打開的對象

buffer:使用者申請的buff,至少BUFSIZ bytes。

當文件流指向打開的文件,但無任何讀/寫操作時使用。

/* setbuf example */ #include <stdio.h>int main () {char buffer[BUFSIZ];FILE *pFile1, *pFile2;pFile1=fopen ("myfile1.txt","w");pFile2=fopen ("myfile2.txt","a");setbuf ( pFile1 , buffer );fputs ("This is sent to a buffered stream",pFile1);fflush (pFile1);setbuf ( pFile2 , NULL );fputs ("This is sent to an unbuffered stream",pFile2);fclose (pFile1);fclose (pFile2);return 0; }

6、int setvbuf ( FILE * stream, char * buffer, int mode, size_t size );

描述:

改變流緩存,改變模式和大小。

如果buffer指針為空,自動申請buffer。

/* setvbuf example */ #include <stdio.h>int main () { FILE *pFile;pFile=fopen ("myfile.txt","w");setvbuf ( pFile , NULL , _IOFBF , 1024 );// File operations herefclose (pFile);return 0; }

三、格式化輸入輸出

四、字符輸入輸出

五、直接輸入輸出

1、size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

描述:

從流中讀取數據塊。

讀取count元素,每個元素大小為size bytes,從stream保存到內存塊ptr。

返回值:成功讀取的元素數目。

/* fread example: read an entire file */ #include <stdio.h> #include <stdlib.h>int main () {FILE * pFile;long lSize;char * buffer;size_t result;pFile = fopen ( "myfile.bin" , "rb" );if (pFile==NULL) {fputs ("File error",stderr); exit (1);}// obtain file size:fseek (pFile , 0 , SEEK_END);lSize = ftell (pFile);rewind (pFile);// allocate memory to contain the whole file:buffer = (char*) malloc (sizeof(char)*lSize);if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}// copy the file into the buffer:result = fread (buffer,1,lSize,pFile);if (result != lSize) {fputs ("Reading error",stderr); exit (3);}/* the whole file is now loaded in the memory buffer. */// terminatefclose (pFile);free (buffer);return 0; }

2、size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

描述:寫塊數據到流。

寫count元素,每個元素大小為size bytes,從內存塊ptrstream到stream。

返回值:成功寫的元素數目。

/* fwrite example : write buffer */ #include <stdio.h>int main () {FILE * pFile;char buffer[] = { 'x' , 'y' , 'z' };pFile = fopen ("myfile.bin", "wb");fwrite (buffer , sizeof(char), sizeof(buffer), pFile);fclose (pFile);return 0; }

六、文件定位

1、int fgetpos ( FILE * stream, fpos_t * pos );

描述:

在流中取回當前的位置。

成功,返回0;失敗,返回非0.

/* fgetpos example */ #include <stdio.h> int main () {FILE * pFile;int c;int n;fpos_t pos;pFile = fopen ("myfile.txt","r");if (pFile==NULL) perror ("Error opening file");else{c = fgetc (pFile);printf ("1st character is %c\n",c);fgetpos (pFile,&pos);for (n=0;n<3;n++){fsetpos (pFile,&pos);c = fgetc (pFile);printf ("2nd character is %c\n",c);}fclose (pFile);}return 0; }

2、int fseek ( FILE * stream, long int offset, int origin );

重定位流的位置。

offset:從origin開始的字節數。

origin:位置。(SEEK_SET SEEK_CUR SEEK_END)

成功,返回0;失敗,返回非0.

如果讀/寫錯誤,設置ferror。

/* fseek example */ #include <stdio.h>int main () {FILE * pFile;pFile = fopen ( "example.txt" , "wb" );fputs ( "This is an apple." , pFile );fseek ( pFile , 9 , SEEK_SET );fputs ( " sam" , pFile );fclose ( pFile );return 0; }

3、int fsetpos ( FILE * stream, const fpos_t * pos );

重新存儲流的當前位置到pos。成功,返回0;失敗,返回非0。

/* fsetpos example */ #include <stdio.h>int main () {FILE * pFile;fpos_t position;pFile = fopen ("myfile.txt","w");fgetpos (pFile, &position);fputs ("That is a sample",pFile);fsetpos (pFile, &position);fputs ("This",pFile);fclose (pFile);return 0; }

4、long int ftell ( FILE * stream );

成功,返回當前的位置。失敗,返回-1L。

得到流中當前的位置。

/* ftell example : getting size of a file */ #include <stdio.h>int main () {FILE * pFile;long size;pFile = fopen ("myfile.txt","rb");if (pFile==NULL) perror ("Error opening file");else{fseek (pFile, 0, SEEK_END); // non-portablesize=ftell (pFile);fclose (pFile);printf ("Size of myfile.txt: %ld bytes.\n",size);}return 0; }

5、void rewind ( FILE * stream );

設置流的位置到begging。

七、異常處理

1、void clearerr ( FILE * stream );

重置error和eof標志。

2、int feof ( FILE * stream );

檢查eof標志。

eof置位,返回非零。否則,返回0。

3、int ferror ( FILE * stream );

檢查error標志。

4、void perror ( const char * str );

打印錯誤信息。

八、宏常量

BUFSIZ:緩存大小EOF:文件結尾FILENAME_MAX:文件名的最大長度FOPEN_MAX:同時打開流的極限值L_tmpnam:臨時文件名的最小長度NULL:空指針TMP_MAX:臨時文件最大數量

九、類型

FILE:控制流的對象fpos_t:文件中的特定位置對象size_t:無符號整型

十、對象

stderr:標準錯誤流,FILE * stderr;stdin:標準輸入流,FILE * stdin;stdout:標準輸出流,FILE * stdout;

總結

以上是生活随笔為你收集整理的stdio.h: C++输入输出操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。