1

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);
          ~~~~~~^~~~~
Mauro
  • 23
  • 5
  • "I'm having problems" is not very clear. Is there a compile error? Is there a crash? Are there incorrect results some time? All the time? What exactly? – kaylum Apr 24 '20 at 01:05
  • "I'm having issues passing the array to it" What issues? You have not even shown any calls to the `wtf` function. – kaylum Apr 24 '20 at 01:07
  • @kaylum sorry about that, I added the errors I get to post. – Mauro Apr 24 '20 at 01:21
  • [passing 2d array to function with in c](https://stackoverflow.com/questions/24838851/passing-2d-array-to-function-with-in-c) – kaylum Apr 24 '20 at 01:43

1 Answers1

1

There were a number of errors, including warnings about unused variables.

Too many to enumerate them, but ...

wtf had it's own table and not the one read by main.

It looks as though you tried to put the wtf into main when you couldn't pass the table array correctly.

Plus, doing neg1, neg2, ... instead of an array: char neg[6] makes the code needlessly complicated. Likewise for pls*

Also, neg* and pls* were being replicated in both functions. I've moved them to globals for compactness and speed. After doing this, I realized they could go [back] to wtf but having them as global is actually faster.

I fixed the table array passing to wtf [which was your main issue, I believe].

I refactored your program. It is still incomplete [and probably still a bit broken], but it should get you a bit further:

#include <stdio.h>
#include <stdlib.h>                     // For exit() function
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#define MAXC 1024

#if 0
char msg[1] = "\n";
char msg1[1] = ",";
char msg2[1] = " ";
char msg3[1] = "+";
char msg4[1] = "-";
#else
const char *msg[5] = {
    "\n", ",", " ", "+", "-"
};
#endif

#if 0
const char *neg1 = "-1";
const char *neg4 = "-4";
const char *neg2 = "-2";
const char *neg3 = "-3";
const char *neg5 = "-5";
const char *neg6 = "-6";
#else
const char *neg[6] = {
    "-1", "-2", "-3", "-4", "-5", "-6"
};
#endif

//const char *cero = "+0";

#if 0
const char *pls4 = "+4";
const char *pls2 = "+2";
const char *pls3 = "+3";
const char *pls5 = "+5";
const char *pls6 = "+6";
const char *pls1 = "+1";
#else
const char *pls[6] = {
    "+1", "+2", "+3", "+4", "+5", "+6"
};
#endif

const char *exitmsg = "exit";
const char *show = "show";
const char *move = "mv";
const char *cp = "cp";
const char *prq = "prq";
const char *prr = "prq";
const char *prb = "prb";
const char *prk = "prk";
const char *cero = "0";

const char *one = "1";

char yes[4] = "yes";

#define ARRSIZE(_arr)       (sizeof(_arr) / sizeof((_arr)[0]))

void
write_string(int fdo,const char *str)
{
    int len = strlen(str);
    write(fdo,str,len);
}

void
wtf(int fdo,char *table[8][8])
{
    int i = 0;
    int j = 0;
#if 0
    char *table[8][8] = { {NULL} };
#else
    char *tab;
#endif
    int idx;
    int match;

    for (i = 0; i < 8; i++) {
        for (j = 0; j < 8; j++) {
            tab = table[i][j];

            do {
                match = 0;

                for (idx = 0;  idx < ARRSIZE(neg);  ++idx) {
                    if (strcmp(tab,neg[idx]) == 0) {
                        write_string(fdo, tab);
                        match = 1;
                        break;
                    }
                }
                if (match)
                    break;

                for (idx = 0;  idx < ARRSIZE(pls);  ++idx) {
                    if (strcmp(tab,pls[idx]) == 0) {
                        write_string(fdo, tab);
                        match = 1;
                        break;
                    }
                }
                if (match)
                    break;

                if (strcmp(tab, cero) == 0) {
                    write_string(fdo, msg[2]);
                    write_string(fdo, tab);
                    match = 1;
                    break;
                }
            } while (0);

            if (j <= 6) {
                write_string(fdo, msg[1]);
            }
        }

        write_string(fdo, msg[0]);
    }
}

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;
    int a0Row, b0Col, a1Row, b1Col;
    int fdo;
    int flag = 0;
    char *bp;

    size_t len = 10;

    if ((fp = fopen("board.txt", "r")) == NULL) {
        printf("Error! opening file");
        return 0;
    }

//fill array
    while (1) {
        // get a line
        bp = fgets(c,sizeof(c),fp);
        if (bp == NULL)
            break;

        // parse all tokens on the line
        while (1) {
            token = strtok(bp,", \t\n");
            bp = NULL;
            if (token == NULL)
                break;

            len = strlen(token);

            // printf("%s ", token);
#if 0
            table[i][j] = malloc(len + 1);
            memcpy(table[i][j++], token, len + 1);
#else
            //printf("token='%s'\n",token);
            table[i][j++] = strdup(token);
#endif

            if (j >= 8) {
                i++;
                j = 0;
                // printf("\n");
            }
        }
    }

    fclose(fp);

    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");
    }

    fdo = open("new.txt", O_WRONLY | O_CREAT,0644);
    if (fdo == -1) {
        perror("new.txt");
        return 1;
    }

    wtf(fdo,table);

    close(fdo);

    return 0;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Thank you so much for all your input, I will definitely use them to better my program. -The reason for the unused variables is 'cause what I posted is just a snippet a bigger and surely very inefficient program I'm writing for school, still learning. forgot to take them all out. Also the wtf(writetofile) code is inside main is because I've been using it(the code of the function) since wtf was not working, was needing to reuse that every time i had to write to file during several occassions through out the code. – Mauro Apr 24 '20 at 02:55