0

How can I read input consisting of unknown number lines chars(same length) by using scanf("%[^\n]%*c", lines[lineCount]) with loops?

//to learn length (?)
for( ; ;columnnumber++){
    
    scanf("%c",&c);
    if(c == '\n'){
        break;
    }
    else{
        lines[0][i] = c;
        i++;
    }
}

lines = realloc(lines,sizeof(char*)*2);
lines[0] = realloc(lines[0],sizeof(char)*columnnumber);
lines[1] = malloc(sizeof(char)*columnnumber);

//tried to use scanf..
while (scanf("%[^\n]%*c", lines[lineCount]) == 1) {
    lineCount++;
    lines = realloc(lines, (lineCount + 1) * sizeof(char *));
    lines[lineCount] = malloc(columnnumber * sizeof(char));
    lineCount++;
}

It does not work. I'm new to C programming if there is a easier way to implement my issue I am also open to your suggestions.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Umit Ogan
  • 1
  • 1
  • You're not adding 1 to the column number for the null terminator. – Barmar May 23 '23 at 19:20
  • Can you use `getline()`? If not, see one of the existing implementations online. `scanf()` is not the right tool here. And you forego `sizeof (char)`; it's defined by the standard to be 1. – Harith May 23 '23 at 19:20
  • 1
    Use `fgets()` to read a line. – Barmar May 23 '23 at 19:20
  • I also tried to use fgets() but I could not handle with unknown number issues. Then I searched a bit and I found this scanf() usage. Thanks for recommendations I will try again – Umit Ogan May 23 '23 at 19:23
  • while (lineCount < MAX_LINES && fgets(lines[lineCount], sizeof(lines[lineCount]), stdin) != NULL) { lineCount++; } (In this usage I'm not using dynamic array and it works well, but I have to use dynamically since maximum values are 90,000) – Umit Ogan May 23 '23 at 19:25
  • Instead of `scanf("%[^\n]%*c", lines[lineCount])` I suggest `scanf(" %[^\n]", lines[lineCount])` which is more aware of the way that various format specifiers handle whitespace. They are focused on *leading* whitespace, and most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. – Weather Vane May 23 '23 at 19:32
  • ... your code tries to read the *next* whitespace that cause the `scanf` to finish. But nobody does that with `%d` or `%f` etc which [leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). So why do that with the `%[^\n]` specifier? As I mentioned: focus on *leading* whitespace. – Weather Vane May 23 '23 at 19:37
  • @chux-ReinstateMonica it was recommended against `"%[^\n]%*c"`. The buffer size is another issue, if you would care to address your remarks to the OP. – Weather Vane May 23 '23 at 20:43
  • 1
    @Umit Ogan, `"scanf(" %[^\n]", lines[lineCount])` is like recommending `gets()`. Both are poor approaches as they risk buffer overrun. Use [`fgets()`](https://stackoverflow.com/questions/76317974/how-can-i-use-scanf-to-read-input-with-an-unknown-number-of-lines-and-characters?noredirect=1#comment134579699_76317974). – chux - Reinstate Monica May 23 '23 at 20:47
  • @chux-ReinstateMonica so what would you do? You've put down an incomplete comment, but failed to suggest anything better. – Weather Vane May 23 '23 at 20:50
  • @WeatherVane Suggestion added. – chux - Reinstate Monica May 23 '23 at 20:52

0 Answers0