hi all, i used the search function, but the threads i found didnt really help me much since i am quite a newbie and didnt understand how it worked.

so what i am trying to do seems simple but it doesnt work. i have a textfile with a single line of numbers. my goal is to read the line and put the numbers into a char array.

char sudoku[81];
int i = 0;
while((fscanf(file, "%ld", &sudoku[i])) != EOF ){ 
  fscanf(file, "%ld", &sudoku[i]);  
  i++;
}

the amount of numbers is fixed to 81, so the file looks like this:
001000009608794020009105000000670002080000060090430050004000010930010840002043000

so when i try to read from the array i only get:
-1-1-1127000000000000000000000000000000000000000000000000000000000000000000000000000000

i appreciate any help, thanks in advance

Recommended Answers

All 5 Replies

#include<stdio.h>

int main(void)
{
    FILE* fd;
    char buff[100];
    fd = fopen("./test.txt","r");
    fgets(buff,sizeof(buff),fd); //here the line from file comes to char array buff

    printf("\n%s\n",buff);

    return 0;
}

thanks alot for the quick reply. seems to work, just that it seems like i made some mistakes.. like i didnt know that this

char sudoku[] = { 0, 0, 1, 0, 0, 0, 0, 0, 9,
                  6, 0, 8, 7, 9, 4, 0, 2, 0,
                  0, 0, 9, 1, 0, 5, 0, 0, 0,
                  0, 0, 0, 6, 7, 0, 0, 0, 2,
                  0, 8, 0, 0, 0, 0, 0, 6, 0,
                  0, 9, 0, 4, 3, 0, 0, 5, 0,
                  0, 0, 4, 0, 0, 0, 0, 1, 0,
                  9, 3, 0, 0, 1, 0, 8, 4, 0,
                  0, 0, 2, 0, 4, 3, 0, 0, 0 };

is not the same as a char array with characters inserted .. or am i thinking wrong again?.. so is there some way to have a line with numbers put into the same form as above?

sorry for being such a nub, trying to get better :c

You're right, that's different from:

char sudoku[] = { '0', '0', '1', '0', '0', '0', '0', '0', '9',
                  '6', '0', '8', '7', '9', '4', '0', '2', '0',
                  '0', '0', '9', '1', '0', '5', '0', '0', '0',
                  '0', '0', '0', '6', '7', '0', '0', '0', '2',
                  '0', '8', '0', '0', '0', '0', '0', '6', '0',
                  '0', '9', '0', '4', '3', '0', '0', '5', '0',
                  '0', '0', '4', '0', '0', '0', '0', '1', '0',
                  '9', '3', '0', '0', '1', '0', '8', '4', '0',
                  '0', '0', '2', '0', '4', '3', '0', '0', '0' };

C will convert actual numbers into the ascii character at that index.
To get the char from a number:

char seven = 7 + '0';

Obviously that code is unecessary, but when working with variables it becomes useful.

I hope you got the answers to your questions....

yup, thanks for the help :)

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.