Почему fwrite пишет больше, чем я говорю его?

dev против резервного копирования - удостоверяются изменение/устранение, Вы хотите подать заявку, прибывают из сценария. толстые, неуклюжие пальцы не имеют никакого места при работе с живыми данными. Если Вы можете, ожидать окна обслуживания, чтобы применить и откатывать, если Вы можете.

, Если Вы не можете ожидать для применения прямо после снимка, скопируйте, Удостоверьтесь, что все понимают, сколько работы могло бы быть вовлечено в продвижение вперед изменений между последним снимком и время whne, Вы подали заявку, "фиксация" должна он не удаваться.

13
задан Sinan Ünür 19 October 2009 в 00:47
поделиться

4 ответа

If you are on a DOSish system (say, Windows) and the file is not opened in binary mode, line-endings will be converted automatically and each "line" will add one byte.

So, specify "wb" as the mode rather than just "w" as @caf points out. It will have no effect on Unix like platforms and will do the right thing on others.

For example:

#include <stdio.h>

#define LF 0x0a

int main(void) {
    char x[] = { LF, LF };

    FILE *out = fopen("test", "w");

    printf("%d", ftell(out));
    fwrite(x, 1, sizeof(x), out);
    printf("%d", ftell(out));

    fclose(out);
    return 0;
}

With VC++:

C:\Temp> cl y.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

y.c
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:y.exe

C:\Temp> y.exe
04

With Cygwin gcc:

/cygdrive/c/Temp $ gcc y.c -o y.exe

/cygdrive/c/Temp $ ./y.exe
02
25
ответ дан 1 December 2019 в 20:11
поделиться

The variable write is uninitialized and so the size of the array and the amount written will be essentially random.

2
ответ дан 1 December 2019 в 20:11
поделиться

It may depend on the mode in which you opened the file. If you open it as a text file, then \n may be written as \r\n in DOS/Windows systems. However, ftello64() probably only gives the binary file pointer, which would count in the extra \r characters written. Try clearing the outbuf[] of any \n data or try opening the out file as binary ("wb" instead of "w").

4
ответ дан 1 December 2019 в 20:11
поделиться

Interesting. Works fine on Windows VC++, albeit ftello64 replaced with ftell.

0
ответ дан 1 December 2019 в 20:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: