日本无码中文字幕片|日本精品在线观看无码视频|国产精品免费啪啪|Av无码一区二区|亚洲在线黄片免费观看|亚洲日韩中文字幕在线观看|熟女激情乱伦在线观看a黄片|成年人观看毛片网址|AV色色色色日韩性草|国产高清无码免费

C語言

C語言文件操作函數freopen詳解

時間:2025-01-18 01:54:44 C語言 我要投稿
  • 相關推薦

C語言文件操作函數freopen詳解

  今天做USACO 用到了文件的操作。 之前做USACO只是格式化的些 寫 freopen("xxx.in","r",stdin) 和"freopen("xxx.out","w",stdout)"

  百度百科上是這么介紹的:

  函數名: freopen

  功 能: 替換一個流,或者說重新分配文件指針,實現重定向。如果stream流已經打開,則先關閉該流。如果該流已經定向,則freopen將會清除該定向。此函數一般用于將一個指定的文件打開一個預定義的流:標準輸入、標準輸出或者標準出錯。

  用 法: FILE *freopen(const char *filename,const char *type, FILE *stream);

  頭文件:stdio.h

  例1:

  復制代碼 代碼如下:

  #include

  #include

  int main()

  {

  if(freopen("file.txt","w",stdout)==NULL)

  fprintf(stderr,"errorn");

  printf("This is in the filen"); //這句話會在file.txt中顯示。

  fclose(stdout); //使用fclose()函數就可以把緩沖區(qū)內最后剩余的數據輸出到磁盤文件中,并釋放文件指針和有關的緩沖區(qū)。

  return 0;

  }

  例2:

  復制代碼 代碼如下:

  //首先在同路徑下創(chuàng)建一個in.txt文本文檔寫入若干數字

  #include

  #include

  int main()

  {

  freopen("in.txt","r",stdin); //從in.txt 中讀入數據

  freopen("out.txt","w",stdout); // 將最后數據寫入out.txt中

  int a,b;

  while(scanf("%d%d",&a,&b)!=EOF) //數據是從in.txt中輸入的

  printf("%dn",a+b); //寫入out.txt中

  fclose(stdin);

  fclose(stdout);

  return 0;

  }

  freopen("CON","w",stdout) 表示在控制臺窗口上寫入數據;

  例3:

  復制代碼 代碼如下:

  #include

  #include

  int main()

  {

  // FILE *stream;

  freopen("file1.txt","w",stdout);

  printf("this is in file1.txt"); // 這句話在file1.txt中顯示

  freopen("CON","w",stdout);

  printf("And this is in command.n"); //這句話在控制臺上顯示

  return 0;

  }

  例5: 關于fread 可以通過下面的程序,一看就知道什么意思了

  復制代碼 代碼如下:

  #include

  #include

  int main()

  {

  FILE *stream

  char s[102400]="";

  if((stream=freopen("file.txt","r",stdin))==null)

  exit(-1);

  fread(s,1,1024,stdin); // 讀取file.txt中1到1024位,放入s中 ,我是這么理解的

  printf("%sn",s);

  return 0;

  }

【C語言文件操作函數freopen詳解】相關文章:

C語言文件操作函數05-22

詳解C語言文件操作中fgets與fputs函數12-31

C語言最實用的文件操作函數大全04-07

C語言超詳細文件操作函數大全05-19

C語言文件操作解析詳解及實例代碼04-13

C語言文件操作中fgets與fputs函數講解07-17

C語言文件操作函數總結分析(超詳細)02-26

C語言中文件操作詳解及實例代碼01-29

C語言文件操作教程05-11