0

I am trying to scan from a .txt file with two floats with an hyphen in between them. The code works if the hyphen is removed, but I am doing a job for school purposes and the hyphen is needed.

The function is supposed to load the info from the file to an array.

Here is the code:

#include <stdio.h>
#include <estruturas.h>
#include <funcoes.h>

void loadmedicos(medico *pmedico,int total){

    FILE *f;
    medico x; //medico is a typedefined struct with 2 floats
    f=fopen("medicos.txt","rt");

    if(f==NULL){
        printf("Ocorreu um erro ao abrir 'medicos.txt'!\n\n");
    }
    for(int i=0;i<total;i++){
        fscanf(f,"%f",&x.horarioentrada);
        fscanf(f,"%f",&x.horariosaida);
        *(pmedico+i)=x;
    }

    fclose(f);
}

If the .txt contains:

19.30 20.30

Then it will read correctly and output those numbers. If the file contains:

19.30-20.30

The second number won't be read. Why is that and how can I fix it?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Supra
  • 3
  • 1
  • Perhaps a [`scanf` (and family)](http://en.cppreference.com/w/c/io/fscanf) reference could be useful? Especially about the format string. Something any [good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) or any good online tutorial should have told you as well. – Some programmer dude Aug 16 '17 at 16:50
  • 1
    How do you know that the second number won't be read? You don't test the return value from `fscanf()`, so you've no idea whether either conversion worked? It isn't clear why you don't use `if (fscanf("%f-%f", &x.horarioentrada, *x.horariosaida) != 2) { …oops; something went wrong… }` — and the 'oops' part is when it becomes better to use `fgets()` to read the line of input and `sscanf()` to parse it; then you can see what was causing trouble more easily. Are the structure members actually of type `float`? – Jonathan Leffler Aug 16 '17 at 16:54
  • 2
    You seem satisfied with the answer but I cannot reproduce. A text file containing your `19.3-20.3` scans and prints `19.299999 -20.299999`. I realise you didn't want to scan the `-` but I do not get "The second number won't be read." – Weather Vane Aug 16 '17 at 17:16
  • Note that if you fail to open the file, you report the error, but then proceed to use the null file pointer, which will probably crash and certainly won't work correctly. It is good to report the problem; it is better if the error message identifies the file that can't be opened by name; it is better still if you ensure that you don't use the failed file stream — in the `for` loop of `fclose()` call — once you've identified that there is a problem. – Jonathan Leffler Aug 16 '17 at 17:19
  • At first it didn't work, but if you have spacing in the txt file and in the fscanf line it works fine for me. file: 13.30 - 20.30 line: fscanf(f,"%f - %f",&x.horarioentrada,&x.horariosaida); – Supra Aug 16 '17 at 18:34
  • "The second number won't be read." is certainly an error in OP's post. – chux - Reinstate Monica Aug 16 '17 at 18:50

1 Answers1

1

You need to include the "-" character as part of the scanf format. That way, the character is skipped over:

fscanf(f, "%f-%f", &x.horarioentrada, &x.horariosaida);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Negative numbers are fine — using the code in the question (not your answer) the 19.30-20.30 should be read as +19.30 and -20.30 without problem, shouldn't it? Note that's not disputing the fix (which is what I'd expect, give or take checking the result of the function call); it just isn't clear that that the diagnosis is correct. – Jonathan Leffler Aug 16 '17 at 16:55
  • @JonathanLeffler It gets read as +19.30 and +20.30. This is assuming the hyphen is the delimiter. – dbush Aug 16 '17 at 16:58
  • Yes — I've clarified my comment to asking why your diagnosis of the code in the question is right; I still don't dispute that what you're suggesting is a fix to the problem. – Jonathan Leffler Aug 16 '17 at 16:59
  • @JonathanLeffler The way I read it, the file contains numbers separated by a hyphen. The part about "the code works if the hyphen is removed" sounds like experimentation on OP's part, reinforced by "but I am doing a job for school purposes *and the hyphen is needed*". – dbush Aug 16 '17 at 17:02
  • We're probably arguing over nothing, but…the code `#include ` — `int main(void) { float x, y; if (sscanf("19.30-20.30", "%f%f", &x, &y) != 2) printf("Oops!\n"); else printf("%+6.2f %+6.2f\n", x, y); return 0; }` is equivalent to the OP's code and the desired input and produces as output `+19.30 -20.30`. Granted, the original has two `fscanf()` calls instead of one `sscanf()` call, but the code is equivalent, not identical. And the result is the same — two numbers successfully read, one negative. I'm not for a second disputing the fix. I'm just not understanding the original problem. – Jonathan Leffler Aug 16 '17 at 17:09
  • 1
    Given that the OP is happy with the answer, which is what I'd suggest anyway, we can let it subside. I'm still unhappy that I don't see why the original code "didn't work" — or how it was known not to work. But the fix seems to work and is fine with me. So, let sleeping dogs lie. – Jonathan Leffler Aug 16 '17 at 17:12