gpt4 book ai didi

c - 未检测到 EOF

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:56 27 4
gpt4 key购买 nike

我希望这不是一个问题。我检测 EOF 的条件似乎不起作用。代码继续通过 EOF 并在语句中处理。当我重新创建文本时,它看起来很合适,但是整个带有垃圾代码的 bmp 也打印出来告诉我文本标志的结尾从未被编码。我在下一个 else 条件中放置了一个 printf 语句,但它从未输入要打印的语句。我看不出问题是什么,如果它就在我面前,或者更不祥的东西。一如既往的感谢!

/*******************************************************************************
* This code is to take a text document and using steganography techniques, hide
* the text within a bmp. It will take each character of the text, parse it into
* four 2 bit pieces and inject those bits into the two least significant bits
* of each pixel color (BGR) byte as well as the line padding.
******************************************************************************/
#include <stdio.h>

/*******************************************************************************
* getIntFromArray (borrowed from class notes). Takes unsigned character array
* and assembles/returns an int value using bit shifting with OR.
******************************************************************************/
int getIntFromArray(unsigned char bytes[])
{
int n =
bytes[0] |
bytes[1] << 8 |
bytes[2] << 16 |
bytes[3] << 24;
return n;
}

/*******************************************************************************
* bitWise. Take unsigned char pointer and character, parses the character
* using bitwise manipulation and injects 2 bits into the 2 least significant
* bits of each pixel color byte as well as padding.
******************************************************************************/
void bitWise(unsigned char* bytes, char character)
{
int i;
char tmpChar;
for(i = 0; i < 4; ++i)
{
tmpChar = character;
tmpChar &= 3;
bytes[i] &= 252;
bytes[i] |= tmpChar;
character = character >> 2;
}
}

int flag = 0;

int main(int argc, char **argv)
{
char *infilename = argv[1];
char *outfilename = argv[2];

unsigned char header[54];

FILE *in = fopen(infilename, "rb");/*Command line input.*/
FILE *out = fopen(outfilename, "wb");/*Command line input.*/

int pixelWidth;
int pixelHeight;
int i;
int j;

fread(header, 1, 54, in);/* read header into array */

pixelWidth = getIntFromArray(&header[18]);
pixelHeight = getIntFromArray(&header[22]);

fwrite(header, 1, sizeof(header), out);/* write header to output file */

for(i = 0; i < pixelHeight; ++i)/*Loop to read pixel data from bmp.*/
{
for(j = 0; j < pixelWidth; ++j)
{
unsigned char bytes[4];
unsigned char character = 0;

fread(&bytes, 1, 4, in);/*Reads sequentially pixel and padding bytes.*/
if(flag == 0)/*Breakout flag, initially set to 0.*/
{
character = getchar();/*Takes in characters from stdin.*/
if(character != EOF)/*Breakout if EOF.*/
{
bitWise(bytes, character);
}
else
{
bitWise(bytes, 0);/*Sets end of hidden text with 4 bytes LSB to 0.*/
flag = 1;
}
}
fwrite(&bytes, 1, 4, out);
}
}
fclose(in);
fclose(out);
return 0;
}

最佳答案

您正在将 signed int 分配给 unsigned int。结果将不是您期望的那样。它将是一个所有位都设置为 1 的值。 (EOF 的值为 -1,因此已签名)。

长话短说,它应该是 int。简单的 int character 就可以达到目的。

另外 getchar() 返回 intint getchar(void);

还有一些其他事情要做:-

  • fread 应检查返回值。

    size_t fread(void * restrict ptr,size_t size, size_t nmemb,FILE * restrict stream);

The fread function returns the number of elements successfully read, which may be less than nmemb if a read error or end-of-file is encountered. If size or nmemb is zero, fread returns zero and the contents of the array and the state of the stream remain unchanged

  • 另一件事是检查fopen() 的返回值。如果失败,返回值将为 NULL

关于c - 未检测到 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447925/

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