Hi all, I am a beginner in C , I need help in reading data from a standard input and storing it in array .I need to read sudoku data's in a file name sudoku.txt and store the values in an array.
Hi all, I am a beginner in C , I need help in reading data from a standard input and storing it in array .I need to read sudoku data's in a file name sudoku.txt and store the values in an array.
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Look up fscanf, fgets, or some of the f-ing functions. RTFM.
Quzah.
Hope is the first step on the road to disappointment.
this is what I have come up with ...
Code:int c; File *data; data=open("sudoku.txt","r"); c=getc(data);
Ok, so that gets you one character. Go read about arrays, and see if you can put those in an array. (Also, read about loops.)
Quzah.
Hope is the first step on the road to disappointment.
How will the compiler know the path of the file??
Code:data=fopen("sudoku.txt","r");
You specify it. Otherwise it defaults to where your executable is. You could always RTFM (click that) like I said in my first post.If you want to be a programmer, you need to learn to look things up.Originally Posted by TFM
Quzah.
Hope is the first step on the road to disappointment.
Is working now!
Code:#include <stdio.h> #include <stdlib.h> int main() { int c,grid[9][9]; int i=0,j=0; FILE *data; data=fopen("sudoku1.txt" ,"r"); while ( fscanf(data, "%d", &c) == 1 ) { if(c==' ') { continue; } else if(c=='\n') { i=0; j++; } grid[i][j]=c; printf("%d\n",grid[i][j++]); counter++; return 0; }
Well, except you should turn the warnings up on your compiler and pay attention to it's reports...
I'm betting it's complaining about an implicit declaration of counter ...
Also there are still logic problems in your code... You really need to print that out the grid values to make sure it's capturing the data correctly...
'Cause I'm betting you're running way off the end of your grid on the j axis.Code:printf("grid[%d][%d] = %d\n",i,j,grid[i][j++]);
First and most important lesson in programming... "Compiles" does not mean "Works".
Last edited by CommonTater; 09-09-2011 at 04:27 AM.