Forum: Mikrocontroller und Digitale Elektronik Float Mit sprintf einlesen


von icke (Gast)


Lesenswert?

Ich versuche aus einem String einen float Wert wie 3.1 einzulesen. 
Danach möchte ich Komma schieben und und aus 3.1 einen dezimalwert 31 
erhalten. Leider steht in meinem int eine Null.

float fVar = 0;
int iVar = 0;

sprintf(fVar,"%f",text);
fVar *= 10;
iVar = (int)fVar;
printf("Wert: %d", iVar);

Woran kann es liegen? Nutze AVR Studio mit GCC sprint

von (prx) A. K. (prx)


Lesenswert?

Probier's mal mit scanf.

von Mark B. (markbrandis)


Lesenswert?

Du meinst wohl scanf?

von Karl H. (kbuchegg)


Lesenswert?

icke schrieb:
> Woran kann es liegen?


Es liegt mit Sicherheit daran, dass du kein Buch hast, sondern versuchst 
C nach der Methode 'Trial and Error' zu lernen.
Aber auch du wirst lernen, dass das nicht funktioniert.

von Alejandro P. (ale500)


Lesenswert?

Auf Un*x:

$ man scanf
1
NAME
2
     fscanf, scanf, sscanf, vfscanf, vscanf, vsscanf -- input format conversion
3
4
LIBRARY
5
     Standard C Library (libc, -lc)
6
7
SYNOPSIS
8
     #include <stdio.h>
9
10
     int
11
     fscanf(FILE *restrict stream, const char *restrict format, ...);
12
13
     int
14
     scanf(const char *restrict format, ...);
15
16
     int
17
     sscanf(const char *restrict s, const char *restrict format, ...);
18
19
     #include <stdarg.h>
20
     #include <stdio.h>
21
22
     int
23
     vfscanf(FILE *restrict stream, const char *restrict format, va_list arg);
24
25
     int
26
     vscanf(const char *restrict format, va_list arg);
27
     int
28
     vsscanf(const char *restrict s, const char *restrict format, va_list arg);
29
30
DESCRIPTION
31
     The scanf() family of functions scans input according to a format, as described below.  This format may contain
32
     conversion specifiers; the results from such conversions, if any, are stored through the pointer arguments.  The
33
     scanf() function reads input from the standard input stream stdin, fscanf() reads input from the stream pointer
34
     stream, and sscanf() reads its input from the character string pointed to by s.
35
36
     The vfscanf() function is analogous to vfprintf(3) and reads input from the stream pointer stream using a vari-
37
     able argument list of pointers (see stdarg(3)).  The vscanf() function scans a variable argument list from the
38
     standard input and the vsscanf() function scans it from a string; these are analogous to the vprintf() and
39
     vsprintf() functions, respectively.
40
41
     Each successive pointer argument must correspond properly with each successive conversion specifier (but see the
42
     * conversion below).  All conversions are introduced by the % (percent sign) character.  The format string may
43
     also contain other characters.  White space (such as blanks, tabs, or newlines) in the format string match any
44
     amount of white space, including none, in the input.  Everything else matches only itself.  Scanning stops when
45
     an input character does not match such a format character.  Scanning also stops when an input conversion cannot
46
     be made (see below).
47
48
     Extended locale versions of these functions are documented in scanf_l(3).  See xlocale(3) for more information.
49
50
CONVERSIONS
51
     Following the % character introducing a conversion, there may be a number of flag characters, as follows:
52
53
     *        Suppresses assignment.  The conversion that follows occurs as usual, but no pointer is used; the result
54
              of the conversion is simply discarded.
55
     hh       Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a char
56
              (rather than int).
57
58
     h        Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a short int
59
              (rather than int).
60
61
     l (ell)  Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a long int
62
              (rather than int), that the conversion will be one of a, e, f, or g and the next pointer is a pointer to
63
              double (rather than float), or that the conversion will be one of c, s or [ and the next pointer is a
64
              pointer to an array of wchar_t (rather than char).
65
66
     ll (ell ell)
67
              Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a long long
68
              int (rather than int).
69
70
     L        Indicates that the conversion will be one of a, e, f, or g and the next pointer is a pointer to long
71
              double.
72
73
     j        Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a intmax_t
74
              (rather than int).
75
76
     t        Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a ptrdiff_t
77
              (rather than int).
78
79
     z        Indicates that the conversion will be one of dioux or n and the next pointer is a pointer to a size_t
80
              (rather than int).
81
82
     q        (deprecated.)  Indicates that the conversion will be one of dioux or n and the next pointer is a pointer
83
              to a long long int (rather than int).
84
85
     In addition to these flags, there may be an optional maximum field width, expressed as a decimal integer, between
86
     the % and the conversion.  If no width is given, a default of ``infinity'' is used (with one exception, below);
87
     otherwise at most this many bytes are scanned in processing the conversion.  In the case of the lc, ls and l[
88
     conversions, the field width specifies the maximum number of multibyte characters that will be scanned.  Before
89
     conversion begins, most conversions skip white space; this white space is not counted against the field width.
90
91
     The following conversions are available:
92
93
     %     Matches a literal `%'.  That is, ``%%'' in the format string matches a single input `%' character.  No con-
94
           version is done, and assignment does not occur.
95
96
     d     Matches an optionally signed decimal integer; the next pointer must be a pointer to int.
97
98
     i     Matches an optionally signed integer; the next pointer must be a pointer to int.  The integer is read in
99
           base 16 if it begins with `0x' or `0X', in base 8 if it begins with `0', and in base 10 otherwise.  Only
100
           characters that correspond to the base are used.
101
102
     o     Matches an octal integer; the next pointer must be a pointer to unsigned int.
103
104
     u     Matches an optionally signed decimal integer; the next pointer must be a pointer to unsigned int.
105
106
     x, X  Matches an optionally signed hexadecimal integer; the next pointer must be a pointer to unsigned int.
107
108
     a, A, e, E, f, F, g, G
109
           Matches a floating-point number in the style of strtod(3).  The next pointer must be a pointer to float
110
           (unless l or L is specified.)
111
112
     s     Matches a sequence of non-white-space characters; the next pointer must be a pointer to char, and the array
113
114
...

von icke (Gast)


Lesenswert?

>Es liegt mit Sicherheit daran, dass du kein Buch hast, sondern versuchst
>C nach der Methode 'Trial and Error' zu lernen.

Warum gleich immer so böse. Sorry, natürlich meinte ich sscanf, mir ist 
bloß im Eifer des Gefechts sprintf rausgerutscht. Naja wie auch immer, 
ich habe es jetzt auf eine andere Art und Weise erledigt.

von Karl H. (kbuchegg)


Lesenswert?

icke schrieb:
>>Es liegt mit Sicherheit daran, dass du kein Buch hast, sondern versuchst
>>C nach der Methode 'Trial and Error' zu lernen.
>
> Warum gleich immer so böse. Sorry, natürlich meinte ich sscanf,

selbst wenn ich mir statt dem sprintf einen sscanf denke, ist da dann 
immer noch ein Fehler, der in jedem Buch ad nauseam durchgekaut wird.
1
  float fVar = 0;
2
  int iVar = 0;
3
4
  sscanf( fVar, "%f", &text );
5
  fVar *= 10;
6
  iVar = (int)fVar;
7
  printf( "Wert: %d", iVar );

Und dann wird wohl noch die Floating Point Lib notwendig sein.

Warum gleich immer so böse?
Weil wir genau diesen Fall, kein Buch und kein Grundlagenwissen, 
mitlerweile hunderte male am Tag haben (ok. hundert sind übertrieben, 
aber bei >10 halten wir an gut besuchten Tagen). Die Subkultur: "ich 
will zwar alles, wills aber nicht lernen" nimmt überhand. Vielleicht 
sollte das Forum eine Casting-Show veranstalten

von Rufus Τ. F. (rufus) Benutzerseite


Lesenswert?

> Die Subkultur: "ich will zwar alles, wills aber nicht lernen"
> nimmt überhand.
> Vielleicht sollte das Forum eine Casting-Show veranstalten.

Ja, aber wie nennen wir die dann?

DFSDSLR?
(übliche Reihenfolge, F = Forum und LR = Lernresistenten)

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.