C语言:从文件读取十六进制以填充数组

时间:2018-01-21 15:43:40

标签: c hex fgets

我目前正在制作加密/解密程序。加密后,结果存储在文本文件中,每个字符都存储为十六进制值。 我目前正在进行解密,第一阶段是读入该文件,并将每个十六进制值存储为数组中的元素。

FILE * decryptIn = fopen("output.txt", "r");
fseek(decryptIn, 0L, SEEK_END); //counts the amount of bytes from start to end of file
int fileSize = ftell(decryptIn); //stores the size of the file in bytes
rewind(decryptIn); //sets offset back to 0 (back to start of file)

int *decryptHexArray = malloc(sizeof(int)*5*fileSize);
int currentPointer;
int counter = 0;

while(fgets(decryptHexArray[counter], fileSize, decryptIn)) //loop that reads each string from the file
{
    counter++;
}

我收到的错误消息是

  

传递' fgets'的参数1从没有a的整数生成指针   投

是否有可能通过fgets达到我想要的效果?

1 个答案:

答案 0 :(得分:3)

char *fgets(char * restrict s, int n,FILE * restrict stream);但是你传递的是int ..这就是抱怨的原因。它甚至说它是 将传递的int类型视为char*,但未提供明确的转换。所以它提出了警告。

您可以先读取char数组中的数字,然后使用strto*获取转换后的int