-3

i'm new programmer in c and need some help. I'm trying to read text file which contains numbers (long doubles) separated by ','. My problem is that I don't know what is the size of each line and every solution I find online assume it's size. This is a part of work I got to do and I can't use scanf/fscanf. finally I would like to have array contains the numbers (without the ','), what is the best way to do it? Thanks a lot!

edited:

 FILE *inputFile;
inputFile = fopen("C:\\Users\\studies\\C\\Exe1\\input_example.txt",
                  "r");
    for (int c = 0; c < 7; c++) {
        fscanf(inputFile, "%Lf,", &params[c]);
    }

any other way I tried to read just didn't go well, fgets, getchar, etc..

Aviad Bs
  • 1
  • 2
  • 1
    Why can you not use `scanf`? Ps this is not a homework writing service – Ed Heal Aug 01 '17 at 17:44
  • 1
    You could count lines and "," first – florgeng Aug 01 '17 at 17:44
  • Read string upto `,` (Use `getdelim` or `getline` if available) then `strtold` – BLUEPIXY Aug 01 '17 at 17:45
  • 3
    "'I'm trying to read text file which contains numbers (long doubles) separated by ','" --> post that code of that try. – chux - Reinstate Monica Aug 01 '17 at 17:46
  • Ed I know it's not a homework service, I just looked a lot online and couldn't find a good way to replace scanf. I edit my message and put my code using scanf. – Aviad Bs Aug 01 '17 at 17:49
  • Questions asking how to (a) read lines and fields and/or (b) store them in a dynamically-allocated array are among the most common on this site, asked multiple times per day. Isn't there supposed to be a mechanism ("community wiki" or something like that) where we can compose a single, good, canonical answer, and simply point each new occurrence to it? – Steve Summit Aug 01 '17 at 18:21
  • @SteveSummit Unfortunately this post has 1) unlimited file length, 2) unlimited line length 3) unlimited text length for a single `long double` and 40 bars use of `fscanf()`. A canonical answer would not have all those restrictions. Taken all together the coding goal is not reasonable. – chux - Reinstate Monica Aug 01 '17 at 18:33
  • @chux Well, me, I'd say that unlimited length and width are both utterly canonical. Clearly it would make sense to have 3 canonical variants, for 2D arrays of string, int, and double, which would be near-identical except for calls to `atoi` and `atof` or the equivalent. (Presumably the poster of this question could make the leap to `strtold`.) Finally, *scanf are useless for this, so I'd leave them out of my canonical answer anyway. – Steve Summit Aug 01 '17 at 18:38
  • 1
    @SteveSummit Curious, what limitation do you see with `fscanf()`? Overflow or something else? – chux - Reinstate Monica Aug 01 '17 at 18:44
  • @chux In general, see https://stackoverflow.com/questions/2430303/disadvantages-of-scanf and http://c-faq.com/stdio/scanfprobs.html. For this question: no way to scan one line for arbitrary number of fields, and if you scan one field at a time, you can't tell the difference between delimiters and newlines, so you don't know the shape of the array. (I suppose you could tease that out somehow, but it's just not worth it.) – Steve Summit Aug 01 '17 at 18:51
  • I'd counter that teasing out the `'\n'` is not so hard [example](https://stackoverflow.com/a/31666357/2410359) and so `fscanf()` solution would be easy enough (Aside from OF). (BTW: those links primary address difficulty in reading strings, not numbers) Yet I agree that a canonical answer, given various set of conditions, would be useful. – chux - Reinstate Monica Aug 01 '17 at 18:58
  • @BLUEPIXY getdelim() and getline() aren't available on Windows, are they? – Bjorn A. Aug 01 '17 at 19:09
  • @BjornA. Until the question was edited, it was not known that the usage environment of the OP was the windows environment. However there is an implementation for windows. – BLUEPIXY Aug 01 '17 at 19:14

1 Answers1

1

Divide and conquer! See if you're able to read the file correctly without storing anything. Just read and print what you read, so you can compare your output with the input file. If they match, you're on your way.

It's easier than you think to read the file. Use a char array as a temporary buffer for each number and read the file character by into the buffer. If the input is a ',' then you have read a complete number. Same goes for the newline '\n'.

// untested snippet
char buf[1024]; // Make it big
size_t i = 0;
int c;
long double d;

while ((c = fgetc(fp)) != EOF) {
    if (c == ',' || c == '\n') {
        buf[i] = '\0';
        d = strtold(buf);
        printf("%lf%c", d, c); // debugging, sanity check
        i = 0;
    }
    else
        buf[i++] = c;
}

There may be uncovered corner cases which the snippet doesn't cover, like missing newline at end of file, or silly Windows \r\n combos. Also, string to double conversion needs proper error checking. Still, the snippet should get you going.

Bjorn A.
  • 1,148
  • 9
  • 12