0

I'm writing chucks of chat *ptr into file, but it takes me so long to finish. I don't know why.

for(int i = 1; i < 64048; i++){
    if(ptr[i] != NULL){
        fwrite(ptr[i], 1, strlen(ptr[i])-6, file);
        free(ptr[i]);
    }
}

there is array of char pointers storing 8186 bytes, and I want to write into file, but the time is so long.

I have 64048 chunks of string need to write into file.

What could cause the slowing fwrite() ??

thank you

4

1 に答える 1

3

fwrite()OP は、他の小さなエラーの中で、size に間違った方法を使用しています。

「8186 バイトを格納する char ポインターの配列」が 1 各配列に 8186 有効バイトのデータがあることを意味する 場合
charサイズ 8186 を使用します。この6が何であるかを確認してください。ノート:
charsize_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream )

が 6 未満の場合、OPが問題を- 6引き起こしていると思われますstrlen(ptr[i])size_tcountfwrite()

for(int i = 0; i < 64048; i++){  // start at 0
    if(ptr[i] != NULL){
        int result; 

        result = fwrite(ptr[i], 1, 8186, file);
        if (result != 8186) ; handle error;

        // or

        size_t len = strlen(ptr[i]) + 1; /// Add 1 for the NUL byte
        if (len < 6) ; handle error of whatever the OP's 6 is about.
        result = fwrite(ptr[i], 1, len, file);
        if (result != len) ; handle error;

        free(ptr[i]);
    }
}
于 2013-09-15T03:10:14.377 に答える