C语言读取ASCII图像文本到二维数组异常问题及解决
问题:ASCII图像文件读取异常,内容无法正确存储
我尝试将名为"ascii_image.txt"的ASCII图像文本文件读取到二维字符数组中,先用getAsciiImageDimension函数获取文件尺寸,再将图像存储到对应尺寸的二维字符数组array中,两个函数均采用逐字符处理方式。目前代码能正确识别文件尺寸,但无法正确存储文件内容。
ascii_image.txt内容如下:
Z ZZZ ZZZZZ
原代码
#include <stdio.h> #include <stdlib.h> #include <math.h> typedef unsigned int i32; // 32位无符号整数 void printTable(int rows, int columns, char table[rows][columns]) { for (int i = 0; i < rows; i = i + 1) { for (int j = 0; j < columns; j = j + 1) printf("%c", table[i][j]); printf("\n"); } printf("\n"); } int getAsciiImageDimension(i32* image_height, i32* image_width, const char* file_name) { FILE *image = fopen(file_name, "r"); int number_of_rows = 0; int row_length = 0; int max_row_length = 0; char character = (char) fgetc(image); while (character != EOF) { if (character == '\n') { number_of_rows = number_of_rows + 1; if (max_row_length < row_length) max_row_length = row_length; row_length = 0; } else row_length = row_length + 1; character = (char) fgetc(image); } *image_height = number_of_rows; *image_width = max_row_length; return 0; } void readAsciiImage(i32 image_height, i32 image_width, char array[image_height][image_width], const char* file_name) { FILE *image = fopen(file_name, "r"); i32 i = 0; i32 j = 0; char character = (char) fgetc(image); while (character != EOF) { array[i][j] = character; if (character == '\n') { while (j < image_width) { array[i][j] = ' '; j = j + 1; } i = i + 1; } else j = j + 1; character = (char) fgetc(image); } } int main() { int image_width; int image_height; getAsciiImageDimension(&image_height, &image_width, "ascii_image.txt"); printf("image_height = %d and image_width = %d \n", image_height, image_width); char array[image_height][image_width]; readAsciiImage(image_height, image_width, array, "ascii_image.txt"); printTable(image_height, image_width, array); // 数组内容错误 return 0; }
编辑1:输出异常说明
输出的第二行为空,无法理解该现象。
编辑2:可正常运行的代码(供参考)
#include <stdio.h> #include <stdlib.h> #include <math.h> void printTable(int rows, int columns, char table[rows][columns]) { for (int i = 0; i < rows; i = i + 1) { for (int j = 0; j < columns; j = j + 1) printf("%c", table[i][j]); printf("\n"); } printf("\n"); } int getAsciiImageDimension(unsigned int* image_height, unsigned int* image_width, const char* file_name) { FILE *image = fopen(file_name, "r"); // 检查文件是否成功打开 if (!image) { printf("无效文件\n"); return -1; } int number_of_rows = 0; int row_length = 0; int max_row_length = 0; char character = (char) fgetc(image); while (character != EOF) { if (character == '\n') { number_of_rows = number_of_rows + 1; if (max_row_length < row_length) max_row_length = row_length; row_length = 0; } else row_length = row_length + 1; character = (char) fgetc(image); } fclose(image); *image_height = number_of_rows; *image_width = max_row_length; return 0; } void readAsciiImage(unsigned int image_height, unsigned int image_width, char array[image_height][image_width], const char* file_name) { FILE *image = fopen(file_name, "r"); unsigned int i = 0; unsigned int j = 0; char character = (char) fgetc(image); while (character != EOF) { array[i][j] = character; if (character == '\n') { while (j < image_width) { array[i][j] = ' '; j = j + 1; } i = i + 1; j = 0; // 关键修复:换行后重置列索引j为0 } else j = j + 1; character = (char) fgetc(image); } fclose(image); } int main() { unsigned int image_width; unsigned int image_height; getAsciiImageDimension(&image_height, &image_width, "ascii_image.txt"); printf("image_height = %d and image_width = %d \n", image_height, image_width); char array[image_height][image_width]; readAsciiImage(image_height, image_width, array, "ascii_image.txt"); printTable(image_height, image_width, array); return 0; }
问题根源
原代码中readAsciiImage函数处理换行符时,仅递增行索引i,但未重置列索引j为0。导致换行后继续从当前j的位置开始写入,超出当前行的范围,进而破坏后续行的存储,出现第二行为空的异常。
内容的提问来源于stack exchange,提问作者3nondatur
相关产品推荐
相关产品推荐

