0

I have been struggling scanning the following line of textfile into a char matrix : O A B C D E T

What i tried was this :

fscanf(ifs, "%c %c %c %c %c %c %c",
graphe->name[0][0], graphe->name[1][0], graphe->name[2][0], 
graphe->name[3][0], graphe->name[4][0], graphe->name[5][0],
graphe->name[6][0]
);

For information: ifs is the file pointer, and the .txt file is opening well. I'm precising that name is a char** because i might replace the letters with words someday. Thus, the string length might need to be increased.

Name is a char** being allocated as:

graphe->name = (char**) malloc(sizeof(char*)*graphe->ordre);
for(int i=0;i<graphe->ordre;i++){
    graphe->name[i] = (char*)malloc(sizeof(char));
}

For some reason, this is not working, am I doing anything wrong? Are there better practices in order to accomplish the same task ?

Pianissimo
  • 36
  • 4
  • 1
    If e.g. `graphe->name[0][0]` is a single `char`, then that `fscanf` call should cause the compiler to emit a warning. If it doesn't then enable more warning, and treat them as errors. And perhaps take some time to refresh how to use the `scanf` family of functions in your beginners book. – Some programmer dude Oct 11 '22 at 19:44
  • 1
    Why malloc individual `char`s? Why not just use a 1D string? – Dúthomhas Oct 11 '22 at 19:44
  • By the way, you have tagged your question with the `gets` tag. Why? You don't seem to use the `gets` function, and you should [never use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – Some programmer dude Oct 11 '22 at 19:46
  • 1
    To build on @Dúthomhas comment, if you know there will always be six characters, why not define `name` as a plain array of size elements, as in `char name[6]`? – Some programmer dude Oct 11 '22 at 19:47
  • 2
    And *always* check what `scanf` and family [*returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value). – Some programmer dude Oct 11 '22 at 19:48
  • I'm reading the name of nodes for a graph, this is why i have to read all of them seperately. – Pianissimo Oct 11 '22 at 19:49
  • @Pianissimo What error are you seeing? – Preetham Solomon Oct 11 '22 at 20:10

1 Answers1

0

Disregarding optimisations and recommandation given, I found out putting a space before the first %c solved the thing : fscanf(ifs, " %c %c %c %c %c %c %c",xxx)

Pianissimo
  • 36
  • 4
  • 1
    `fscanf` won't read the ending newline. It will be left in the file for the next input operation. And `%c` will not skip white-space (like most other formats), and newline is a white-space character. Adding an explicit space in the format string will ask all `scanf` functions to skip white-space. – Some programmer dude Oct 11 '22 at 20:23
  • Better `char line[1024];` and then read every line and parse with `sscanf()`, e.g. `while (fgets (line, sizeof line, ifs)) { if (sscanf (line, " %c %c %c %c %c %c %c", &a, &b, &c ...) == 7) { /* you have good input in a, b, c, ... */ } }` – David C. Rankin Oct 11 '22 at 21:41