2015-07-03 73 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; 
} 

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