0

I want to read array of floats from stdin and store them in values array, at the same time calculating how many arrays are there in the input.

Given:

-1.0  2.0  1e-1
3.2 2.1 5.6
-1.0 2.0 3e-2

I should get:

arrays = 3

However, this code to catch \n is not working and I do not have a clue how to count rows and assign floats simultaneously. I need to know when \n appears so I could separate arrays apart.

#include <stdio.h>

int main()
{
    int i=0;
    int arrays=0;
    double values[100];
    char c;
    
    while(scanf("%lf, %[\n]c", &values[i], &c) != EOF){    // i want to catch \n into c variable
        i++;
        if(c=="\n"){
            arrays++;
            break;
        }
    }
    return 0;
}

Now I got to this:

#include <stdio.h>

int main()
{
    int count = 0;
    char *line;
    char *data;
    size_t len = 0;
    int i=0;
    double array[100];
    while ((getline(&line, &len, stdin)) != -1){
        printf("%c",line[0]);
        count++;
        while(sscanf(line,"%lf",&array[i])==1){
            i++;
        }
    }
    return 0;
}

Do you see the mistake there? It will get one line and end itself.

wallshock
  • 101
  • 6
  • 6
    Your sample input does not contain commas, so the comma in the format string will never match. Check the value returned by scanf so you know how many conversions were made. – William Pursell Apr 26 '22 at 14:44
  • 5
    Rather than trying to make `scanf` work, it would be much easier to use `fgets` or `getline` – William Pursell Apr 26 '22 at 14:46
  • 2
    You know that `%[\n]c` isn't a format spec? Nor is the `%[\n]s` frequently seen in beginners' code. The `%[...]` specifier isn't a subset of another type, but a distinct type in its own right, with its own specified behaviour. – Weather Vane Apr 26 '22 at 14:51
  • 1
    I suggest you read one line at a time with `fgets()` and then apply `strtok()` to break it into tokens which you can convert with `sscanf()`. Another option is if you know the *maximum* items on one line, you can use the return value from `sscanf()` to determine how many. Suppose it's max 3. Then `int convs = sscanf(str, "%lf%lf%lf", &v1, &v2, &v3);` tells you how many conversions were made. – Weather Vane Apr 26 '22 at 14:53
  • 2
    `scanf` is simply **not** the right tool for this job. – Steve Summit Apr 26 '22 at 15:01
  • 2
    Use `fgets()` or POSIX `getline()` to read the lines — that makes the line counting easy. Use [`sscanf()` in a loop](https://stackoverflow.com/q/3975236/15168) to scan each line, collecting the values. – Jonathan Leffler Apr 26 '22 at 15:37
  • `scanf` treats **any** character that `isspace` as a signal that it should match any number of `isspace` characters. If you have spaces and new lines as separate syntax, you are going to have a lot of trouble reading that file with `scanf`. – Neil Apr 26 '22 at 17:42

1 Answers1

0

Final working code for anyone having the same problem:

#include <stdio.h>

int main()
{
    int count = 0;
    char *line=NULL;
    size_t len = 0;
    int i=0;
    int n;
    double array[200];
    while ((getline(&line, &len, stdin)) != -1){
        //printf("%c",line[0]);
        count++;
        while(sscanf(line,"%lf%n",&array[i],&n)==1){
            //printf("%lf",array[i]);
            line+=n;
            i++;
        }
    }
    return 0;
}

Thanks to everyone for help!

wallshock
  • 101
  • 6