fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void load_image_data(char* f_name)
  5. {
  6. FILE* fp;
  7. char buf[640];
  8. errno_t err;
  9. int x_size, y_size;
  10. int max_gray;
  11. int ix, iy;
  12.  
  13. err = fopen_s(&fp, f_name, "rb");
  14. if (err) {
  15. fprintf(stderr, "fopen_s\n");
  16. return;
  17. }
  18.  
  19. // P5
  20. fgets(buf, sizeof buf, fp);
  21. if (buf[0] != 'P' || buf[1] != '5') {
  22. fprintf(stderr, "not P5\n");
  23. return;
  24. }
  25.  
  26. // x y
  27. x_size = y_size = 0;
  28. while (x_size == 0) {
  29. fgets(buf, sizeof buf, fp);
  30. if (buf[0] == '#') continue;
  31. sscanf_s(buf, "%d%d", &x_size, &y_size);
  32. }
  33.  
  34. // max
  35. max_gray = 0;
  36. while (max_gray == 0) {
  37. fgets(buf, sizeof buf, fp);
  38. if (buf[0] == '#') continue;
  39. sscanf_s(buf, "%d", &max_gray);
  40. }
  41.  
  42. printf("\n%s\n", f_name);
  43. printf("%d %d\n", x_size, y_size);
  44. printf("%d\n", max_gray);
  45.  
  46. for (iy = 0; iy < y_size; iy++) {
  47. for (ix = 0; ix < x_size; ix++) {
  48. printf("%02x", fgetc(fp));
  49. }
  50. printf("\n");
  51. }
  52.  
  53. fclose(fp);
  54. }
  55.  
  56. int main()
  57. {
  58. char f_name[256];
  59. int i;
  60.  
  61. for (i = 0; i < 10; i++) {
  62. sprintf_s(f_name, _countof(f_name), "std%d.pgm", i);
  63. load_image_data(f_name);
  64. }
  65. for (i = 0; i < 10; i++) {
  66. sprintf_s(f_name, _countof(f_name), "n%d.pgm", i);
  67. load_image_data(f_name);
  68. }
  69. load_image_data("number2.pgm");
  70. return 0;
  71. }
  72.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty