Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- FILE *input;
- input = fopen("C:\\Users\\zatovicovci\\Desktop\\Martin\\colours.txt", "r");
- FILE *output;
- output = fopen("C:\\Users\\zatovicovci\\Desktop\\Martin\\colours_RGB.txt", "w");
- if (input == NULL) {
- printf("Cannot open the selected FILE.\n");
- }
- int CountLines = 0;
- int ValidLines = 0;
- int i = 0;
- int see = 1;
- char read[256];
- char copy[256];
- char red[3];
- char green[3];
- char blue[3];
- long int red_int, green_int, blue_int;
- while(fgets(read, sizeof(read), input) != NULL)
- {
- if(read == '\n')
- {
- continue;
- }
- CountLines += 1;
- int len = strlen(read);
- if (len > 0 && read[len - 1] != '\n')
- {
- read[len] = '\n';
- strncpy(copy, read, len);
- }
- else
- {
- strncpy(copy, read, strlen(read));
- copy[strlen(copy) - 1] = '\0';
- }
- if(strlen(read) < 7)
- {
- continue;
- }
- if(strlen(read) > 7)
- {
- continue;
- }
- i = 0;
- for(i = 0; i <= 6; i += 1)
- {
- if((read[i] >= '0' && read[i] <= '9') || (read[i] >= 'a' && read[i] <= 'f') || (read[i] >= 'A' && read[i] <= 'F'))
- {
- ValidLines += 1;
- red[0] = read[0];
- red[1] = read[1];
- green[0] = read[2];
- green[1] = read[3];
- blue[0] = read[4];
- blue[1] = read[5];
- see = 1;
- break;
- }
- else
- {
- printf("\nThe line %s contains invalid characters.", copy);
- see = 0;
- break;
- }
- }
- red[2] = '\0';
- green[2] = '\0';
- blue[2] = '\0';
- if(see == 1)
- {
- red_int = (int)strtol(red, NULL, 16);
- green_int = (int)strtol(green, NULL, 16);
- blue_int = (int)strtol(blue, NULL, 16);
- fprintf(output, "RGB code of the hexadecimal code %s is: %i %i %i.\n", copy, red_int, green_int, blue_int);
- }
- }
- printf("\n\nThe number of occupied lines is: %i", CountLines);
- printf("\n\nThe number of valid lines is: %i", ValidLines);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement