Advice on saving to file using fprintf vs fputs and saving file on time schedule.

closed account (Lzvqko23)
I am looking for some advice on saving my file on a time schedule (every X minutes) .. I have included all my relevant code I think re the file.

But I would like to save the file every 5 min just in case. Is this possible ?

Right now .. I do the fclose when the app closes. So if anything happens will I lose all my data .. until I actually do the fclose on application closure ?

(The application can run for days or weeks before being shut down so I would like to save every so often)

Also re the actual writing to the file .. what is more efficient in this case fprintf (commented right now) vs fputs ?

I have I think most of the required code .. but may have omitted a line or two.

The application does work .. so nothing is broken, just looking for advice re efficiency mostly. There is about 51,000 lines of text to the text file every 30 hours. I have determined that the program is writing 100% of the data to the file .. I am not missing any data from the file vs on the screen. The application writes a slightly compressed version to the file than the screen.

This is 16 bit DOS application .. compiled with Watcom compiler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
     
    // Determine Transmission File Naming  
     
     sprintf(tranFn,"%sTran.txt",sysPrefix);   // Transmission Logging  Kevin ver 4.264 June 27/2014
    
    // Open Transmission File -  Append/Text
     
     tranLog = fopen(tranFn,"at"); // append, text     Added Kevin June 27/2014  Transmission / Affiliation Logging   
     
    
    //  Check for Transmission File Opening
    
    if(tranLog == NULL)   // Unknown Affil / Trans Logging   Kevin  June 27/2014
    {
             shouldNever("Transmission File: 0001TRAN.TXT Open Failed");
    }
    
    
    // Write data to Transmission File .. 
    
    strcpy(transcode,"Tr");
    
    LongTimeTmp = localtime(&now);
    sprintf(tmDateResult,"%04d/%02d/%02d %02d:%02d:%02d",1900+LongTimeTmp->tm_year,1+LongTimeTmp->tm_mon,LongTimeTmp->tm_mday,LongTimeTmp->tm_hour,LongTimeTmp->tm_min,LongTimeTmp->tm_sec);
    
    sprintf(radiocode,"%5hu",callingDes->theCode);
    sprintf(transtype,"%s",flagbytes[flags]);
    sprintf(groupcode,"%4hu",destDes->theCode);

    fputs(tmDateResult,tranLog);  // DateTime of Transmission
    fputc(',',tranLog);           // Comma    
    fputs(radiocode,tranLog);     // Transmitting Radio Code
    fputc(',',tranLog);           // Comma
    fputs(transcode,tranLog);     // Tr String
    fputc(',',tranLog);           // Comma 
    fputs(transtype,tranLog);     // Transmission Type  
    fputc(',',tranLog);           // Comma
    fputs(groupcode,tranLog);     // Group Code
    fputc('\n',tranLog);          // End of line
    
    
    // Write to Transmission File Option ? Using fprintf
    
    //LongTimeTmp = localtime(&now);
    //sprintf(tmDateResult,"%04d/%02d/%02d %02d:%02d:%02d",1900+LongTimeTmp->tm_year,1+LongTimeTmp->tm_mon,LongTimeTmp->tm_mday,LongTimeTmp->tm_hour,LongTimeTmp->tm_min,LongTimeTmp->tm_sec);
                            
    // Log Radio Transmission .. ver 4.224 June 23/2014
    //fprintf(tranLog,"%s,Valid Transmission Noted:,%5hu,Tr->,%4hu,%s \n", tmDateResult,callingDes->theCode,destDes->theCode,destDes->titleString());
                 
    
    
    
    // Close of Transmission File 
    
    if(tranLog ) 
    {
        fclose(tranLog);
    } 
    else 
    {
       sprintf(tempArea,"CLOSE of 0001TRAN.TXT failed");
       shouldNever(tempArea);
    } 
Last edited on
fprintf vs fputs: http://stackoverflow.com/a/5690991

As for saving the file, I don't think it should be a problem with the amount of data you appear to be writing. Calling fclose doesn't specifically save the data, but the file won't be closed until all write operations have been completed. I'm guessing your program runs in a loop. If you really want to make sure the data is saved, you could close and re-open the file every time you loop, or every 5, 10, ... times. If you want to specifically save it after a set time period, I believe you would have to use multithreading.
closed account (Lzvqko23)
Thanks ..

I have not had any problems with the program that I can tell .. I was concerned about it not logging all the data, but it does not seem like a problem. I am still doing some testing .. so I will leave it for a few days and see how it does.
closed account (Lzvqko23)
Seems that the program buffers the data by default .. and I have determined that the amount of data is just fine. It is about 315 lines before it is written to the file. That is just fine .. I was more worried about losing hours of data should something happen. But it seems that data is written every few minutes depending on the activity. So .. there really is no issue here.
Topic archived. No new replies allowed.