2
  while (fscanf(ifp, " %d %d kl. %d %s - %s %d - %d %d\n", &round, &date, &clock, teeam,  &goals, &attendance)

I should probably know this, but the second %d should import a date to my variable, like 20.20.2012 but instead i only get the first 20 and not rest of it.

thank you :)

Winkz
  • 329
  • 2
  • 5
  • 19

2 Answers2

6

Internally, the string is read as long as it represents a valid integer (so it stops when '.' is encountered).

How would you represent the date as one integer? You could have 3 variables and read them like this:

fscanf(ifp, "%d.%d.%d", &day, &month, &year);

By the way, 20/20 is a strange date :-)

ales_t
  • 1,967
  • 11
  • 10
  • Haha yea 20/20 is a strange date! But i would like to represent it as 20.10.2012 – Winkz Nov 25 '12 at 14:21
  • You mean as a string? You can use dasblinkenlight's solution, or you can always get it again from the 3 variables: char date_str[11]; date_str = sprintf("%02d.%02d.%04d", day, month, year); – ales_t Nov 25 '12 at 14:31
  • I took your way of writting it, as 3 variables, but could you tell me how and if its possible to make something like 2 - 2 into one variable? – Winkz Nov 25 '12 at 14:35
  • If you want to evaluate an arithmetic expression, a related post is here: http://stackoverflow.com/questions/9329406/evaluating-arithmetic-expressions-in-c – ales_t Nov 25 '12 at 14:38
3

The %d format specifier lets you scan a single numeric value, not a sequence of three numbers.

You can read a date in the format that you expect like this:

char date_buf[11];
scanf("%10[0-9.]", date_buf);

The text is not parsed as three ints, but stored as a text instead. You can break it into a month, a day, and a year like this:

int month = atoi(&date_buf[0]);
int day = atoi(&date_buf[3]);
int year = atoi(&date_buf[6]);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523