victoria277
victoria277

Reputation: 67

Read data from a txt.file and print out its 2D array contents

I need some help, I would like to print out information from a file (in characters)

I decided to use a 2D array since the data looks something like this

0 0 . . . 
0 0 . . . 
. . . . . 
. . . . . 
. . . . . 

It seems that I cannot print them out with the code that I have prepared now

please see below:

/* Read from file, pass on file contents to a 2D array, Print file contents from 2D array*/

#include<stdio.h>
#include<string.h>
#define N 5


//My proposed function to print an array with contents from a file
void printboard(int **a, int n, int n);

int main(int argc, char *argv[])
{   
    char linestr[100];
    int board[N][N];
    int k;
    int h=0, l=0; 
    if(argc==2) //File should be called from the terminal hence working with argc & argv
    {
        FILE *fp;
        fp = fopen(argv[1], "r");


     if(fp == NULL) 
        {
            printf("Error, can't open '%s' file!!!\n", argv[1]);
            return -1;
        }
    while (fgets(linestr,sizeof linestr, fp) != NULL)
        for(k=0; k<strlen(linestr); k++)
        {
            if (linestr[k]!='\n')
            {
                board[h][l]=(int)linestr[k];
                l++;


            }
            h++;
            l=0;
        }
        fclose(fp);
    }
    printboard(board,h,l);

return 0;
}

void printboard(int **a, int n, int n)
{
    int i, j;
    for (i=0; i< N; i++)
    {
        for (j=0; j< N; j++)
        {
            printf("%c", a[i][j]);
        }
        printf("\n");
    }


}

I have very basic knowledge in C and only began coding 1.5 months ago. Is there any advice from the community how I can fix this or do it better? The aim is to print contents of a file in a 2D array format. I would really like to have the data work with 2D array because I need to work further on it to move the '0' in a game called Peg Solitaire.

Upvotes: 3

Views: 1397

Answers (2)

mday299
mday299

Reputation: 139

Argument names in functions must be unique. So your printboard function cannot have two "n" arguments, you'll have to change one name (to "m" or something). Also, since you're printing n x n matrices and you have defined "N" via the preprocessor, do you even need those arguments? :)

The passing of the array (first argument of printboard) is a bit trickier and requires some knowledge of how pointers and memory work. See discussion here:

http://cboard.cprogramming.com/c-programming/97898-passing-2-dimensional-array-function.html

What this boils down to is changing your function definition to:

void printboard(int a[][N], int m, int n);

One little thing: in order to print your numbers you probably want to print them as integers rather than characters so you should change

printf("%c", a[i][j]);

to

printf("%d ", a[i][j]);

I added the space so the numbers would run together when print out to the terminal.

As far as parsing the input, that's a whole nuther topic. I'd suggest strtok to break up your lines as you read them from the file. Then you'll use something like sscanf to store the data into your board array. See these refs:

http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/ http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Upvotes: 1

esskar
esskar

Reputation: 10940

i am just guessing here that you want points (.) displayed when the field is empty so prefill your board array.

char board[N][N]; // i changed it to char, was int but char is enough
memset(board, '.', sizeof(board));

Upvotes: 0

Related Questions