国产精品久久9a久美女性色,日韩综合中文字幕,天天射天天色天天干,亚洲天堂中文在线,成年人午夜视频,国产精品伦理久久久久久,亚洲欧美视频一区二区

C語(yǔ)言

C語(yǔ)言文件操作函數(shù)freopen詳解

時(shí)間:2025-01-18 01:54:44 C語(yǔ)言 我要投稿
  • 相關(guān)推薦

C語(yǔ)言文件操作函數(shù)freopen詳解

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

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

  函數(shù)名: freopen

  功 能: 替換一個(gè)流,或者說(shuō)重新分配文件指針,實(shí)現(xiàn)重定向。如果stream流已經(jīng)打開(kāi),則先關(guān)閉該流。如果該流已經(jīng)定向,則freopen將會(huì)清除該定向。此函數(shù)一般用于將一個(gè)指定的文件打開(kāi)一個(gè)預(yù)定義的流:標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出或者標(biāo)準(zhǔn)出錯(cuò)。

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

  頭文件:stdio.h

  例1:

  復(fù)制代碼 代碼如下:

  #include

  #include

  int main()

  {

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

  fprintf(stderr,"errorn");

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

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

  return 0;

  }

  例2:

  復(fù)制代碼 代碼如下:

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

  #include

  #include

  int main()

  {

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

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

  int a,b;

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

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

  fclose(stdin);

  fclose(stdout);

  return 0;

  }

  freopen("CON","w",stdout) 表示在控制臺(tái)窗口上寫(xiě)入數(shù)據(jù);

  例3:

  復(fù)制代碼 代碼如下:

  #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"); //這句話在控制臺(tái)上顯示

  return 0;

  }

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

  復(fù)制代碼 代碼如下:

  #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語(yǔ)言文件操作函數(shù)freopen詳解】相關(guān)文章:

C語(yǔ)言文件操作函數(shù)05-22

詳解C語(yǔ)言文件操作中fgets與fputs函數(shù)12-31

C語(yǔ)言最實(shí)用的文件操作函數(shù)大全04-07

C語(yǔ)言超詳細(xì)文件操作函數(shù)大全05-19

C語(yǔ)言文件操作解析詳解及實(shí)例代碼04-13

C語(yǔ)言文件操作中fgets與fputs函數(shù)講解07-17

C語(yǔ)言文件操作函數(shù)總結(jié)分析(超詳細(xì))02-26

C語(yǔ)言中文件操作詳解及實(shí)例代碼01-29

C語(yǔ)言文件操作教程05-11