ok I'm taking a text file.. 16 lines.. 6 character values per line and trying to randomly get one character per each line.. and place them into a 2d array (4x4). The values i'm getting aren't correct for some reason.. anyone have any helpful tips?

int main()
{
	srand (time(NULL));
	int r;
	char line[6];
	char board[4][4];
	char c;
	FILE *fid;
	fid = fopen("file.txt","r");
	int i=0; int j=0;
	while (fgets(line, sizeof(line), fid)!=NULL)
	{
		fscanf(fid, "%s", &line);
		r = rand()%5;
		printf("The random number is: %d\n", r);
		printf("The line is %s\n", line);
		c = line[r];
		board[i][j]=c;
	
		if (i==3)
		{
			i=0;
			j++;
		}
		else
		{
			i++;
		}
	}

	for(i=0; i<4; i++)
	{
		for(j=0; j<4; j++)
		{
			printf("%c\n",board[i][j]);
		}
	}
return 0;
}

I don't see anything glaringly wrong, but since you didn't bother to show us what you are reading, what should have happened, and what did happen, that's as far as I'm going to go with it.

Remember, the more details you give, the less we have to guess -- and guess wrong.

I don't see anything glaringly wrong, but since you didn't bother to show us what you are reading, what should have happened, and what did happen, that's as far as I'm going to go with it.

Remember, the more details you give, the less we have to guess -- and guess wrong.

the input is a text file.. it reads something in the lines of:

LYRTTE
MVTRWE
...
..(the periods just symbolize the fact that it goes on 16 lines)

when outputting I get:

The random number is: 3
The line is E
The random number is: 4
The line is WE
The random number is: 2
... (this goes on 16 times)

E

F
D
A
S
I


W
O


C
I

#1: Bad definition
File contents:
LYRTTE\n
MVTRWE\n

All lines in the file end with \n, or value 10 char line[6]; // Only 6 characters defined

while (fgets(line, sizeof(line), fid)!=NULL)  // Read 'LYRTT' from the file and add the required \0 to fill out the 6 characters
{                        
    fscanf(fid, "%s", &line);    // Throw away the data just read and read the final E, \n.  store E \0  in line.

Also, on the fscanf() you don't need the &. And why use that when fgets() does the job better? See this.

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.