Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7. FILE *input;
  8. input = fopen("C:\\Users\\zatovicovci\\Desktop\\Martin\\colours.txt", "r");
  9. FILE *output;
  10. output = fopen("C:\\Users\\zatovicovci\\Desktop\\Martin\\colours_RGB.txt", "w");
  11.  
  12. if (input == NULL) {
  13. printf("Cannot open the selected FILE.\n");
  14. }
  15.  
  16. int CountLines = 0;
  17. int ValidLines = 0;
  18. int i = 0;
  19. int see = 1;
  20. char read[256];
  21. char copy[256];
  22.  
  23. char red[3];
  24. char green[3];
  25. char blue[3];
  26.  
  27. long int red_int, green_int, blue_int;
  28.  
  29. while(fgets(read, sizeof(read), input) != NULL)
  30. {
  31. if(read == '\n')
  32. {
  33. continue;
  34. }
  35.  
  36. CountLines += 1;
  37. int len = strlen(read);
  38.  
  39. if (len > 0 && read[len - 1] != '\n')
  40. {
  41. read[len] = '\n';
  42. strncpy(copy, read, len);
  43. }
  44. else
  45. {
  46. strncpy(copy, read, strlen(read));
  47. copy[strlen(copy) - 1] = '\0';
  48. }
  49.  
  50. if(strlen(read) < 7)
  51. {
  52. continue;
  53. }
  54.  
  55. if(strlen(read) > 7)
  56. {
  57. continue;
  58. }
  59.  
  60. i = 0;
  61.  
  62. for(i = 0; i <= 6; i += 1)
  63. {
  64. if((read[i] >= '0' && read[i] <= '9') || (read[i] >= 'a' && read[i] <= 'f') || (read[i] >= 'A' && read[i] <= 'F'))
  65. {
  66. ValidLines += 1;
  67.  
  68. red[0] = read[0];
  69. red[1] = read[1];
  70. green[0] = read[2];
  71. green[1] = read[3];
  72. blue[0] = read[4];
  73. blue[1] = read[5];
  74. see = 1;
  75. break;
  76. }
  77. else
  78. {
  79. printf("\nThe line %s contains invalid characters.", copy);
  80. see = 0;
  81. break;
  82.  
  83. }
  84. }
  85.  
  86. red[2] = '\0';
  87. green[2] = '\0';
  88. blue[2] = '\0';
  89.  
  90. if(see == 1)
  91. {
  92. red_int = (int)strtol(red, NULL, 16);
  93. green_int = (int)strtol(green, NULL, 16);
  94. blue_int = (int)strtol(blue, NULL, 16);
  95.  
  96. fprintf(output, "RGB code of the hexadecimal code %s is: %i %i %i.\n", copy, red_int, green_int, blue_int);
  97. }
  98. }
  99. printf("\n\nThe number of occupied lines is: %i", CountLines);
  100. printf("\n\nThe number of valid lines is: %i", ValidLines);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement