Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Programma in C.

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2015
    Messaggi
    18

    Programma in C.

    Ciao a tutti, devo scrivere un programma che, inserite due date, mi restituisca la differenza in giorni. Devo, inoltre, inserire le due date nel file "input.txt" e il risultato nel file "output.txt".
    Non so come scrivere l'fprintf del file "input.txt", potreste darmi una mano?
    Il codice è il seguente.

    codice:
    #include <stdio.h>
    #include <time.h>
    int main ()
    {
        static struct tm date1;
        static struct tm date2;
        time_t start, end, startdate, enddate;
        double diffsec;
        int d, m, y;
        FILE *in, *out;
        system ("color 1b");
        time (&start);
    
    
        in = fopen ("input.txt", "w");
        out = fopen ("output.txt", "w");
    
    
        do
        {
            printf ("Enter the start-date using the slash bar (/): ");
            scanf ("%d/%d/%d", &d, &m, &y);
            date1.tm_mday = d;
            date1.tm_mon = m-1;
            date1.tm_year = y - 1900;
    
    
            printf ("Enter the end-date using the slash bar (/): ");
            scanf ("%d/%d/%d", &d, &m, &y);
            date2.tm_mday = d;
            date2.tm_mon = m-1;
            date2.tm_year = y - 1900;
        }
        while (mktime(&date2) < mktime(&date1) || d>31 || m>12 || y<1900 || d==0 || m==0);
    
    
        fprintf (in, "", ); //COSA DEVO SCRIVERE?
        fprintf(out, "%.f", difftime(mktime(&date2), mktime(&date1))/86400);
        fclose (in);
        fclose (out);
        time (&end);
        diffsec = difftime (end, start);
    
    
        printf ("\n");
        if (difftime(mktime(&date2), mktime(&date1))/86400 == 1)
            printf ("The difference between the dates is 1 day\n");
        else
            printf ("The difference between the dates is %.f days\n", difftime(mktime(&date2), mktime(&date1))/86400);
        printf ("\n");
        printf ("Time of execution: %f seconds\n", diffsec);
        printf ("\n");
        system ("PAUSE");
    }

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,472
    fprintf è usata per scrivere. Per leggere userai la fscanf
    No MP tecnici (non rispondo nemmeno!), usa il forum.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.