C语言 : read in hex from file to populate array

标签 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++;
}

我得到的错误信息是

passing argument 1 of 'fgets' makes pointer from integer without a cast

是否可以使用 fgets 实现我想要的?

最佳答案

char *fgets(char * restrict s, int n,FILE * restrict stream); 但是您正在向它传递 int.. 这就是提示的原因。它甚至说它是 将传递的 int 类型视为 char* 但未提供显式转换。所以它提出了警告。

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

关于C语言 : read in hex from file to populate array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48368558/

相关文章:

c - 在 fork 中写入和读取 - C

c - 在 C linux 中使用管道的 2 路通信

在 bash 脚本上调用 execve bash 找不到参数

c - fatal error : stdio. h 没有这样的文件或目录

typescript - 如何为十六进制颜色字符串创建独立类型?

c - popen 的用途是什么?为什么 fdopen 和 fgets 在某些情况下工作得更好?

c - 安装SIGTSTP前台进程

C:将参数从可变参数函数传递给可变参数宏

Python字符串删除十六进制代码,如 'president trump\\xe2\\x80\\x99s'

c - 解释数据表中的十六进制并使用 C 按位​​左移运算符进行转换