Ausgabe in Datei speichers



  • 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