fork download
  1. #include <stdio.h>//第3講問2
  2. #include <stdlib.h>
  3. #define W 12
  4. #define H 10
  5.  
  6. char map[H][W];
  7.  
  8. void read_map(){
  9. int ch,x=0,y=0,count=0;
  10. FILE*fp;
  11. fp=fopen("map.txt","rt");
  12. if(fp==NULL){
  13. perror("ファイルを開けない.");
  14. exit(1);
  15. }
  16. while( (ch=fgetc(fp)) != -1){
  17. if(ch==' '){
  18. map[y][x]=0;
  19. count++;
  20. x++;
  21. }
  22. else if(ch=='#'){
  23. map[y][x]=1;
  24. count++;
  25. x++;
  26. }
  27. else if(ch=='G'){
  28. map[y][x]=2;
  29. count++;
  30. x++;
  31. }
  32. else if(ch=='\n'){
  33. }
  34. else{
  35. printf("invalid data.(%d,%d) %d %c\n",x,y,ch,ch);
  36. exit(1);
  37. }
  38. if( x == W ){ y ++; x=0; }
  39. }
  40. if(count<120){
  41. printf("mapが足りない\n");
  42. exit(1);
  43. }
  44. fclose(fp);
  45. }
  46.  
  47. void print_map(){
  48. int y,x;
  49. for(y=0;y<H;y++){
  50. for(x=0;x<W;x++){
  51. if(map[y][x]==0)printf(" ");
  52. else if(map[y][x]==1)printf("#");
  53. else if(map[y][x]==2)printf("G");
  54. else if(map[y][x]==3)printf(".");
  55. }
  56. printf("\n");
  57. }
  58.  
  59. }
  60.  
  61. void main(){
  62. read_map();
  63. print_map();
  64. }
  65.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty