C言語で大容量ファイルの転送を行うには

C 言語では、以下の手順でファイルの大容量転送を実現できます。

  1. fopen()
FILE *file = fopen("filename", "rb");
if (file == NULL) {
    printf("Failed to open the file.\n");
    return;
}
  1. メモリ確保
// 假设缓冲区大小为1024字节
char *buffer = (char *)malloc(1024);
  1. fread() 関数
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, 1024, file)) > 0) {
    // 发送缓冲区中的数据
    if (send(socket, buffer, bytesRead, 0) == -1) {
        printf("Failed to send data.\n");
        break;
    }
}
  1. fclose()
  2. 解放()
fclose(file);
free(buffer);

大容量ファイル転送の実装例を簡単に示しました。ただし、大容量ファイルでは、チャンク転送、断点再開などのより複雑な処理が必要な場合があります。

bannerAds