fork download
  1. #define WINDOWS_OS !defined(__linux__) && !defined(__unix__) && !defined(__APPLE__)
  2.  
  3. #if WINDOWS_OS
  4. # include <fcntl.h>
  5. # include <io.h>
  6. #endif
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #if WINDOWS_OS
  11. # if defined(__BORLANDC__)
  12. # define Setmode(f,t) setmode((f),(t))
  13. # define Fileno(f) fileno(f)
  14. # define O_Binary O_BINARY
  15. # else
  16. # define Setmode(f,t) _setmode((f),(t))
  17. # define Fileno(f) _fileno(f)
  18. # define O_Binary _O_BINARY
  19. # endif
  20. #else
  21. # define Setmode(f,t) 0
  22. #endif
  23. #define ArrLen(arr) (sizeof(arr) / sizeof(arr[0]))
  24.  
  25. /* CRコードをカウントする。 */
  26.  
  27. int main(int argc, char *args[]) {
  28.  
  29. int ch;
  30. int count = 0;
  31. char data[100];
  32. int i, sz, md;
  33. FILE *file;
  34.  
  35. if (argc == 2) {
  36. file = fopen(args[1], "rb");
  37. if (file == NULL) {
  38. perror(NULL);
  39. exit(EXIT_FAILURE);
  40. }
  41. } else if (argc > 2) {
  42. printf("arguments are 0 or 1.\n");
  43. return 0;
  44. } else {
  45. md = Setmode(Fileno(stdin), O_Binary);
  46. if (md == -1) {
  47. perror(NULL);
  48. exit(EXIT_FAILURE);
  49. }
  50. file = stdin;
  51. }
  52.  
  53.  
  54. while ((sz = fread(data, sizeof(data[0]), ArrLen(data), file)) != 0) {
  55. for (i = 0; i < sz; ++i) {
  56. if (data[i] == 0x0D) {
  57. ++count;
  58. }
  59. }
  60. }
  61.  
  62. printf("Count CR Code (0x0D): %d\n", count);
  63.  
  64. if (file != stdin) {
  65. fclose(file);
  66. }
  67.  
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 2296KB
stdin
おお!
CRカウントでけた!
stdout
Count CR Code (0x0D): 0