使用来自外部文件的信息,如何正确地使用fread填充我的数组?

时间:2022-01-27 19:38:32

I need to be able to make sure my array is correctly receiving values from the file card.raw through fread.

我需要能够确保我的数组正确地从文件卡接收值。通过从文件中读生。

I am not confident about using arrays with pointers, so if anybody could help me with the theory here, it would be GREATLY appreciate it. Thanks in advance.

我对使用数组和指针不太有信心,所以如果有人能在这里帮助我,我将非常感激。提前谢谢。

The code is supposed to take literally one block of size 512 bytes and stick it into the array. Then I am just using a debugger and printf to examine the arrays output.

该代码应该只需要一个大小为512字节的块,并将其插入到数组中。然后我使用调试器和printf检查数组输出。

/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char* argv[])
{

    //Size of EACH FAT JPEG in bytes    
    #define FILESIZE 512   

    unsigned char* buffer[FILESIZE];

    ///Step 1: Open jpeg
    FILE* readfrom = fopen("card.raw", "rb");
    if (readfrom == NULL)
    {
        printf("Could not open");
    }

    ///Step 2: Find Beginning of JPEG.  The first digits will be 255216255 Then 224 or 225
    fread(&buffer, FILESIZE, 1, readfrom);
    for(int x = 0; x < FILESIZE; x++)
    {
        printf("%d = %c\n", x,  buffer[x]);
    }

    fclose(readfrom);
}

2 个解决方案

#1


0  

  1. Use return values from input functions. fread() reports how many elements were read - code might not have read 512. Swap FILESIZE, 1 to detect the number of characters/bytes read.

    使用来自输入函数的返回值。fread()报告读取了多少元素——代码可能没有读取512。交换FILESIZE, 1来检测字符/字节数。

    // fread(&buffer, FILESIZE, 1, readfrom);
    size_t count = fread(&buffer, 1, FILESIZE, readfrom);
    
  2. Only print out up to the number of elements read. Recommend hexadecimal (and maybe decimal) output rather than character.

    只打印出读取的元素数量。推荐十六进制(或十进制)输出而不是字符。

    for(size_t x = 0; x < count; x++) {
      // printf("%d = %c\n", x,  buffer[x]);
      printf("%3zu = %02X % 3u\n", x, buffer[x], buffer[x]);
    }
    
  3. If the fopen() failed, best to not continue with for() and fclose().

    如果fopen()失败,最好不要继续()和fclose()。

    if (readfrom == NULL) {
      printf("Could not open");
      return -1;
    }
    

#2


0  

The second parameter is size, in bytes, of each element to be read.
The third parameter is Number of elements each one with a size of the <second parameter> bytes.
So, swap your second and first parameters.

Replace unsigned char* buffer[FILESIZE]; with unsigned char buffer[FILESIZE];. For now, you have an array of unsigned char *, when you need unsigned char. Because buffer is already a pointer, you don't need to take its address. In fread call, replace &buffer with buffer.

It must go like this: fread(buffer, 1, FILESIZE, readfrom);

One more thing: add return with a specific error code after printf("Could not open");, because if file hasn't been open, you cannot read from it, can you? And add return 0; in the end of main.

And take your #define out of main.

Read more about fread here: http://www.cplusplus.com/reference/cstdio/fread/

第二个参数是要读取的每个元素的大小(以字节为单位)。第三个参数是元素的数量,每个元素的大小为 <第二个参数> 字节。所以,交换第二个和第一个参数。取代unsigned char *缓冲(文件大小);无符号字符缓冲区(文件大小);。现在,当需要无符号字符时,有一个未签名的char *数组。因为缓冲区已经是一个指针,所以不需要使用它的地址。在fread调用中,用缓冲区替换缓冲区。它必须像这样:fread(缓冲区,1,FILESIZE, readfrom);还有一件事:在printf(“不能打开”)之后添加一个特定的错误代码,因为如果文件没有打开,您就不能读取它,对吗?并添加返回0;在main的结尾。把你的#定义出主要的。阅读更多关于fread的信息:http://www.cplusplus.com/reference/cstdio/fread/。

#1


0  

  1. Use return values from input functions. fread() reports how many elements were read - code might not have read 512. Swap FILESIZE, 1 to detect the number of characters/bytes read.

    使用来自输入函数的返回值。fread()报告读取了多少元素——代码可能没有读取512。交换FILESIZE, 1来检测字符/字节数。

    // fread(&buffer, FILESIZE, 1, readfrom);
    size_t count = fread(&buffer, 1, FILESIZE, readfrom);
    
  2. Only print out up to the number of elements read. Recommend hexadecimal (and maybe decimal) output rather than character.

    只打印出读取的元素数量。推荐十六进制(或十进制)输出而不是字符。

    for(size_t x = 0; x < count; x++) {
      // printf("%d = %c\n", x,  buffer[x]);
      printf("%3zu = %02X % 3u\n", x, buffer[x], buffer[x]);
    }
    
  3. If the fopen() failed, best to not continue with for() and fclose().

    如果fopen()失败,最好不要继续()和fclose()。

    if (readfrom == NULL) {
      printf("Could not open");
      return -1;
    }
    

#2


0  

The second parameter is size, in bytes, of each element to be read.
The third parameter is Number of elements each one with a size of the <second parameter> bytes.
So, swap your second and first parameters.

Replace unsigned char* buffer[FILESIZE]; with unsigned char buffer[FILESIZE];. For now, you have an array of unsigned char *, when you need unsigned char. Because buffer is already a pointer, you don't need to take its address. In fread call, replace &buffer with buffer.

It must go like this: fread(buffer, 1, FILESIZE, readfrom);

One more thing: add return with a specific error code after printf("Could not open");, because if file hasn't been open, you cannot read from it, can you? And add return 0; in the end of main.

And take your #define out of main.

Read more about fread here: http://www.cplusplus.com/reference/cstdio/fread/

第二个参数是要读取的每个元素的大小(以字节为单位)。第三个参数是元素的数量,每个元素的大小为 <第二个参数> 字节。所以,交换第二个和第一个参数。取代unsigned char *缓冲(文件大小);无符号字符缓冲区(文件大小);。现在,当需要无符号字符时,有一个未签名的char *数组。因为缓冲区已经是一个指针,所以不需要使用它的地址。在fread调用中,用缓冲区替换缓冲区。它必须像这样:fread(缓冲区,1,FILESIZE, readfrom);还有一件事:在printf(“不能打开”)之后添加一个特定的错误代码,因为如果文件没有打开,您就不能读取它,对吗?并添加返回0;在main的结尾。把你的#定义出主要的。阅读更多关于fread的信息:http://www.cplusplus.com/reference/cstdio/fread/。