PHP über Konsole erzeugen
-
HI!
Ich hatte ein PHP-Skript geschrieben.
Jetz möchte ich, dass man mit einem C++ Konsolenprogramm z.b. ein PHP-Skript schreiben kann, wobei ich z.b. Überschrift in HTML angebe.
Ich hatte es mit "fopen" und "fwrite" versucht.
"Fopen" funktioniert, da wird dann eine Datei angelegt. Nun soll der Inhalt in die Datei geschrieben werden, wie mach ich, dass das HTML "MIT" Variablen in die Datei geschrieben wird?
Ich wollte nähmlich ein Skript schreiben, wo ich die Lieder einer Musikgruppe angebe, und das C++ Programm soll das Skript erstellen, und ich möchte nicht immer ein komplettes skript für jede Gruppe schreiben! Deshalb soll das Skript von dem C++ Prgramm erstellt werden, dass ich da dann nur den Namen der Gruppe angebe.
Wie funktioniert "fwrite" so, dass es in eine HTML-Datei schreibt, und den HTML Code nicht als C++ Code nimmt? Und es soll der Inhalt einer Variable (Gruppenname) in die HTML Datei geschrieben werden
Wie geht das?
-
Nur mal so, wieso schreibst nicht ein PHP-Skript welches ein Skript für jede
Gruppe erzeugt und du gibst die Lieder komfortabel über den Browser ein?
-
Hab ich eben gemacht, geht auch
Wäre trozdem mal interessant, wenn ich wüsste, wie fwrite funktioniert!!
-
Aus ISO-C-FDIS.1999-04
7.19.5.3 The fopen function
Synopsis
1 #include <stdio.h>
FILE *fopen(const char * restrict filename,
const char * restrict mode);
Description
2 The fopen function opens the file whose name is the string pointed to by filename,
and associates a stream with it.
3 The argument mode points to a string. If the string is one of the following, the file is
open in the indicated mode. Otherwise, the behavior is undefined.228)
r open text file for reading
w truncate to zero length or create text file for writing
a append; open or create text file for writing at end-of-file
rb open binary file for reading
wb truncate to zero length or create binary file for writing
ab append; open or create binary file for writing at end-of-file
r+ open text file for update (reading and writing)
w+ truncate to zero length or create text file for update
a+ append; open or create text file for update, writing at end-of-file
r+b or rb+ open binary file for update (reading and writing)
w+b or wb+ truncate to zero length or create binary file for update
a+b or ab+ append; open or create binary file for update, writing at end-of-file
4 Opening a file with read mode ('r' as the first character in the mode argument) fails if
the file does not exist or cannot be read.
5 Opening a file with append mode ('a' as the first character in the mode argument)
causes all subsequent writes to the file to be forced to the then current end-of-file,
regardless of intervening calls to the fseek function. In some implementations, opening
a binary file with append mode ('b' as the second or third character in the above list of
mode argument values) may initially position the file position indicator for the stream
beyond the last data written, because of null character padding.
6 When a file is opened with update mode ('+' as the second or third character in the
above list of mode argument values), both input and output may be performed on the
associated stream. However, output shall not be directly followed by input without an
intervening call to the fflush function or to a file positioning function (fseek,
fsetpos, or rewind), and input shall not be directly followed by output without an
intervening call to a file positioning function, unless the input operation encounters endof-
file. Opening (or creating) a text file with update mode may instead open (or create) a
binary stream in some implementations.
7 When opened, a stream is fully buffered if and only if it can be determined not to refer to
an interactive device. The error and end-of-file indicators for the stream are cleared.
Returns
8 The fopen function returns a pointer to the object controlling the stream. If the open
operation fails, fopen returns a null pointer.7.19.8.2 The fwrite function
Synopsis
1 #include <stdio.h>
size_t fwrite(const void * restrict ptr,
size_t size, size_t nmemb,
FILE * restrict stream);Description
2 The fwrite function writes, from the array pointed to by ptr, up to nmemb elements
whose size is specified by size, to the stream pointed to by stream. For each object,
size calls are made to the fputc function, taking the values (in order) from an array of
unsigned char exactly overlaying the object. The file position indicator for the
stream (if defined) is advanced by the number of characters successfully written. If an
error occurs, the resulting value of the file position indicator for the stream is
indeterminate.
Returns
3 The fwrite function returns the number of elements successfully written, which will be
less than nmemb only if a write error is encountered. If size or nmemb is zero,
fwrite returns zero and the state of the stream remains unchanged.
-
HI!
Genau Das:
#include <stdio.h> size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);
Steht auch in meiner Hilfe! Kannst du mir erklären, was "restrict ptr" u.s.w. bedeutet? Ich kann nähmlich nicht allsoviel Englisch!!
-
size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);
Bedeutet:
const void * restrict ptr
-Ist der Zeiger auf die Daten welche du schreiben möchtest
size_t size
-Ist die größe eines dieser Datenblöcke
size_t nmemb
-Ist die Anzahl dieser Datenblöcke
FILE * restrict stream
-Ist dein Dateinhandle
-
Und wie stell ich es an (:D), dass eine Datei angelegt wird, wo einfach nur der Text "Hello World" (Der in einer Variable ist) reingeschrieben wird??
(Nur mal so zum beispiel)
-
Für Textdateien würde ich fprintf verwenden und nicht fwrite.
-
Und wie kann ich damit eine Textdatei mit dem Inhalt "Hello World!" ausgeben??
Und was ist der unterschied zwischen "fprintf" und "fwrite"?
-
fprintf schreibt den Text nur so in eine Datei, fwrite ist glaub ich nur dazu da Binärdateien zu schreiben.
Also:
//Bin Datei muss geöffnet werden int Feld[5] = {1, 4, 8, 133, 19}; fwrite(&Feld,sizeof(Feld),1,Datei); // Datei ist der Zeiger
schreibt eine Binärdatei, hab ich nur schnell gecodet, können Fehler drin sein....
Und jetzt fprintf...
//TXT Datei muss geöffnet werden fprintf(Datei, "Der Text steht in der Datei"); //Datei ist der Zeiger
Hab ich auch nur schnell gecodet...
Ich hoffe ich konnte dir fprintf was näher bringen, also wie prinitf nur das der Zeiger an den Anfang muss...robotangel