LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2008, 03:52 PM   #1
psernaalvarez
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
help with fgets in program which solves sudokus


I´m creating a sudoku solver. The part of the code that reads from the sudoku is as follows and i've got the problem that all lines when read contain the info of the last line of the sudoku what am i doing wrong??

Code:

char linebuf[9];
char *lines[9];
int i;

for(i=0; i<10; i++){

char *p= fgets(linesbuf,9,file);
if (p==NULL) break;
lines[i]=p;
}

thanks a lot
 
Old 01-29-2008, 04:12 PM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hya,

I somewhat think all fgets calls return same pointer which is an address of linesbuf.

Happy Penguins!
 
Old 01-31-2008, 12:58 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by psernaalvarez View Post
i've got the problem that all lines when read contain the info of the last line of the sudoku
That’s what you’ve told the program to do. In particular, fgets() does not allocate any new memory and the returned pointer should be the same as the pointer given to the function. Therefore, you end up with (I’ve fixed an off-by-one error, which I’ll talk about later):
Code:
for(i=0; i<9; i++) {
	if(fgets(linebuf, 9, file) == NULL)
		break;
	lines[i] = linebuf;
}
Which amounts to (if you ignore the input part):
Code:
lines[0] = linebuf;
lines[1] = linebuf;
lines[2] = linebuf;
…
lines[8] = linebuf;
If you change linebuf later on, you will get the latest change when you try to access lines[n][m].

Your other problem is array indexing. In particular, you declare an lines to be an array of 9 pointers to char, but you try to access lines[9] which is the tenth element of the array (which doesn’t exist).

Along with this problem, you try to use string functions to store to a nine-element array. This means that you will end up with only eight characters followed by the ASCII NUL. I don’t know what sudoku version you have, but all the ones I’ve seen have rows and columns of 9 characters. To overcome this problem, I would recommend either using non-string input functions (e.g., fread()) or adding an extra character to your array.

I would also recommend using one multi-dimensional array, rather than allocating memory (since the memory would be the same size). So you would end up with something like this:
Code:
lines[9][9];
Or this
Code:
lines[9][10];
Depending on whether you want to use fread() or fgets().
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Commission solves Windows XPN mystery LXer Syndicated Linux News 0 04-26-2006 06:54 PM
apt-get auto solves dependencies linuxmandrake Linux - General 3 09-24-2005 04:50 PM
Compiling the Kernel Solves most problems Nappa Slackware 3 12-05-2003 06:03 PM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM
ctrl/alt f1 solves my touchpad problem - why? tgarland Linux - Hardware 6 07-02-2003 09:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration