輸出資料至檔案:

#include <stdio.h>

#include <stdlib.h>

 

main(void){

  FILE *fpTest = fopen("Test.txt","a+");

  if (!fpTest ) {

    printf("檔案建立失敗...\n");

    exit(1);

  }

  int y = 19;
  fprintf(fpTest ,"She is %d years old.\n", y);
  fclose(fpTest );

  return 0;

}

 

至檔案讀取資料:

#include <stdio.h>

#include <stdlib.h>

 

假設Test.txt 中有 "She is 19 years old." 的文字

main(void) {

  FILE *fPtr;

  charc1[20], c2[20], c4[20], c5[20];

  intc3;

  fPtr = fopen("Test.txt", "r");

  if(!fPtr) {

    printf("檔案開啟失敗...\n");

    exit(1);

  }

    fscanf(fPtr, "%s%s%d%s%s", c1, c2, &c3, c4, c5);

    fclose(fPtr);

    printf("%s %s %d %s %s\n", c1, c2, c3, c4, c5);

    return 0;

}

 

 

格式化旗標:

1、如果想要在程式進行過程中,讓所有的串流都維持指定的格式,可以使用格式化旗標( ios::left),用setf()unsetf()        來設定與取消格式化旗標。 

2、可以一次設定一個格式化旗標,如果要設定兩個格式化旗標,使用 | 來連結,例如: 

    cout.setf(ios::showbase | ios::hex);

3、基本操作:cout.unsetf(ios::dec); // 取消10進位顯示

                        cout.setf(ios::hex | ios::scientific); // 16進位顯示或科學記號顯示

 

 

arrow
arrow
    全站熱搜

    阿洲 發表在 痞客邦 留言(0) 人氣()