1. 程式人生 > >56.fread fwrite

56.fread fwrite

寫入 失敗 spa pre file turn nbsp log sizeof

  • fwrite
     1 //初始化數組
     2     int a[100];
     3     for (int i = 0; i < 100;i++)
     4     {
     5         printf("\n%d", a[i] = i);
     6     }
     7 
     8     //以寫的方式打開文件
     9     FILE *pf = fopen("C:\\1.bin", "wb");
    10 
    11     //寫入第一個內存首地址,第二個元素大小,第三個個數
    12     int num=fwrite(a, sizeof(int), 99, pf);
    13     //返回寫入成功的個數.失敗返回0
    14     printf("
    fwrite return =%d", num); 15 fclose(pf);

  • fread
    1 //分配大小
    2     void *p = malloc(400);
    3     //以讀的方式打開文件
    4     FILE *pf = fopen("C:\\1.bin", "rb");
    5     //第一個內存首地址,第二個元素大小,第三個個數
    6     int num= fread(p, 4, 100, pf);
    7     //返回讀取成功個數
    8     printf("fread  return =%d", num);
    9     fclose(pf);

56.fread fwrite