GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Apr-2008, 04:16
anirudhroxrulz anirudhroxrulz is offline
New Member
 
Join Date: Apr 2008
Posts: 3
anirudhroxrulz is on a distinguished road

contents of .txt file into 2D array


hi guys...im trying to get the contents of a sudoku in a .txt file into an array sudoku[9][9]...feels really weird coz my program is about verifying a given soduko solution...basically im done with the whole algorithm of checking...and im stuck with stupid fscanf
the .txt file looks like this
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8

and heres the code ive written...basically when i run it nothing happens...the compiler just keeps running...
CPP / C++ / C Code:
inp=fopen("xx.txt", "r");
	while(!feof(inp)) {
		for (i=0; i<9; i++) {
			for (j=0; j<9; j++) {
				fscanf(inp, "%d", &sudoku[i][j]);
			}
		}
	}
Please Help
Last edited by LuciWiz : 07-Apr-2008 at 08:23. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 07-Apr-2008, 04:19
anirudhroxrulz anirudhroxrulz is offline
New Member
 
Join Date: Apr 2008
Posts: 3
anirudhroxrulz is on a distinguished road

Re: contents of .txt file into 2D array


oh jeez...i figured it out...
i misplaced i with j in one place...
the code does work...
sorries

guess the topic can be closed
thank you
  #3  
Old 08-Apr-2008, 10:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: contents of .txt file into 2D array


Actually, no it doesn't. There's still a bug. See this
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #4  
Old 10-Apr-2008, 07:29
anirudhroxrulz anirudhroxrulz is offline
New Member
 
Join Date: Apr 2008
Posts: 3
anirudhroxrulz is on a distinguished road

Re: contents of .txt file into 2D array


well...my program runs perfectly...
yea i saw the link u posted...i get it...
but this still runs...
i get perfect outcomes...
  #5  
Old 10-Apr-2008, 22:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: contents of .txt file into 2D array


Quote:
Originally Posted by anirudhroxrulz
well...my program runs perfectly...

Humor me: try the following:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    FILE *inp;
    int i, j;
    int sudoku[9][9];
    inp = fopen("xx.txt", "r");
    while (!feof(inp)) {
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                printf("reading sudoku[%d][%d]\n", i, j);
                fscanf(inp, "%d", &sudoku[i][j]);
                if(feof(inp)) {
                    printf("found eof\n");
                }
            }
        }
    }
    return 0;
}

Output:
Code:
reading sudoku[0][0] reading sudoku[0][1] reading sudoku[0][2] reading sudoku[0][3] reading sudoku[0][4] reading sudoku[0][5] reading sudoku[0][6] reading sudoku[0][7] reading sudoku[0][8] reading sudoku[1][0] reading sudoku[1][1] reading sudoku[1][2] reading sudoku[1][3] reading sudoku[1][4] reading sudoku[1][5] reading sudoku[1][6] reading sudoku[1][7] reading sudoku[1][8] reading sudoku[2][0] reading sudoku[2][1] reading sudoku[2][2] reading sudoku[2][3] reading sudoku[2][4] reading sudoku[2][5] reading sudoku[2][6] reading sudoku[2][7] reading sudoku[2][8] reading sudoku[3][0] reading sudoku[3][1] reading sudoku[3][2] reading sudoku[3][3] reading sudoku[3][4] reading sudoku[3][5] reading sudoku[3][6] reading sudoku[3][7] reading sudoku[3][8] reading sudoku[4][0] reading sudoku[4][1] reading sudoku[4][2] reading sudoku[4][3] reading sudoku[4][4] reading sudoku[4][5] reading sudoku[4][6] reading sudoku[4][7] reading sudoku[4][8] reading sudoku[5][0] reading sudoku[5][1] reading sudoku[5][2] reading sudoku[5][3] reading sudoku[5][4] reading sudoku[5][5] reading sudoku[5][6] reading sudoku[5][7] reading sudoku[5][8] reading sudoku[6][0] reading sudoku[6][1] reading sudoku[6][2] reading sudoku[6][3] reading sudoku[6][4] reading sudoku[6][5] reading sudoku[6][6] reading sudoku[6][7] reading sudoku[6][8] reading sudoku[7][0] reading sudoku[7][1] reading sudoku[7][2] reading sudoku[7][3] reading sudoku[7][4] reading sudoku[7][5] reading sudoku[7][6] reading sudoku[7][7] reading sudoku[7][8] reading sudoku[8][0] reading sudoku[8][1] reading sudoku[8][2] reading sudoku[8][3] reading sudoku[8][4] reading sudoku[8][5] reading sudoku[8][6] reading sudoku[8][7] reading sudoku[8][8] reading sudoku[0][0] found eof reading sudoku[0][1] found eof reading sudoku[0][2] found eof reading sudoku[0][3] found eof reading sudoku[0][4] found eof reading sudoku[0][5] found eof reading sudoku[0][6] found eof reading sudoku[0][7] found eof reading sudoku[0][8] found eof reading sudoku[1][0] found eof reading sudoku[1][1] found eof reading sudoku[1][2] found eof reading sudoku[1][3] found eof reading sudoku[1][4] found eof reading sudoku[1][5] found eof reading sudoku[1][6] found eof reading sudoku[1][7] found eof reading sudoku[1][8] found eof reading sudoku[2][0] found eof reading sudoku[2][1] found eof reading sudoku[2][2] found eof reading sudoku[2][3] found eof reading sudoku[2][4] found eof reading sudoku[2][5] found eof reading sudoku[2][6] found eof reading sudoku[2][7] found eof reading sudoku[2][8] found eof reading sudoku[3][0] found eof reading sudoku[3][1] found eof reading sudoku[3][2] found eof reading sudoku[3][3] found eof reading sudoku[3][4] found eof reading sudoku[3][5] found eof reading sudoku[3][6] found eof reading sudoku[3][7] found eof reading sudoku[3][8] found eof reading sudoku[4][0] found eof reading sudoku[4][1] found eof reading sudoku[4][2] found eof reading sudoku[4][3] found eof reading sudoku[4][4] found eof reading sudoku[4][5] found eof reading sudoku[4][6] found eof reading sudoku[4][7] found eof reading sudoku[4][8] found eof reading sudoku[5][0] found eof reading sudoku[5][1] found eof reading sudoku[5][2] found eof reading sudoku[5][3] found eof reading sudoku[5][4] found eof reading sudoku[5][5] found eof reading sudoku[5][6] found eof reading sudoku[5][7] found eof reading sudoku[5][8] found eof reading sudoku[6][0] found eof reading sudoku[6][1] found eof reading sudoku[6][2] found eof reading sudoku[6][3] found eof reading sudoku[6][4] found eof reading sudoku[6][5] found eof reading sudoku[6][6] found eof reading sudoku[6][7] found eof reading sudoku[6][8] found eof reading sudoku[7][0] found eof reading sudoku[7][1] found eof reading sudoku[7][2] found eof reading sudoku[7][3] found eof reading sudoku[7][4] found eof reading sudoku[7][5] found eof reading sudoku[7][6] found eof reading sudoku[7][7] found eof reading sudoku[7][8] found eof reading sudoku[8][0] found eof reading sudoku[8][1] found eof reading sudoku[8][2] found eof reading sudoku[8][3] found eof reading sudoku[8][4] found eof reading sudoku[8][5] found eof reading sudoku[8][6] found eof reading sudoku[8][7] found eof reading sudoku[8][8] found eof

Scroll down the output that I show: it goes through the loops twice. Now, you might claim that, since you observe the answers you expected that it is working perfectly, and perhaps for your purposes it is satisfactory.

However it is a very real bug, and if you get into the habit of ignoring bugs (like this or others that may be pointed out to you from time to time), well, that's your business. Others who read threads like this may be interested in knowing how to avoid bugs.

For example
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *inp;
    char *inname = "xx.txt";
    int i, j;
    int sudoku[9][9];
    inp = fopen(inname, "r");
    if (inp == NULL) {
        printf("There was a problem opening file %s for reading.\n", inname);
        return(EXIT_FAILURE);
    }

    for (i = 0; i < 9; i++) {
        for (j = 0; j < 9; j++) {
            if (fscanf(inp, "%d", &sudoku[i][j]) != 1) {
                printf("There was a problem reading sudoku[%d][%d]\n", i, j);
                return (EXIT_FAILURE); /* or whatever you want to do in case of an error */
            }
        }
    }
    /* now process the array */
    return 0;
}

Regards,

Dave
Last edited by davekw7x : 10-Apr-2008 at 23:51.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
How to use the data obtain in one C file to another C file? TommyC C Programming Language 12 15-Jan-2008 09:51
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 03:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:02.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.