gpt4 book ai didi

c - fwrite() 不向文件写入任何内容 - C

转载 作者:行者123 更新时间:2023-11-30 17:06:55 25 4
gpt4 key购买 nike

我目前正在编写霍夫曼代码来压缩和解压缩文件。我目前正处于将霍夫曼树中每个字符的“路线”转换为 int 并将其写入二进制文件的阶段。这就是我所得到的:

int ConversionToBinary ( FILE *binaryFile, char *string, int *currentBit, int *bitCounter ) {
int x;
int stringLength;
stringLength = CalculateStringLength ( string );
short int i;
for ( i = 0; i < stringLength; i++ ) {
x = ConversionToNumber ( string [ i ] );
*currentBit = x | 1 << *contadorDeBits;
*bitCounter++;
if ( *bitCounter == MAX_NUMBER_OF_BITS ) {
fwrite ( currentBit, 1, sizeof ( int ), binaryFile );
*currentBit = 0;
*bitCounter = 0;
}
}
}

该部分代码基于我在 this thread 中找到的内容:

这是调用 ConversionToBinary 的函数的相关部分,以防万一:

currentBit = bitCounter = 0;
EmptyString ( string );
while ( ( character = fgetc ( textFile ) ) != EOF ) {
auxElement = SearchInHashTable ( HashTable, character );
strcpy ( string, auxElement -> route );
ConversionToBinary ( binaryFile, string, &currentBit, &bitCounter );
EmptyString ( string );
}

如您所见,我将所有相关数据存储在哈希表中,以便我可以轻松地从那里检索它。

事情是,当我调用ConversionToBinary时,它没有写任何东西,我不知道为什么会这样。我已经检查过条件 *( bitCounter == MAX_NUMBER_OF_BITS ) 是否满足,但逻辑上应该如此,所以即使实际转换是错误的,至少它应该写一些东西 em> 在我的文件中,但事实并非如此。

我目前已经没有想法了,我需要一些帮助。

最佳答案

I was checking why that operation went the way it did. If I'm not getting this wrong, it has to do something with the operator precedence. According to this, the operators * and ++ have the same precedence, and they are evaluated from right to left. So when i was doing this: *bitCounter++, instead of incrementing said value, i was setting its memory address one block further away, so when I asked for value it was showing me the value stored in said address. Was that the error i was committing more or less? – ShadowGeist

这几乎是正确的,除了 *bitCounter++ 产生存储在增量之前的地址的值,即。 e.在第一个循环中的原始 bitCounter 处,在第二个循环中的下一个地址处,依此类推。

关于c - fwrite() 不向文件写入任何内容 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com