Hey everyone,
I'm working on writing a program that reads in an ASCII image (an image made from clever placement of characters) from a file called image.txt and prints it to the screen. When compiling the code I receive no error messages, but when I attempt to run the program I receive a "segmentation fault: 11" message. If someone could explain what I'm doing wrong I would really appreciate it.
I've attached the ASCII image I'm currently working with to this post. It's dimensions are 65 rows by 129 columns (given in the image). I want to make the maximum size image that this program can deal with 250x250. Thanks in advance!
Code:
#include <stdio.h>
void printarray(char array[250][250], int rows, int cols);
int main()
{
int rowlength, collength, i, j;
char character;
char array[rowlength][collength];
FILE *ifp;
ifp = fopen("image.txt", "r");
if (ifp == NULL)
{
printf("Could not open numbers.txt\n");
}
else
{
printf("Opened file image.txt sucessfully\n");
fscanf(ifp, "%d", &rowlength);
fscanf(ifp, "%d", &collength);
for (i = 0 ; i < rowlength ; i++)
{
for (j = 0 ; j < collength ; j++)
{
fscanf(ifp, "%c", &character);
array[i][j] = character;
}
printf("\n");
}
printarray(array, rowlength, collength);
}
return 0;
}
void printarray(char array[250][250], int rows, int cols)
{
int i, j;
printf("\n");
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < cols ; j++)
{
printf("%c", array[i][j]);
}
printf("\n");
}
printf("\n");
return;
}