Thanks Dave, that worked. I can't figure out what is different about what you did and what I did but your works. I've now tried to put that piece into my progressing code and I'm getting the same problem that I got before. When I output the contents of the array to stdout I get a bunch of jumbled signs as output.
Here's my code:
Code:
#include <stdio.h>
int main()
{
int c, i, j, row, col, nl, cr;
row = col = nl = cr = 0;
FILE *fp = fopen("cashiers.txt", "r");
// Figure out how many rows and columns the text file has
while ((c = getc(fp)) != EOF)
{
if (c == '\n')
nl++;
if (c == '\r')
cr++;
col++;
if (c == '\n')
row++;
putchar(c);
}
col = (col - (nl + cr));
col = (int) (col/row);
printf("\nnumber of rows is %d\n", row);
printf("number of columns is %d\n\n", col);
// read letters into array
char array[row][col];
if ( fp )
{
for ( ;; )
{
c = getc(fp);
if ( c == EOF )
{
break;
}
if ( c != '\n' && c != '\r' )
{
array[i][j] = c;
if ( ++j >= col )
{
j = 0;
if ( ++i >= row )
{
break;
}
}
}
}
fclose(fp);
}
for ( i = 0; i < row; i++ )
{
for ( j = 0; j < col; j++ )
{
putchar(array[i][j]);
}
putchar('\n');
}
return 0;
}
And here's what I get as output:
Code:
DNOPSNOITACAVFX
BSERENITYNZEKYI
ZBREAMOANARHECM
BBASSWGITOOLKAY
QSCENERYTNLCYMM
TUORTIASEAUBDPA
JZIVVVYYVDXOWSE
FSSENREDLIWAMIR
FIRSEHRSPNLTLTT
CLSCUILRTLYKAES
XAOHQKFOEFYLUSR
ORBGIIWYDSAQLEM
GEYITNEFPGOROEA
EKALNGGAXRERCGJ
TEKRAMSGCAFETOY
number of rows is 15
number of columns is 15
O=
4" P" "
ww/w
w ?=
" wl 1
5
= 2w
w
w " &
w(ww
w w..w
w @
" Fw
" >wFw
I feel that I'm doing something wrong with my variable types or something and that's why it's coming out like that.
Once again, thanks for your help.