I'm having trouble wrapping my brain around this concept. I have taken two java classes before so this is my first C class. My question is about a portion of my assignment. Basically the assignment is it reads in data about a word search in the format like so:

http://pastie.org/2582599

Let me explain a little bit about the data. Every character has a space immediately after, so between each character, and a newline at the very end of the line. The word search is an array made up of N x N characters, so its size is always that of a square.
Immediately after the word search square is a list of the words each on its own separate line.

Here is my question, how do i use fgets() to get the information (at least the word search part) into an 2d array of 50 x 50 ( because it is stated the word search will never be as big as that).

I wrote down everything my professor said in class and was able to piece a little bit of it together.

http://pastie.org/2582622

I guess its a question of how i get the information from one file into my 2d array. And then how do i deal with the words at the end because I am going to need to search for them in the array later and display the array only showing the words in the puzzle, as if it solved them.

Read a character at a time. If it's a letter, add it to the matrix. If it's a \n, move to the next row in the matrix.

Read the words into an array.

I realize the logical process of actually putting those things into an array using getchar(). Every description of fgets(), at least that i've seen in my book only deals with a one dimensional array. How do i translate that into a two dimensional array format?

Read a line from the file. Look at each character in the buffer. If it's a letter, add it to the matrix. If it's a \n, move to the next row in the matrix and read the next line.

ok i need something simple to illustrate what i'm having problems understanding.

lets say i had a data file with this info

A B C
E F G
H I J

3x3 set of chars.

int main()
{
     char inarray[3][3];
     int i;

     while(fgets(inarray,sizeof(inarray),stdin) !=NULL)
     {
          for(i = 0, i <50; i++)
          { 
             inarray[0][i] = getchar();
             if(inarrray[0][i] =='n')
                break;
             getchar(); // to deal with the spaces
          }
          //another for loop goes here to deal with the rest of the array because you can assume i as n in NxN
      }

If i knew how to properly do this i wouldn't have really an issue.

int main()
{
     char inarray[3][3];
     int i;

     while(fgets(inarray,sizeof(inarray),stdin) !=NULL)   // read a line from the file
     {
          for(i = 0, i <50; i++)
          { 
             inarray[0][i] = getchar();     // Read a character from the file

So after reading the line from the file, why do you start reading again using getchar() ? You are throwing away the first line. Just read the first line into a temporary buffer (not inarray) and test each character in the array instead of the getchar() .

since fgets will not do what you're trying to do:

while(fgets(inarray,sizeof(inarray),stdin) !=NULL)

Because fgets reads the chars in to "an array", and you're giving the address of "three" arrays ((3X3) means array of 3 arrays of chars) to it.
You can easily read chars in to the array with getchar() , no need to use fgets() (unless you've been explicitly told to ).
This example may help you.

#include <stdio.h>
/*based on your example illustration
A B C                           
E F G                             
H I J
*/
int main(void){
    char inarray[3][3];
    int i=0,j=0,c;

    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            inarray[i][j]=(char) getchar();

    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            printf("%c",inarray[i][j]);

return 0;

}

since fgets will not do what you're trying to do:

It absolutely will.

Because fgets reads the chars in to "an array", and you're giving the address of "three" arrays ((3X3) means array of 3 arrays of chars) to it.

So he gives the address of an array, and tests each character to see if it should be moved to the matrix.

You can easily read chars in to the array with getchar() , no need to use fgets() (unless you've been explicitly told to ).
This example may help you.

You may need to reread his posts and try to understand the format of the file. Your code is woefully inadequate.

I believe this bit of code will take care of the first line of the array, but how do i get the next lines. I think I need to have this in while loop because when i get to the words at the end, each word will temporarily go into the buffer so i can send it to a function that finds the word which i have yet to write.

int main()
{
	char inarray[50][50];
	char buffer [50];
	int i;
	//insert memgets to wipe out any garbage in arrays

	while(fgets(buffer,sizeof(buffer),stdin) != NULL)
	{
		for(i = 0; i < 50; i++)
		{
			if(buffer[0][i] == ' ')
			{
			   	i++;
			}
			elseif(buffer[0][i] == '\n')
			{
				break;
			}
			else
			{
				inarray[0][i] = buffer[0][i];
			}
		}
		
	}
}

You may need to reread his posts and try to understand the format of the file.

And have you understood the format of the file yourself? if so, do you mind explaining it to me.(since OP doesn't seem to interested in explaining that, he has got his own problems).

Your code is woefully inadequate.

I was just trying to explain the OP how to handle multi-dimensional arrays there.That was supposed to be a base code for his program.

I believe this bit of code will take care of the first line of the array, but how do i get the next lines. I think I need to have this in while loop because when i get to the words at the end, each word will temporarily go into the buffer so i can send it to a function that finds the word which i have yet to write.

DO you even try to compile your code before posting here?
Have you read everything there is to know about arrays?
From the way you have written your code, I don't think so.
I'm not trying to be rude here but if you haven't, read it first.

And have you understood the format of the file yourself? if so, do you mind explaining it to me.(since OP doesn't seem to interested in explaining that, he has got his own problems).

It looks to me like an NxN matrix defining the word search followed by the words hidden in the matrix. The matrix is A letter followed by a space, and each row is terminated with a newline.

The only problem with the format is that there's no clean separation between the matrix and the word list. By my reckoning, a little introspection is needed in parsing the file. I chose to use the second character in the line to determine whether we're looking at a matrix line or a word list line (ie. either a space or not), and that seems to work well even though it's a bit ugly:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define N 22

int main(void)
{
    FILE *in = fopen("test.txt", "r");
    
    if (in != NULL) {
        char matrix[N][N];
        char line[BUFSIZ];
        size_t row = 0;
        size_t col;
        
        while (fgets(line, sizeof line, in) != NULL
            && isspace(line[1])
            && row < N)
        {
            size_t i;
            
            for (i = col = 0; line[i] != '\n' && col < N; i += 2, col++)
                matrix[row][col] = line[i];
                
            ++row;
        }
        
        for (row = 0; row < N; row++) {
            for (col = 0; col < N; col++)
                printf("%c ", matrix[row][col]);
                
            putchar('\n');
        }
        
        do {
            line[strcspn(line, "\n")] = '\0';
            puts(line);
        } while (fgets(line, sizeof line, in) != NULL);
        
        fclose(in);
    }
    
    return 0;
}

Sorry, I've never had to deal with opening up files and extracting data since my visual basic days. Anyways thanks for the suggestions and I will report back with results.

Nice, I tried to grasp this concept with a simple test

test.txt = "ABC
DEF
GHI"

Note that there are no spaces.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
	char inarray[3][3];
	char buffer[10];
	int i,y,z = 0;
	int row = 0;

	FILE *in = fopen("test.txt", "r");

	while(fgets(buffer,sizeof(buffer),in) != NULL)
	{
	
		for(i = 0; i < 3; i++)
		{
			inarray[row][i] = buffer[i];
			
		}
			row++;
	}

	for ( y = 0; y < 3; y++)
	{
		for( z = 0; z < 3; z++)
		{
			printf("%c", inarray[y][z]);
		
		}
			printf("\n");
	} 

	return(0);
}

This code was able to do the job splendidly but I have some questions.

1. How do I know how big my buffer size should be.
2. When I did it with spaces. it would display "A B
D E
G H"

NVM I got it to display with the spaces. Thanks everyone for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.