1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char **argv)
{
if(argc != 3)
{
printf("Incorrect number of arguments.\n");
exit(1);
}
//Open files
FILE *fin;
fin = fopen(argv[1], "r");
if(fin ==NULL)
{
printf("File: %s does not exist\n", argv[1]);
exit(1);
}
FILE *fout;
fout = fopen(argv[2], "w");
if(fout ==NULL)
{
printf("File: %s does not exist\n", argv[2]);
exit(1);
}
int i=0;
char p2[3]; //P2
int row_quantity; //Number of rows
int column_quantity; //Number of columns
int current_row; //Current row
int current_column; //Current column
int pixel; //255
//Read in non pixels
fscanf(fin, "%s", p2);
fscanf(fin, "%d", &column_quantity);
fscanf(fin, "%d", &row_quantity);
fscanf(fin, "%d", &pixel);
//Print out non-pixels
fprintf(fout, "%s\n", p2);
fprintf(fout, "%d %d\n", column_quantity, row_quantity);
fprintf(fout, "%d\n", pixel);
//Allocate memory
int **array = (int **)malloc(sizeof(int*) *(row_quantity));
//Read in pixels
for(current_row = 0; current_row < row_quantity; current_row++)
{
for(current_column =0; current_column < column_quantity; current_column++)
{
array[current_column] = (int *)malloc(sizeof(int) *(column_quantity));
fscanf(fin, "%d", &array[current_row][current_column]);
}
}
//Print with vertical flip
for(current_row = row_quantity-1; current_row >= 0; current_row--)
{
for(current_column = 0; current_column < column_quantity; current_column++)
{
fprintf(fout, "%d ", array[current_row][current_column]);
}
fprintf(fout, "\n");
}
return 0;
}
|