int myArray[8][8];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _file_grid
{
size_t width;
size_t height;
char **data;
} FILE_GRID, *PFILE_GRID;
PFILE_GRID readFile(const char *fpath)
{
PFILE_GRID pData = NULL;
FILE *fp = fopen(fpath, "rb");
char buffer[1024];
size_t width = 0;
if (fp)
{
pData = (PFILE_GRID)calloc(1, sizeof(FILE_GRID));
while((fgets(buffer, sizeof(buffer), fp)) != NULL)
{
pData->height++;
width = strlen(buffer);
pData->data = (char**)realloc(pData->data, sizeof(char*) * pData->height); //allocate another line worth of memory
//(note, this is fairly inefficient, a better way would be to start the allocation at like... 10 * sizeof(char*) and then increase the buffer by 10 units
//each time it runs out of room, saves on realloc calls which can be expensive if you don't have enough linear memory for the allocation.
pData->data[pData->height - 1] = strdup(buffer); //duplicate the buffer to it's own memory location.
//trim the newline characters.
if (pData->data[pData->height - 1][width - 1] == '\n')
pData->data[pData->height - 1][width - 1] = '\0';
pData->width = width;
}
fclose(fp);
}
return pData;
}
void freeFile(PFILE_GRID pGrid)
{
for(size_t h = 0; h < pGrid->height; h++)
free(pGrid->data[h]); //free all the strdup'd pointers, they are dynamically allocated too.
free(pGrid->data); //free the top level pointer now that the sub-pointers have been deallocated.
free(pGrid); //finally free the grid itself.
}
12121212 34343434 56565656
PFILE_GRID pGrid = readFile("B:\\temp.txt");
if (pGrid)
printf("Char at (%d, %d): %c\n", 3, 1, pGrid->data[1][3]);
freeFile(pGrid);
Char at (3, 1): 4
#include <stdio.h>
#define W_H_SIZE 8
#define W_W_SIZE 64
int main(void)
{
FILE *fpIn;
char newarray[W_H_SIZE][W_H_SIZE];
int i,j;
fpIn = fopen("file.txt", "r");
for(i =0; i < 8; i++)
{
fscanf(fpIn,"%s",&newarray[i]);
}
printf("%s",newarray);
//int i;
getchar();
return 0;
}
#include <stdio.h>
int pawnCk(char **boardArray,int kingRow, int kingCol);
int main(void)
{
char boardArray[8][8] = { 0 };
int check = 0 ;
int bKingRow = 8;
int bKingCol = 8;
int wKingRow = 8;
int wKingCol = 8;
int i = 0;
int j = 0;
char ch;
FILE *fpIn, *fpOut;
fpIn = fopen("Board.txt", "r");
while ((ch = getc(fpIn)) != EOF) {
switch(ch) {
case 'k':
bKingRow = i;
bKingCol = j;
boardArray[i][j] = ch;
case 'K':
wKingRow = i;
wKingCol = j;
boardArray[i][j] = ch;
case 'p': case 'P':
case 'q': case 'Q':
case 'n': case 'N':
case 'b': case 'B':
case 'r': case 'R':
if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
boardArray[i][j] = ch;
case '*':
if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
i++;
break;
case '\n':
if (i != 8) { printf("Bad file: There were only %d spaces on line %d\n", i, j); return -1; }
i = 0;
j++;
if (j >= 8);
break;
}
}
if (j < 7) { printf("Bad file: There were only %d lines.\n", j); return -1;}
if (i != 8) { printf("Bad file: There were only %d spaces on the last line.\n", i); return -1;}
if ((bKingRow < 8) && (bKingCol < 8))
{
check = pawnCk(boardArray, bKingRow, bKingCol); //On compile:Error [Warning] Passing arg1 of 'pawCk' from incompatible pointer type.
}
getchar();
return 0;
}
int pawnCk(char **boardArray,int kingRow, int kingCol)
{
int atkRow ,atkCol1, atkCol2;
if (kingRow = 'k')
{
atkRow = kingRow + 1;
atkCol1 = kingCol - 1;
}
//Rich(Nextgen) Comment: She doesn't know you, but I think I figured out why she is getting this error, I emailed her but feel free to offer other opinions as well, Please & Thank You
if (boardArray[atkRow][atkCol1] = 'P') //On Debug: Access violation (segmentation fault) raised in your program
return 1;
atkCol2 = kingCol + 1;
if (boardArray[atkRow][atkCol2] = 'P')
return 1;
else
return 0;
}
if (kingRow = 'K')
{
atkRow = kingRow -1;
atkCol1=kingCol - 1;
if (boardArray[atkRow][atkCol1] = 'p')
{
return 1;
}
atkCol2 = kingCol + 1;
if (boardArray[atkRow][atkCol2] = 'p')
return 1;
else
return 0;
}
}


#include <stdio.h>
int pawnCk(char boardArray[8][8],int kingRow, int kingCol);
int main(void)
{
char boardArray[8][8] = { 0 };
int check = 0 ;
int bKingRow = 8;
int bKingCol = 8;
int wKingRow = 8;
int wKingCol = 8;
int i = 0;
int j = 0;
char ch;
FILE *fpIn, *fpOut;
fpIn = fopen("Board.txt", "r");
while ((ch = getc(fpIn)) != EOF) {
switch(ch) {
case 'k':
bKingRow = i;
bKingCol = j;
boardArray[i][j] = ch;
case 'K':
wKingRow = i;
wKingCol = j;
boardArray[i][j] = ch;
case 'p': case 'P':
case 'q': case 'Q':
case 'n': case 'N':
case 'b': case 'B':
case 'r': case 'R':
if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
boardArray[i][j] = ch;
case '*':
if (i >= 8) { printf("Bad file: Too many spaces on line %d\n", j); return -1; }
i++;
break;
case '\n':
if (i != 8) { printf("Bad file: There were only %d spaces on line %d\n", i, j); return -1; }
i = 0;
j++;
if (j >= 8)
break;
}
}
if (j < 7) { printf("Bad file: There were only %d lines.\n", j); return -1;}
if (i != 8) { printf("Bad file: There were only %d spaces on the last line.\n", i); return -1;}
if ((bKingRow < 8) && (bKingCol < 8))
{
check = pawnCk(boardArray, bKingRow, bKingCol); //On compile:Error [Warning] Passing arg1 of 'pawCk' from incompatible pointer type.
}
getchar();
return 0;
}
int pawnCk(char boardArray[8][8],int kingRow, int kingCol)
{
int atkRow ,atkCol1, atkCol2;
if (boardArray[kingRow][kingCol] == 'k')
{
atkRow = kingRow + 1;
atkCol1 = kingCol - 1;
}
//Rich(Nextgen) Comment: She doesn't know you, but I think I figured out why she is getting this error, I emailed her but feel free to offer other opinions as well, Please & Thank You
if (boardArray[atkRow][atkCol1] == 'P') //On Debug: Access violation (segmentation fault) raised in your program
return 1;
atkCol2 = kingCol + 1;
if (boardArray[atkRow][atkCol2] == 'P')
return 1;
else
return 0;
if (kingRow == 'K')
{
atkRow = kingRow -1;
atkCol1=kingCol - 1;
if (boardArray[atkRow][atkCol1] == 'p')
{
return 1;
}
atkCol2 = kingCol + 1;
if (boardArray[atkRow][atkCol2] == 'p')
return 1;
else
return 0;
}
}