将二进制文件读入同一个后Unsigned Char为(null)
Unsigned Char is (null) after reading the binary file into the same
我必须 open/read 一个文件,它是一个 ascii 艺术文件(图像)
并要求我将return图像的宽度和高度传递给主程序,然后要求我通过指针将图片数据传回。这是我必须使用的函数原型:
unsigned char *foo(char *filename, int *width, int *height)
在 foo 内部,我必须使用动态字符数组
存储tha图像数据。我需要使用 fread() 来阅读
那个数据。我还必须考虑每行末尾的回车 return。
打开并读取数据后,将其传回主程序。然后主例程必须创建一个动态二维数组来存储图像,复制一维数组
进入二维数组,在屏幕上显示图像。
图像文件名:data.txt
我的代码:
#include <stdio.h>
#include <stdlib.h>
void readDimension(FILE *inFile, int *width, int *height)
{
int i;
for (i = 0; i < 2; i++)
{
if (i == 0)
{
fscanf(inFile, "%d", width);
}
if (i == 1)
{
fscanf(inFile, "%d", height);
}
}
}
unsigned char *foo(char *filename, int *width, int *height)
{
FILE *inFile = fopen(filename, "rb");
readDimension(inFile, width, height);
unsigned char *ret = malloc(*width * *height);
fread(ret, 1, *width * *height, inFile);
fclose(inFile);
return ret;
}
int main(int argc, char** argv)
{
FILE *inFile;
int width, height;
unsigned char art;
if (argc == 1)
{
printf("Please specify a file name.\n");
}
else if (argc == 2)
{
inFile = fopen(argv[1], "rb");
if (inFile != NULL)
{
fclose(inFile);
art = foo(argv[1], &width, &height);
int n = sizeof(art);
printf("Data in Array: \%c \n", art);
printf("Size of Array: %d \n", n);
}
else
{
printf("Error: File Not Found %s", argv[1]);
}
}
printf("Width: %d\n", width); // Testing
printf("Height: %d\n", height); // Testing
}
问题是您正在尝试获取 art
的大小,这是一个指针。它有固定的尺寸。大小应计算为 width*height
:
printf("Size of Array: %d x %d (%d)\n", width, height, width*height);
您将 art
声明为 unsigned char
,但您正在为其分配一个指向 unsigned char 的指针。这是不正确的:art
应该用星号声明。
您还在传递数组 art
时打印单个字符 %c
。这不会打印任何感兴趣的内容。如果要打印第一个字符,请打印 art[0]
。如果你想打印整张图片,做一对嵌套循环遍历 width
和 height
,打印每个字符,并在每行后打印 '\n'
。
这里有一些补充说明:
- 您不应该使用
fread
一次读取整个文件,因为它会在数据中引入 \n
s。相反,您应该逐行阅读文件。
- 您可以在读取数据的同一个函数中读取宽度和高度
- 您不需要循环来读取两个整数。您可以在一行中完成,如下所示:
fscanf(inFile, "%d %d", width, height);
我必须 open/read 一个文件,它是一个 ascii 艺术文件(图像) 并要求我将return图像的宽度和高度传递给主程序,然后要求我通过指针将图片数据传回。这是我必须使用的函数原型:
unsigned char *foo(char *filename, int *width, int *height)
在 foo 内部,我必须使用动态字符数组 存储tha图像数据。我需要使用 fread() 来阅读 那个数据。我还必须考虑每行末尾的回车 return。
打开并读取数据后,将其传回主程序。然后主例程必须创建一个动态二维数组来存储图像,复制一维数组 进入二维数组,在屏幕上显示图像。
图像文件名:data.txt
我的代码:
#include <stdio.h>
#include <stdlib.h>
void readDimension(FILE *inFile, int *width, int *height)
{
int i;
for (i = 0; i < 2; i++)
{
if (i == 0)
{
fscanf(inFile, "%d", width);
}
if (i == 1)
{
fscanf(inFile, "%d", height);
}
}
}
unsigned char *foo(char *filename, int *width, int *height)
{
FILE *inFile = fopen(filename, "rb");
readDimension(inFile, width, height);
unsigned char *ret = malloc(*width * *height);
fread(ret, 1, *width * *height, inFile);
fclose(inFile);
return ret;
}
int main(int argc, char** argv)
{
FILE *inFile;
int width, height;
unsigned char art;
if (argc == 1)
{
printf("Please specify a file name.\n");
}
else if (argc == 2)
{
inFile = fopen(argv[1], "rb");
if (inFile != NULL)
{
fclose(inFile);
art = foo(argv[1], &width, &height);
int n = sizeof(art);
printf("Data in Array: \%c \n", art);
printf("Size of Array: %d \n", n);
}
else
{
printf("Error: File Not Found %s", argv[1]);
}
}
printf("Width: %d\n", width); // Testing
printf("Height: %d\n", height); // Testing
}
问题是您正在尝试获取 art
的大小,这是一个指针。它有固定的尺寸。大小应计算为 width*height
:
printf("Size of Array: %d x %d (%d)\n", width, height, width*height);
您将 art
声明为 unsigned char
,但您正在为其分配一个指向 unsigned char 的指针。这是不正确的:art
应该用星号声明。
您还在传递数组 art
时打印单个字符 %c
。这不会打印任何感兴趣的内容。如果要打印第一个字符,请打印 art[0]
。如果你想打印整张图片,做一对嵌套循环遍历 width
和 height
,打印每个字符,并在每行后打印 '\n'
。
这里有一些补充说明:
- 您不应该使用
fread
一次读取整个文件,因为它会在数据中引入\n
s。相反,您应该逐行阅读文件。 - 您可以在读取数据的同一个函数中读取宽度和高度
- 您不需要循环来读取两个整数。您可以在一行中完成,如下所示:
fscanf(inFile, "%d %d", width, height);