initialize an array reading a .dat file | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

initialize an array reading a .dat file

Hi all I have generate a initial state of a sudok using the following code. Every triplet is translated to row,column,value. For example : 1,1,5 means row=1 column=1 value=5 [code] #include <stdio.h> unsigned char arr[] = { 1,1,5,1,3,2, 2,6,9,2,7,8, 3,2,8,3,4,6,3,6,1,3,7,5, 4,2,5, 5,1,3,5,5,1,5,8,6,5,9,5, 6,3,9,6,5,3,6,8,4,6,9,1, 7,3,4,7,5,6,7,7,1, 8,5,8, 9,4,3,9,5,4,9,6,2,9,8,8}; int main (int argc, char **argv) { FILE *fp; fp = fopen ("initial.dat", "wb"); fwrite (arr, 3, 44, fp); fclose (fp); } [/code] I would like to make a program just read the file (dynamically with malloc) and assign the values to an array[9][9] . So, after the read array[0][0]=5 array[0][2]=2 array[1][5]=9 ... array[8][7]=8

22nd Aug 2018, 6:02 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
2 Respuestas
+ 6
Is it like, you are defining level in file and want to read and start the game?
22nd Aug 2018, 7:11 AM
code learner
code learner - avatar
0
I am trying this code : https://code.sololearn.com/c5d0R7uUjKN5/#c But I get Testing fread() function: 1 1 5 ==>51 3 2 ==>22 6 9 ==>92 7 8 ==>83 2 8 ==>83 4 6 ==>63 6 1 ==>13 7 5 ==>54 2 5 ==>55 1 3 ==>35 5 1 ==>15 8 6 ==>65 9 5 ==>56 3 9 ==>96 5 3 ==>36 8 4 ==>46 9 1 ==>17 3 4 ==>47 5 6 ==>67 7 1 ==>18 5 8 ==>89 4 3 ==>39 5 4 ==>49 6 2 ==>29 8 8 ==>8 Any ideas please ?
22nd Aug 2018, 8:14 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar