2015-07-03 52 views
2
#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:该程序完成后将在控制台中显示一张图片。 “** **”是我工作的地方。

+0

请盟友适当的格式化! – Olaf

+0

我没有看到任何git拉。你什么意思? – Olaf

+0

[如何将序列号(例如:0,36,。,1)更改为项目(例如:行,列,字符,计数)](http://stackoverflow.com/questions/31214251/how- to-change-sequential-numbers-ex-0-36-1-items-ex-row-column-char) – Olaf

回答

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的返回值(无论是空或不)的一部分,但你一定要添加。

+0

为什么一个(复杂的)“散射阵列”,其实不是一个真正的2D阵列,而是一个1D指针阵列?一个简单的char(* p)[width] [heigth] = malloc(sizeof * p);'也可以。作为'(* p)[x] [y] ='。';'访问元素。 – alk

+0

假设> = C99就像'char a [width] [height]一样简单;'不需要动态分配。 – alk

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; 
} 

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