包含未知项的多维数组

时间:2015-07-03 23:50:56

标签: c arrays multidimensional-array console

#include <stdio.h>
#include <stdlib.h>

int main() {

int *width;
int *height;
int row;
int column;
int character;
int count;
int pictureit;
double i = 0;
FILE *fp;


char file[50]; 
char line[25]; // assume each line has max 25 characters 

printf("What file should we pull from: ");
scanf("%s", file);

//read file using File pointer

fp = fopen(file, "r");

// read the first line in the file 
fgets(line, sizeof(line), fp);

width = strtok(line,"x");
height = strtok(NULL, "/0");


// read all the future lines in the file excluding the first 
while (fgets(line, sizeof(line), fp)) {

row = strtok(line, ",");
column = strtok(NULL, ",");
character = strtok(NULL, ",");
count = strtok(NULL, "/0");


if(i < count) {


**printf("%s", pictureit[row][column] = character);**

i++;

}
}
fclose(fp);

return 0;
}

我用这种设置拉入文件

75x53
0,36,.,1
0,37,M,1
1,32,.,1
1,33,:,1
1,34,A,1
1,35,M,2
1,37,O,1
1,38,:,1
2,23,.,1
2,24,:,1
2,25,A,1
2,26,M,5

我已经集思广益了一段时间。我如何在控制台上显示它?它显然需要进入二维数组。程序需要知道数组的高度和宽度,以在该位置显示空格或字符。

PS:完成后,该程序将在控制台中显示一张图片。 &#34; ** **&#34;是我工作的地方。

2 个答案:

答案 0 :(得分:4)

你可以动态分配一个具有正确尺寸的二维数组(根据你的第一行),然后用文件中的数据填充它,最后用两个嵌套的for循环打印它。

编辑:基本上,你会这样做:

//...

//Create the dynamic array
char ** array = malloc(sizeof(char) * height);
int i;
for(i = 0; i < height; i++)
    array[i] = malloc(sizeof(char) * width);

// Fill your array here (put different chars in it) ...

//Print it
int x,y;
for(y = 0; y < height; y++)
{
    for(x = 0; x < width; x++)
        printf("%c ", array[y][x]);

    printf("\n");
}

//Free the array
for(i = 0; i < height; i++)
    free(array[i]);
free(array);

我自愿省略了检查malloc返回值的部分(无论是否为NULL),但你一定要添加它。

答案 1 :(得分:2)

通常我不会这样做,但我觉得有必要进行扫描练习:

int main(void)
{
  char fn[100];
  fprintf(stdout, "Enter file name:");
  fflush(stdout);
  int result = fscanf(stdin, " %99s", fn);
  if (1 != result)
  {
    fprintf(stderr, "Reading the file's name failed.\n");
    exit(EXIT_FAILURE);
  }

  {
    size_t width= 0;
    size_t height 0;

    FILE * pf = fopen(fn, "r");
    if (NULL == pf)
    {
      fprintf(stderr, "Opening file '%s' failed.\n", fn);
      exit(EXIT_FAILURE);
    }

    {
      result = fscanf(pf, " %zux%zu", &width, &height);
      if (2 != result)
      {
        fprintf(stderr, "Reading width and/or heigth from file '%s' failed.\n", fn);
        exit(EXIT_FAILURE);
      }

      {
        char (*pa)[width][height] = calloc(1, sizeof *pa);
        if (NULL == pa)
        {
          perror("calloc() failed");
          exit(EXIT_FAILURE);
        }

        {
          size_t number_of_rows = width * height;

          fprintf(stderr, "Trying to read %zu data rows.\n", number_of_rows);

          for (size_t row = 0; row < number_of_rows; ++row)
          {
            size_t x, y;
            char c;
            int i;
            result = fscanf(pf, " %zu,%zu,%c,%d", &x, &y, &c, &i);
            if (4 != result)
            {
              fprintf(stderr, "Reading data (#%zu) row from '%s' failed.\n", row, fn);
              exit(EXIT_FAILURE);
            }

            /* Add check here to avoid accessing the array out-of-bounds! */
            (*pa)[x][y] = c;
          }
        }

        {
          for (size_t row = 0; row < width; ++row)
          {
            for (size_t column = 0; column < height; ++column)
            {
              fprintf(stdout, "%c", (*pa)[row][column]);
            }

            fprintf(stdout, "\n");
          }
        }

        free(pa);
      }
    }

    fclose(pf);
  }

  return EXIT_SUCCESS;
}

我也很好奇要打印的图片......; - )