写入超过约2GB的文件时不会停止。

huangapple go评论65阅读模式
英文:

Writing doesn't stop for files above ~2gb in C

问题

我有一些C代码,应该生成一个密码并将其保存到文件中。我尝试输入一个大约为10 GB的长度(荒谬地大,但我想看看它能有多快),但它没有在10 GB处停止。

似乎当它超过1 GiB时(我假设是2 GiB,因为这是在使用#define _FILE_OFFSET_BITS 64之前它通常到达的大小),它不会停止写入,而是似乎会无限继续。

我正在使用Raspbian。

非常感谢任何帮助。

英文:

I have some C code that is supposed to generate a password and save it to a file. I tried to enter a length that would be about 10 gb (absurdly big but I wanted to see how fast it could do it), and it didn't stop at 10 gb.

#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(){
    int i = 0;
    char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-+={[]}\\|;:'\",<.>/?`~!@#$^&*()";
    long long size;
    char keypath[512];
    char password;
    printf("Where should the key be saved? ");
    fgets(keypath, sizeof(keypath), stdin);
    keypath[strcspn(keypath, "\n")] = 0;
    printf("How long should the key be? ");
    scanf("%lli",&size);
    getchar();
    srand((unsigned int)(time(NULL)));
    FILE* fptr = fopen(keypath,"a");
    if (fptr == NULL) {
        printf("Failed to open file for writing\n");
        return 1;
    }
    for (i = 0; i < size; i++){
        int choice = rand() % strlen(chars);
        password = chars[choice];
        fprintf(fptr, "%c", chars[choice]);
    }
    fclose(fptr); 
    return 0;
}

It seems like when it goes somewhere over 1 gib (I assume 2 gib since that's the regular amount it goes to before using #define _FILE_OFFSET_BITS 64) it doesn't stop writing and instead keeps going seemingly indefinitely.

I am using Raspbian.

Any help is much appreciated.

答案1

得分: 4

你很好地将变量 size 声明为 long long 类型,这样就确保了它能够表示 10 GB 的文件大小。

然而,你忘记了对变量 i 采取相同的操作。相反,你给了 i 数据类型 int

由于这个原因,循环

for (i = 0; i < size; i++){
    int choice = rand() % strlen(chars);
    password = chars[choice];
    fprintf(fptr, "%c", chars[choice]);
}

会因为有符号整数溢出而导致未定义的行为,除非你碰巧在一个能够表示 10 GB 文件大小的平台上。在大多数常见的个人电脑平台上,INT_MAX(即 int 能够表示的最大值)的值是 2147483647。在这些平台上,int 无法表示值 10000000000

英文:

It is good that you made the variable size to be of the type long long, so that it is guaranteed to be able to represent a file size of 10 GB.

However, you forgot to do the same with the variable i. Instead, you gave i the data type int.

Due to this, the loop

for (i = 0; i < size; i++){
    int choice = rand() % strlen(chars);
    password = chars[choice];
    fprintf(fptr, "%c", chars[choice]);
}

will invoke undefined behavior due to signed integer overflow, unless you happen to be on a platform on which int is able to represent a file size of 10 GB. On most common PC platforms, INT_MAX (which is the maximum value an int can represent) has a value of 2147483647. On those platforms, an int is unable to represent the value 10000000000.

huangapple
  • 本文由 发表于 2023年6月22日 01:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定