Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFFER_SIZE 1024 * 16
- int main(int argc, char *argv[]) {
- if(argc == 1) {
- fprintf(stderr, "Chybi cesta ke vstupnimu a vystupnimu souboru.");
- exit(1);
- } else if(argc == 2) {
- fprintf(stderr, "Chybi cesta k vystupnimu souboru.");
- exit(1);
- }
- FILE* input = fopen(argv[1], "r");
- FILE *output = fopen(argv[2], "w");
- char line[BUFFER_SIZE];
- int y = -1;
- int width;
- int height;
- int **arr;
- if (input == NULL)
- {
- fprintf(stderr, "Vstupni soubor \"%s\" neexistuje", argv[1]);
- exit(1);
- }
- while (fgets(line, BUFFER_SIZE, input))
- {
- char* tmp = strdup(line);
- int x = 0;
- const char* tok;
- for (tok = strtok(line, " ");
- tok && *tok;
- tok = strtok(NULL, " \n"))
- {
- if(y == -1) {
- if(x == 0) {
- width = strtol(tok, NULL, 10);
- } else if(x == 1) {
- height = strtol(tok, NULL, 10);
- arr = (int **)malloc(height * sizeof(int *));
- for (int i=0; i<height; i++)
- arr[i] = (int *)malloc(width * sizeof(int));
- }
- }
- //printf("%d ", strtol(tok, NULL, 10));
- if(y != -1) {
- arr[x][y] = strtol(tok, NULL, 10);
- }
- x++;
- }
- free(tmp);
- y++;
- }
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- if(j == 0) {
- fprintf(output, "%d", arr[i][j]);
- } else if(j == width - 1) {
- fprintf(output, " %d\n", arr[i][j]);
- } else {
- fprintf(output, " %d", arr[i][j]);
- }
- }
- }
- fclose(input);
- fclose(output);
- for (int i = 0; i < height; i++)
- {
- int* currentIntPtr = arr[i];
- free(currentIntPtr);
- }
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement