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 *file = fopen("input.txt", "r");
- int length;
- char *buffer;
- int *numericArray;
- if (file) {
- fseek(file, 0, SEEK_END);
- length = ftell(file);
- buffer = malloc(length);
- if (buffer == NULL) {
- return EXIT_FAILURE;
- }
- fseek(file, 0, SEEK_SET);
- fread(buffer, 1, length, file);
- fclose(file);
- char *bufferCopy;
- strcpy(bufferCopy, buffer);
- char *nextNumber = strtok(bufferCopy, ",");
- int arrayLength = 0;
- while (nextNumber != NULL) {
- nextNumber = strtok(NULL, ",");
- arrayLength++;
- }
- numericArray = malloc(sizeof(int) * arrayLength);
- if (numericArray == NULL) {
- return EXIT_FAILURE;
- }
- int i = 0;
- nextNumber = strtok(buffer, ",");
- while (nextNumber != NULL) {
- numericArray[i] = atoi(nextNumber);
- nextNumber = strtok(NULL, ",");
- i++;
- }
- for (i = 0; i < arrayLength; i++) {
- int j;
- for (j = i + 1; j < arrayLength; j++) {
- if (compare(numericArray[i], numericArray[j])) {
- int k;
- for (k = j; k < arrayLength - 1; k++) {
- numericArray[k] = numericArray[k+1];
- }
- arrayLength--;
- numericArray = realloc(numericArray, sizeof(int) * arrayLength);
- if (numericArray == NULL) {
- return EXIT_FAILURE;
- }
- }
- }
- }
- file = fopen("output.txt", "w");
- for (i = 0; i < arrayLength; i++) {
- fprintf(file, "%d", numericArray[i]);
- if (i < arrayLength - 1) {
- fprintf(file, ",", numericArray[i]);
- }
- }
- fclose(file);
- }
- return EXIT_SUCCESS;
- }
- int compare(int a, int b) {
- if (a == b) {
- return 1;
- }
- else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement