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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable:4996)
main(void)
{
FILE *gridinput;
FILE *gridoutput;
char filenamein[] = "shortgrid.txt";
char filenameout[] = "results.txt";
#define GRID_ROWS 20
#define GRID_COLUMNS 10
int grid[GRID_ROWS][GRID_COLUMNS];
if ((gridinput = fopen(filenamein, "r")) == NULL)
{
fprintf(stderr, "Filename, %s, could not be open for read\n", gridinput);
exit(-1);
}
if ((gridoutput = fopen(filenameout, "w")) == NULL)
{
fprintf(stderr, "Filename, %s, could not be open for write\n", gridoutput);
exit(-1);
}
int data;
unsigned int rowcount;
unsigned int columncount;
rowcount = 0;
columncount = 0;
while (rowcount < GRID_ROWS && columncount < GRID_COLUMNS)
{
if ((data = fgetc(gridinput)) != '\n')
{
grid[rowcount][columncount] = data - '0';
if (++columncount == GRID_COLUMNS && ++rowcount < GRID_ROWS)
columncount = 0;
}
}
for (rowcount = 0; rowcount < GRID_ROWS; rowcount++)
{
for (columncount = 0; columncount < GRID_COLUMNS; columncount++)
{
printf("%d",grid[rowcount][columncount]);
fprintf(gridoutput, "%d", grid[rowcount][columncount]);
}
printf("\n");
fprintf(gridoutput, "\n");
}
}
|