I have this long program and I need to write an array to a file but I'm having issues creating a function that passes array to it so I can write to file without redoing it every time.
This is what I want the function to be but I'm having issues passing the array to it.
the whole part of the program for this functionallity is at bottom plus contents of the file that the program needs to be able to run
void wtf(char *array)
{
int fdi = open("board.txt", O_RDWR);
char msg[1] = "\n";
char msg1[1] = ",";
char msg2[1] = " ";
int i = 0;
int j = 0;
char *neg4 = "-4";
char *neg2 = "-2";
char *neg3 = "-3";
char *neg5 = "-5";
char *neg6 = "-6";
char *neg1 = "-1";
char *cero = "+0";
char *pls4 = "+4";
char *pls2 = "+2";
char *pls3 = "+3";
char *pls5 = "+5";
char *pls6 = "+6";
char *pls1 = "+1";
char *table[8][8] = {{NULL}};
if (fdi != -1)
{
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
if ((strcmp(table[i][j], neg4) == 0) || (strcmp(table[i][j], neg3) == 0) || (strcmp(table[i][j], neg2) == 0) ||
(strcmp(table[i][j], neg5) == 0) || (strcmp(table[i][j], neg6) == 0) || (strcmp(table[i][j], neg1) == 0))
{
write(fdi, table[i][j], strlen(table[i][j]));
}
else if ((strcmp(table[i][j], pls4) == 0) || (strcmp(table[i][j], pls3) == 0) || (strcmp(table[i][j], pls2) == 0) ||
(strcmp(table[i][j], pls5) == 0) || (strcmp(table[i][j], pls6) == 0) || (strcmp(table[i][j], pls1) == 0))
{
write(fdi, table[i][j], strlen(table[i][j]));
}
else // if (strcmp(table[i][j], cero) == 0)
{
write(fdi, msg2, sizeof(msg2));
write(fdi, table[i][j], strlen(table[i][j]));
}
if (j <= 6)
{
write(fdi, msg1, sizeof(msg1));
}
}
write(fdi, msg, sizeof(msg));
}
close(fdi);
}
}
the code..
#include <stdio.h>
#include <stdlib.h> // For exit() function
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#define MAXC 1024
void wtf(char *array);
int main()
{
char c[1000];
char ch[1000];
FILE *fp, *fd, *op;
char word[1000], *token;
char *table[8][8] = {{NULL}};
int i = 0;
int j = 0;
char input[50];
char *endPos, *begPos;
char *buffer;
char *exit = "exit";
char *show = "show";
char *move = "mv";
char *cp = "cp";
char *prq = "prq";
char *prr = "prq";
char *prb = "prb";
char *prk = "prk";
char *neg4 = "-4";
char *neg2 = "-2";
char *neg3 = "-3";
char *neg5 = "-5";
char *neg6 = "-6";
char *neg1 = "-1";
char *cero = "0";
char *pls4 = "+4";
char *pls2 = "+2";
char *pls3 = "+3";
char *pls5 = "+5";
char *pls6 = "+6";
char *pls1 = "+1";
char *one = "1";
char msg[1] = "\n";
char msg1[1] = ",";
char msg2[1] = " ";
char msg3[1] = "+";
char msg4[1] = "-";
char yes[4] = "yes";
int a0Row, b0Col, a1Row, b1Col;
int fdi;
int flag = 0;
size_t len = 10;
if ((fp = fopen("board.txt", "r")) == NULL)
{
printf("Error! opening file");
return 0;
}
//fill array
while (fscanf(fp, "%3s", c) != EOF)
{
token = strtok(c, ",");
len = strlen(token);
//printf("%s ", token);
table[i][j] = malloc(len + 1);
memcpy(table[i][j++], token, len + 1);
if (j >= 8)
{
i++;
j = 0;
//printf("\n");
}
}
printf("\n\n\n");
for (i = 0; i < 8; i++)
{
//printf("\n");
for (j = 0; j < 8; j++)
{
printf("%s ", table[i][j]);
}
printf("\n");
}
fclose(fp);
fdi = open("new.txt", O_RDWR);
if (fdi != -1)
{
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
if ((strcmp(table[i][j], neg4) == 0) || (strcmp(table[i][j], neg3) == 0) || (strcmp(table[i][j], neg2) == 0) ||
(strcmp(table[i][j], neg5) == 0) || (strcmp(table[i][j], neg6) == 0) || (strcmp(table[i][j], neg1) == 0))
{
write(fdi, table[i][j], strlen(table[i][j]));
}
else if ((strcmp(table[i][j], pls4) == 0) || (strcmp(table[i][j], pls3) == 0) || (strcmp(table[i][j], pls2) == 0) ||
(strcmp(table[i][j], pls5) == 0) || (strcmp(table[i][j], pls6) == 0) || (strcmp(table[i][j], pls1) == 0))
{
write(fdi, table[i][j], strlen(table[i][j]));
}
else
{
write(fdi, msg2, sizeof(msg2));
write(fdi, table[i][j], strlen(table[i][j]));
}
if (j <= 6)
{
write(fdi, msg1, sizeof(msg1));
}
}
write(fdi, msg, sizeof(msg));
}
close(fdi);
}
return 0;
}
contents of file board.txt so program can run
-4,-2,-3,-5,-6,-3,-2,-4
-1,-1,-1,-1,-1,-1,-1,-1
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0
+1,+1,+1,+1,+1,+1,+1,+1
+4,+2,+3,+5,+6,+3,+2,+4
I'm sure its probably very ineficient, just learning
Thanks for any input.
update:added errors I get
when I try doing
wtf(table)
compiler says
warning: passing argument 1 of 'wtf' from incompatible pointer type [-Wincompatible-pointer-types]
wtf(table);
^~~~~
chess.c:8:16: note: expected 'char *' but argument is of type 'char * (*)[8]'
void wtf(char *array);
then I try
wtf(*table)
compiler says
chess.c:586:18: warning: passing argument 1 of 'wtf' from incompatible pointer type [-Wincompatible-pointer-types]
wtf(*table);
^~~~~
chess.c:8:16: note: expected 'char *' but argument is of type 'char **'
void wtf(char *array);
~~~~~~^~~~~