Ausgabe in Datei speichers



  • hallo
    ich hab hier vollgendes Programm

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int wert = 10;
    char filename[10] = "datei.dat";
    FILE *fptr;

    fptr = fopen(filename, "w");

    if(fptr == NULL)
    {
    fprintf(stderr, "Fehler beim öffnen der Datei %s.", filename);
    exit(EXIT_FAILURE);
    }
    fprintf(fptr, "Der in der Variablen enthaltene Wert lautet %d.\n", wert);
    fclose(fptr);
    }

    Der Wert der Variable wird auch wie gewollt in der datei.dat gespeichert allerdings überschreibt er bei jedem Neustart des Programms den alten Wert.
    Wie schaffe ich es das der Wert vom letzten Aufruf des Programmes nicht überschrieben wird???



  • wenn Du zum Öffnen der Datei nicht den parameter "w" nimmst, sondern "a", erstellt er die Datei, so sie noch nicht existiert und ansonsten schreibt er in die Datei fortlaufend rein (a=append <- anschließen)



  • Vielen Dank
    jetzt geht es.
    Nun würde ich noch gern wissen ob es möglich ist in die datei.dat das datum hinter der Ausgabe des Programmes einzufügen?



  • Hol dir das Datum im programm und füge es vor da \n in deine fprintf ausgabe ein

    Allerdings hat es sich oft als hilfreich erwiesen, das Datum an den Anfang der zeile zu setzen, dann einen delimiter zeichn und dann den text.

    Hier ein Beispiel fürs DAtum aus der MSDN

    /* LOCALTIM.C: This program uses time to get the current time 
     * and then uses localtime to convert this time to a structure 
     * representing the local time. The program converts the result 
     * from a 24-hour clock to a 12-hour clock and determines the 
     * proper extension (AM or PM).
     */
    
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    void main( void )
    {
            struct tm *newtime;
            char am_pm[] = "AM";
            time_t long_time;
    
            time( &long_time );                /* Get time as long integer. */
            newtime = localtime( &long_time ); /* Convert to local time. */
    
            if( newtime->tm_hour > 12 )        /* Set up extension. */
                    strcpy( am_pm, "PM" );
            if( newtime->tm_hour > 12 )        /* Convert from 24-hour */
                    newtime->tm_hour -= 12;    /*   to 12-hour clock.  */
            if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight. */
                    newtime->tm_hour = 12;
    
            printf( "%.19s %s\n", asctime( newtime ), am_pm );
    }
    
    Output
    
    Tue Mar 23 11:28:17 AM
    

    Hier ein Stück Code das wir benutzen

    char tbuffer[100+1]={""};
    time_t aclock;
    time( &aclock );		 
    strftime(tbuffer, 29,"%Y/%m/%d", localtime( &aclock));
    


  • Dankeschön
    hat alles auf anhieb funktioniert 🙂



  • Mein Programm läuft nun unter Linux. Aber unter Windows bekomme ich nur Fehlermeldungen.
    Folgener Programmteil verursacht den Fehler.

    char   filename	[10] = "alc.log";
    FILE * fptr;
    fptr = fopen	(filename, "a+");
    
    struct 			tm *newtime;
    char 			am_pm[] = "AM";
    			time_t long_time;
    
    time			(&long_time);
    newtime = localtime	(&long_time);
    
    if			(newtime->tm_hour > 12)
    			    strcpy(am_pm, "PM");
    if			(newtime->tm_hour > 12)
    			    newtime->tm_hour -= 12;
    if			(newtime->tm_hour == 0)
    			    newtime->tm_hour = 12;
    
    fprintf 	(fptr, "%.19s %s\n", asctime (newtime), am_pm, "Sie haben" );
    fprintf	(fptr, "%.1f Promille\n", A/(r*G));
    fclose      (fptr)
    

    Und hier noch die Fehlermeldung beim compilieren:

    Compiling...
    alc.c
    error C2143: syntax error : missing ';' before 'type'
    error C2275: 'FILE' : illegal use of this type as an expression
    error C2065: 'filename' : undeclared identifier
    warning C4047: 'function' : 'const char *' differs in levels of indirection
    from 'int '
    warning C4024: 'fopen' : different types for formal and actual parameter 1
    warning C4047: '=' : 'int ' differs in levels of indirection from 'struct _iobuf *'
    error C2143: syntax error : missing ';' before 'type'
    error C2143: syntax error : missing ';' before 'type'
    error C2275: 'time_t' : illegal use of this type as an expression
    error C2146: syntax error : missing ';' before identifier 'long_time'
    error C2065: 'long_time' : undeclared identifier
    error C2065: 'newtime' : undeclared identifier
    warning C4047: '=' : 'int ' differs in levels of indirection from 'struct tm *'
    error C2223: left of '->tm_hour' must point to struct/union
    error C2065: 'am_pm' : undeclared identifier
    warning C4047: 'function' : 'char *' differs in levels of indirection from 'int '
    warning C4024: 'strcpy' : different types for formal and actual parameter 1
    C error C2223: left of '->tm_hour' must point to struct/union
    error C2223: left of '->tm_hour' must point to struct/union
    error C2223: left of '->tm_hour' must point to struct/union
    error C2223: left of '->tm_hour' must point to struct/union
    warning C4047: 'function' : 'const struct tm *' differs in levels of indirection from 'int '
    warning C4024: 'asctime' : different types for formal and actual parameter 1
    error C2059: syntax error : '='
    warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
    warning C4024: 'fprintf' : different types for formal and actual parameter 1
    Cwarning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
    warning C4024: 'fclose' : different types for formal and actual parameter 1
    Error executing cl.exe.

    Was mach ich nur falsch. Unter Linux läuft alles so wie es soll 🙂



  • Schreib die Zeile

    char filename [10] = "alc.log";

    mals so

    char filename [10] = {"alc.log"};

    Soweit ich weis müssen die Initialisier für Vektoren in geschweiften Klammern stehen.



  • PAD schrieb:

    Schreib die Zeile

    char filename [10] = "alc.log";

    mals so

    char filename [10] = {"alc.log"};

    Soweit ich weis müssen die Initialisier für Vektoren in geschweiften Klammern stehen.

    Das hat leider auch nichts gebracht.
    Immer noch die selbe Fehlermeldung.



  • Poste bitte mal die Zeile in der der erste Fehler auftritt + die 10 Zeilen davor und danach.
    Markiere bitte die Fehlerhafte Zeile und sende die dazugehörige Fehlermeldung.

    Vielleicht kann man dann helfen.



  • printf ("\naufgenommener Alkohol in Gramm: ");
    scanf ("%lf" ,&A);

    printf ("\nVerteilungsfaktor im Koerper: ");
    scanf ("%lf" ,&r);

    printf ("\nKoerpergewicht in Kilogramm: ");
    scanf ("%lf" ,&G);

    char filename [10] = "alc.log";
    FILE * fptr;
    fptr = fopen (filename, "a+");

    struct tm *newtime;
    char am_pm[] = "AM";
    time_t long_time;

    time (&long_time);
    newtime = localtime (&long_time);

    if (newtime->tm_hour > 12)
    strcpy(am_pm, "PM");
    if (newtime->tm_hour > 12)
    newtime->tm_hour -= 12;
    if (newtime->tm_hour == 0)
    newtime->tm_hour = 12;

    fprintf (fptr, "%.19s %s\n", asctime (newtime), am_pm, "Sie haben" );
    fprintf (fptr, "%.1f promille\n", A/(r*G));
    fclose

    Fehlermeldung zu der markierten Zeile:
    error C2143: syntax error : missing ';' before 'type'



  • Da wir uns hier in einem C-Programm befinden, ist die Deklaration von Varioablen nur am Anfang eines Blockes erlaubt und nicht irgendwo innerhalb eines Blockes. Die Definition innerhalb eines Blockes ist in C++ zulässig, wenn du die markierte Zeile an den Anfang des Blockes ({) verschiebst
    sollte der Fehlker weg sein



  • Also der Fehler ist nicht mehr da wenn ich die Zeile an den Anfang setzte.
    Allerdings die anderen 14 Fehlermeldungen sind noch da 😞



  • Dann spielen wir unser Spiel noch einmal

    Poste bitte mal die Zeile in der der erste Fehler auftritt + die 10 Zeilen davor und danach.
    Markiere bitte die Fehlerhafte Zeile und sende die dazugehörige Fehlermeldung.

    Vielleicht kann man dann helfen.



  • printf ("\naufgenommener Alkohol in Gramm: ");
    scanf ("%lf" ,&A);

    printf ("\nVerteilungsfaktor im Koerper: ");
    scanf ("%lf" ,&r);

    printf ("\nKoerpergewicht in Kilogramm: ");
    scanf ("%lf" ,&G);

    FILE * fptr;
    fptr = fopen (filename, "a+");

    struct tm *newtime;
    char am_pm[] = "AM";
    time_t long_time;

    time (&long_time);
    newtime = localtime (&long_time);

    if (newtime->tm_hour > 12)
    strcpy(am_pm, "PM");
    if (newtime->tm_hour > 12)
    newtime->tm_hour -= 12;
    if (newtime->tm_hour == 0)
    newtime->tm_hour = 12;

    fprintf (fptr, "%.19s %s\n", asctime (newtime), am_pm, "Sie haben" );
    fprintf (fptr, "%.1f promille\n", A/(r*G));
    fclose

    Fehlermeldung
    error C2275: 'FILE' : illegal use of this type as an expression



  • PAD schrieb:

    Da wir uns hier in einem C-Programm befinden, ist die Deklaration von Variablen nur am Anfang eines Blockes erlaubt und nicht irgendwo innerhalb eines Blockes. Die Definition innerhalb eines Blockes ist in C++ zulässig, wenn du die markierte Zeile an den Anfang des Blockes ({) verschiebst´sollte der Fehlker weg sein

    Gleicher Text wie beim letzten Mal, Allerdings dachte ich das jemand der programmiert auch zu analogschlüssen fähig ist, obwohl diese fähigkeit noch geübt werden muss folgt hier eine Liste von C Zeilen aus deinem Programm die alle Deklaration von Variablen sind und somit dahin müssen wo schon der Filename steht.

    FILE * fptr; 
    fptr = fopen (filename, "a+"); 
    
    struct tm *newtime; 
    char am_pm[] = "AM"; 
    time_t long_time;
    

    Falls nach dem Beseitigen aller falsch positionierten Deklarationen noch Fehler übrig sind, dann

    Aber das Spiel kennen wir ja schon

    p.s.
    Übrigens das es unter Linux läuft, dafür gibt es 2 mögliche Erklärungen dur benutzt dort einen C++ Compiler, die übersetzen 99,.. % aller C-Programme, oder du hast dort einen C- Compiler der dem neuesten Standard (ich glaube C99) unterstützt.



  • so ich hab jetzt

    FILE * fptr;  
    fptr = fopen (filename, "a+");
    

    nach oben verschoben. Das läuft auch. Fehlermeldung für die Zeile ist weg 🙂

    Aber wenn ich

    struct tm *newtime;  
    char am_pm[] = "AM";  
    time_t long_time;
    

    nach oben verschiebe kommt für diese 3 Zeilen folgende Fehlermeldung:
    **
    error C2143: syntax error : missing ';' before 'type'
    error C2143: syntax error : missing ';' before 'type'
    error C2146: syntax error : missing ';' before identifier 'long_time'**



  • Schreib bitte das "#include <time.h>" an das Ende der anderen includes hinzu wie es im Beispiel von mir
    weiter oben steht



  • #include <time.h> war schon da.
    Ich hab eben einfach mal

    struct tm *newtime;   
    char am_pm[] = "AM";   
    time_t long_time;
    

    unter

    void  main (void) {
    

    gesetzt und siehe da 0 errors beim compilieren 🙂

    viele Dank für deine Geduld.


Anmelden zum Antworten