 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
herborner@gmx.de Guest
|
Posted: Sun Jun 26, 2005 5:18 pm Post subject: fprintf oder printf |
|
|
Hallo Zusammen,
c++ ist für mich noch Neuland. Kann mir jemand vielleicht den
unterschied zwischen "printf" und "fprintf" oder "scanf" zu "fscanf"
erklären und wann setze ich welche Variante ein?
Danke
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Hannah Schroeter Guest
|
Posted: Sun Jun 26, 2005 8:35 pm Post subject: Re: fprintf oder printf |
|
|
Hallo!
<herborner (AT) gmx (DOT) de> wrote:
| Quote: | Hallo Zusammen,
c++ ist für mich noch Neuland. Kann mir jemand vielleicht den
unterschied zwischen "printf" und "fprintf" oder "scanf" zu "fscanf"
erklären und wann setze ich welche Variante ein?
|
Fast alle hier werden Dich darauf hinweisen, daß Streams das eigentliche
C++-Konzept sind.
printf(foo, bar, baz) entspricht fprintf(stdout, foo, bar, baz)
scanf(foo, bar, baz) entspricht fscanf(stdin, foo, bar, baz)
So einfach ist das.
Gruß,
Hannah.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sun Jun 26, 2005 8:37 pm Post subject: Re: fprintf oder printf |
|
|
[email]herborner (AT) gmx (DOT) de[/email] writes:
| Quote: | c++ ist für mich noch Neuland. Kann mir jemand vielleicht den
unterschied zwischen "printf" und "fprintf" oder "scanf" zu "fscanf"
erklären
|
fprintf() macht formatierte Ausgabe in ein File.
printf() macht formatierte Ausgabe nach "standard output". D.h wenn Du
ein Programm auf der Kommandozeile startest, erscheint die durch
printf() produzierte Ausgabe erscheint typischerweise im gleichen
Fenster unter der Kommandozeile.
[In einer anständigen Shell kannst Du die vom Program nach "standard
output" geschriebene Ausgabe auch in eine umleiten oder einem weiteren
Prozess als "standard input" schicken.]
Der Ausdruck
printf(x,y,z)
ist also gleichbedeutend mit
fprintf(stdout,x,y,z)
Analoges gilt für scanf() und fscanf(). fscanf() macht formatiert
Eingabe aus einer Datei, scanf() von "standard input".
Der Ausdruck
scanf(x,y,z)
ist also gleichbedeutend mit
fscanf(stdin,x,y,z)
| Quote: | und wann setze ich welche Variante ein?
|
Das ist hoffentlich jetzt klar.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Horst Kraemer Guest
|
Posted: Mon Jun 27, 2005 11:02 am Post subject: Re: fprintf oder printf |
|
|
[email]herborner (AT) gmx (DOT) de[/email] wrote:
| Quote: | Hallo Zusammen,
c++ ist für mich noch Neuland. Kann mir jemand vielleicht den
unterschied zwischen "printf" und "fprintf" oder "scanf" zu "fscanf"
erklären und wann setze ich welche Variante ein?
|
printf(toto,titi) entspricht fprintf(stdout,toto,titi)
scanf(toto,titi) entspricht fcsanf(stdin,toto,titi),
Man verwendet die Varianten ohne f also, wenn man auf die
Standardausgabe (i.a. Bildschirm) augibt bzw. von der Standardeingabe
(i.a. Tastatur) einliest und spart dabei etwas Schreibarbeit.
[Man kann auch printf zum Scheiben in eine Datei verwenden, wenn man
das betraffende Programm z.B. per
myprog >myfile
aufruft. Dann wird vor Start des Programms die Datei "myfile" vom
Betriebssystem zum Schreiben geoeffnet und mit der Standardausgabe
"stdout" des C-Programms assoziiert. D.h. der "Bildschirm" ist fuer
diesen Programmaufruf dann die Datei...
]
Achtung:
puts(s) entspricht fputs(s,stdout);fputs("n",stdout)
d.h. puts haengt im Gegensatz zu fputs noch ein Zeilenendezeichen an
und ausserdem hat fputs den stream-Parameter hinten und nicht vorn
--
Horst
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|