[Linux, C Language] data written into file incompletely

TKD
TKD used Ask the Experts™
on
Hi, experts:
I read data from database 'tr069db.dat', and store the data into database when press ctrl+c. But I find that the data isn't stored completely.

Original Database File : tr069db.dat
126
DeviceSummary 1 0 -
DeviceInfo. 0 0 -
DeviceInfo.Manufacturer 1 0 III
DeviceInfo.ManufacturerOUI 1 0 820623
DeviceInfo.ModelName 1 0 -
DeviceInfo.Description 1 0 -
DeviceInfo.ProductClass 1 0 ADV
DeviceInfo.SerialNumber 1 0 3388967
DeviceInfo.HardwareVersion 1 0 1.0
DeviceInfo.SoftwareVersion 1 0 1.0
DeviceInfo.EnabledOptions 1 0 -
DeviceInfo.AdditionalHardwareVersion 1 0 -
DeviceInfo.AdditionalSoftwareVersion 1 0 -
DeviceInfo.ProvisioningCode 1 1 -
DeviceInfo.DeviceStatus 1 0 -
DeviceInfo.UpTime 3 0 -
DeviceInfo.FirstUseDate 5 0 -
DeviceInfo.DeviceLog 1 0 -
ManagementServer. 0 0 -
ManagementServer.URL 1 1 http://140.92.63.253:8080/TESTACS/receiver
ManagementServer.Username 1 1 uname
ManagementServer.Password 1 1 psword
ManagementServer.PeriodicInformEnable 4 1 -
ManagementServer.PeriodicInformInterval 3 1 -
ManagementServer.PeriodicInformTime 5 1 -
ManagementServer.ParameterKey 1 0 -
ManagementServer.ConnectionRequestURL 1 0 http://140.92.63.241:8000/Inform?c=testing
ManagementServer.ConnectionRequestUsername 1 1 uname
ManagementServer.ConnectionRequestPassword 1 1 psword
ManagementServer.UpgradesManaged 4 1 -
ManagementServer.KickURL 1 0 -
ManagementServer.DownloadProgressURL 1 0 -
ManagementServer.UDPConnectionRequestAddress 1 0 -
ManagementServer.UDPConnectionRequestAddressNotificationLimit 3 1 -
ManagementServer.STUNEnable 4 1 -
ManagementServer.STUNServerAddress 1 1 -
ManagementServer.STUNServerPort 3 1 -
ManagementServer.STUNUsername 1 1 -
ManagementServer.STUNPassword 1 1 -
ManagementServer.STUNMaximumKeepAlivePeriod 2 1 -
ManagementServer.STUNMinimumKeepAlivePeriod 3 1 -
ManagementServer.NATDetected 4 0 -
GatewayInfo. 0 0 -
GatewayInfo.ManufacturerOUI 1 0 -
GatewayInfo.ProductClass 1 0 -
GatewayInfo.SerialNumber 1 0 -
Config. 0 0 -
Config.PersistentData 1 1 -
Config.ConfigFile 1 1 -
Time. 0 0 -
Time.NTPServer1 1 1 -
Time.NTPServer2 1 1 -
Time.NTPServer3 1 1 -
Time.NTPServer4 1 1 -
Time.NTPServer5 1 1 -
Time.CurrentLocalTime 5 0 -
Time.LocalTimeZone 1 1 -
UserInterface. 0 0 -
UserInterface.PasswordRequired 4 1 -
UserInterface.PasswordUserSelectable 4 1 -
UserInterface.UpgradeAvailable 4 1 -
UserInterface.WarrantyDate 5 1 -
UserInterface.ISPName 1 1 -
UserInterface.ISPHelpDesk 1 1 -
UserInterface.ISPHomePage 1 1 -
UserInterface.ISPHelpPage 1 1 -
UserInterface.ISPLogo 6 1 -
UserInterface.ISPLogoSize 3 1 -
UserInterface.ISPMailServer 1 1 -
UserInterface.ISPNewsServer 1 1 -
UserInterface.TextColor 1 1 -
UserInterface.BackgroundColor 1 1 -
UserInterface.ButtonColor 1 1 -
UserInterface.ButtonTextColor 1 1 -
UserInterface.AutoUpdateServer 1 1 -
UserInterface.UserUpdateServer 1 1 -
UserInterface.AvailableLanguages 1 0 -
UserInterface.CurrentLanguage 1 1 -
LAN. 0 0 -
LAN.AddressingType 1 1 -
LAN.IPAddress 1 1 -
LAN.SubnetMask 1 1 -
LAN.DefaultGateway 1 1 -
LAN.DNSServers 1 1 -
LAN.MACAddress 1 1 -
LAN.MACAddressOverride 4 1 -
LAN.DHCPOptionNumberOfEntries 3 0 -
LAN.DHCPOption. 7 1 -
LAN.Stats. 0 0 -
LAN.Stats.ConnectionUpTime 3 0 -
LAN.Stats.TotalBytesSent 3 0 -
LAN.Stats.TotalBytesReceived 3 0 -
LAN.Stats.TotalPacketsSent 3 0 -
LAN.Stats.TotalPacketsReceived 3 0 -
LAN.Stats.CurrentDayInterval 3 0 -
LAN.Stats.CurrentDayBytesSent 3 0 -
LAN.Stats.CurrentDayBytesReceived 3 0 -
LAN.Stats.CurrentDayPacketsSent 3 0 -
LAN.Stats.CurrentDayPacketsReceived 3 0 -
LAN.Stats.QuarterHourInterval 3 0 -
LAN.Stats.QuarterHourBytesSent 3 0 -
LAN.Stats.QuarterHourBytesReceived 3 0 -
LAN.Stats.QuarterHourPacketsSent 3 0 -
LAN.Stats.QuarterHourPacketsReceived 3 0 -
LAN.IPPingDiagnostics. 0 0 -
LAN.IPPingDiagnostics.DiagnosticsState 1 1 -
LAN.IPPingDiagnostics.Host 1 1 -
LAN.IPPingDiagnostics.NumberOfRepetitions 3 1 -
LAN.IPPingDiagnostics.Timeout 3 1 -
LAN.IPPingDiagnostics.DataBlockSize 3 1 -
LAN.IPPingDiagnostics.DSCP 3 1 -
LAN.IPPingDiagnostics.SuccessCount 3 0 -
LAN.IPPingDiagnostics.FailureCount 3 0 -
LAN.IPPingDiagnostics.AverageResponseTime 3 0 -
LAN.IPPingDiagnostics.MinimumResponseTime 3 0 -
LAN.IPPingDiagnostics.MaximumResponseTime 3 0 -
LAN.TraceRouteDiagnostics. 0 0 -
LAN.TraceRouteDiagnostics.DiagnosticsState 1 1 -
LAN.TraceRouteDiagnostics.Host 1 1 -
LAN.TraceRouteDiagnostics.Timeout 3 1 -
LAN.TraceRouteDiagnostics.DataBlockSize 3 1 -
LAN.TraceRouteDiagnostics.MaxHopCount 3 1 -
LAN.TraceRouteDiagnostics.DSCP 3 1 -
LAN.TraceRouteDiagnostics.ResponseTime 3 0 -
LAN.TraceRouteDiagnostics.NumberOfRouteHops 3 0 -
LAN.TraceRouteDiagnostics.RouteHops. 7 0 -

// main.c
 
#include <signal.h>
#include "file_op.h"
 
void signal_handler(int sig);
struct parameter_array parameters;
 
int main(int argc, char *argv[])
{
        /* Handle Ctrl+C */
        (void) signal(SIGINT, signal_handler);
 
        open_filedb("tr069db.dat", &parameters);
        for (;;)
                ;
        return 0;
}
 
void signal_handler(int sig)
{
        close_filedb("tr069db.dat", &parameters);
        (void) signal(SIGINT, SIG_DFL);
        kill(getpid(), SIGINT);
}
 
// file_op.c
#include <stdio.h>
#include <string.h>
#include "file_op.h"
 
int open_filedb(char *filename, struct parameter_array *parameters)
{
        FILE *fp = NULL;
        struct field record;
        unsigned int num_records = 0;
        int results = 0, index = 0;
 
        /* Open the database */
        fp = fopen(filename, "r");
        if (fp == NULL) {
                perror("Can't open database.");
                return -1;
        }
 
        /* Get the count of records */
        results = fscanf(fp, "%d", &num_records);
        if (results == 1 && num_records > 0) {
                /* Allocate memory to store all parameters */
                parameters->size = num_records;
                parameters->ptrparameter = (struct parameter *)calloc(num_records, sizeof(struct parameter));
                if (parameters->ptrparameter == NULL) {
                        perror("Can't allocate memory");
                        fclose(fp);
                        return -1;
                }
        }
        else {
                perror("No parameter in database");
                fclose(fp);
                return -1;
        }
        while (fscanf(fp, "%s %u %u %s", &record.name, &record.type, &record.is_writable, &record.value) == 4) {
                /* Name */
                parameters->ptrparameter[index].name = (char *)malloc(strlen(record.name) + 1);
                strcpy(parameters->ptrparameter[index].name, record.name);
 
                /* Type */
                parameters->ptrparameter[index].type = record.type;
 
                /* Writable */
                parameters->ptrparameter[index].is_writable = record.is_writable;
 
                /* Value */
                parameters->ptrparameter[index].value = (char *)malloc(strlen(record.value) + 1);
                strcpy(parameters->ptrparameter[index].value, record.value);
 
                fprintf(stderr, "index:%d, parameter:%s\n", index, parameters->ptrparameter[index].name);
                ++index;
        }
 
        if (fp != NULL)
                fclose(fp);
        return 0;
}
int close_filedb(char *filename, struct parameter_array *parameters)
{
        FILE *fp;
        int index, size;
 
        /* Open the database */
        fp = fopen(filename, "w");
        if (fp == NULL) {
                perror("Can't open database.");
                return -1;
        }
 
        size = parameters->size;
 
        // Write all parameters into database and release the allocated memory
        fprintf(fp, "%d\n", size);
        for (index = 0 ; index < size ; ++index) {
                fprintf(stderr, "index:%d, parameter:%s\n", index, parameters->ptrparameter[index].name);
                fprintf(fp, "%s %d %d %s\n",
                        parameters->ptrparameter[index].name,
                        parameters->ptrparameter[index].type,
                        parameters->ptrparameter[index].is_writable,
                        parameters->ptrparameter[index].value);
 
                free(parameters->ptrparameter[index].name);
                free(parameters->ptrparameter[index].value);
        }
 
        return 0;
}
 
// file_op.h
#ifndef FILE_OP_H
#define FILE_OP_H
 
enum datatype {
                XSD_OBJECT = 0,
                XSD_STRING,
                XSD_INT,
                XSD_UNSIGNEDINT,
                XSD_BOOLEAN,
                XSD_DATETIME,
                XSD_BASE64,
};
 
typedef enum {False, True} Boolean;
 
struct parameter
{
        char *name;
        enum datatype type;
        char *value;
        Boolean is_writable;
};
 
struct parameter_array
{
 
 
        int size;
        struct parameter *ptrparameter;
};
 
struct field
{
        char name[64];
        enum datatype type;
        Boolean is_writable;
        char value[1024];
};
 
 
int open_filedb(char *name, struct parameter_array *parameters);
int close_filedb(char *filename, struct parameter_array *parameters);
 
#endif

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
TKD

Author

Commented:
After pressing Ctrl + C...

Database stored after press Ctrl+C
 
126
DeviceSummary 1 0 -
DeviceInfo. 0 0 -
DeviceInfo.Manufacturer 1 0 III
DeviceInfo.ManufacturerOUI 1 0 820623
DeviceInfo.ModelName 1 0 -
DeviceInfo.Description 1 0 -
DeviceInfo.ProductClass 1 0 ADV
DeviceInfo.SerialNumber 1 0 3388967
DeviceInfo.HardwareVersion 1 0 1.0
DeviceInfo.SoftwareVersion 1 0 1.0
DeviceInfo.EnabledOptions 1 0 -
DeviceInfo.AdditionalHardwareVersion 1 0 -
DeviceInfo.AdditionalSoftwareVersion 1 0 -
DeviceInfo.ProvisioningCode 1 1 -
DeviceInfo.DeviceStatus 1 0 -
DeviceInfo.UpTime 3 0 -
DeviceInfo.FirstUseDate 5 0 -
DeviceInfo.DeviceLog 1 0 -
ManagementServer. 0 0 -
ManagementServer.URL 1 1 http://140.92.63.253:8080/TESTACS/receiver
ManagementServer.Username 1 1 uname
ManagementServer.Password 1 1 psword
ManagementServer.PeriodicInformEnable 4 1 -
ManagementServer.PeriodicInformInterval 3 1 -
ManagementServer.PeriodicInformTime 5 1 -
ManagementServer.ParameterKey 1 0 -
ManagementServer.ConnectionRequestURL 1 0 http://140.92.63.241:8000/Inform?c=testing
ManagementServer.ConnectionRequestUsername 1 1 uname
ManagementServer.ConnectionRequestPassword 1 1 psword
ManagementServer.UpgradesManaged 4 1 -
ManagementServer.KickURL 1 0 -
ManagementServer.DownloadProgressURL 1 0 -
ManagementServer.UDPConnectionRequestAddress 1 0 -
ManagementServer.UDPConnectionRequestAddressNotificationLimit 3 1 -
ManagementServer.STUNEnable 4 1 -
ManagementServer.STUNServerAddress 1 1 -
ManagementServer.STUNServerPort 3 1 -
ManagementServer.STUNUsername 1 1 -
ManagementServer.STUNPassword 1 1 -
ManagementServer.STUNMaximumKeepAlivePeriod 2 1 -
ManagementServer.STUNMinimumKeepAlivePeriod 3 1 -
ManagementServer.NATDetected 4 0 -
GatewayInfo. 0 0 -
GatewayInfo.ManufacturerOUI 1 0 -
GatewayInfo.ProductClass 1 0 -
GatewayInfo.SerialNumber 1 0 -
Config. 0 0 -
Config.PersistentData 1 1 -
Config.ConfigFile 1 1 -
Time. 0 0 -
Time.NTPServer1 1 1 -
Time.NTPServer2 1 1 -
Time.NTPServer3 1 1 -
Time.NTPServer4 1 1 -
Time.NTPServer5 1 1 -
Time.CurrentLocalTime 5 0 -
Time.LocalTimeZone 1 1 -
UserInterface. 0 0 -
UserInterface.PasswordRequired 4 1 -
UserInterface.PasswordUserSelectable 4 1 -
UserInterface.UpgradeAvailable 4 1 -
UserInterface.WarrantyDate 5 1 -
UserInterface.ISPName 1 1 -
UserInterface.ISPHelpDesk 1 1 -
UserInterface.ISPHomePage 1 1 -
UserInterface.ISPHelpPage 1 1 -
UserInterface.ISPLogo 6 1 -
UserInterface.ISPLogoSize 3 1 -
UserInterface.ISPMailServer 1 1 -
UserInterface.ISPNewsServer 1 1 -
UserInterface.TextColor 1 1 -
UserInterface.BackgroundColor 1 1 -
UserInterface.ButtonColor 1 1 -
UserInterface.ButtonTextColor 1 1 -
UserInterface.AutoUpdateServer 1 1 -
UserInterface.UserUpdateServer 1 1 -
UserInterface.AvailableLanguages 1 0 -
UserInterface.CurrentLanguage 1 1 -
LAN. 0 0 -
LAN.AddressingType 1 1 -
LAN.IPAddress 1 1 -
LAN.SubnetMask 1 1 -
LAN.DefaultGateway 1 1 -
LAN.DNSServers 1 1 -
LAN.MACAddress 1 1 -
LAN.MACAddressOverride 4 1 -
LAN.DHCPOptionNumberOfEntries 3 0 -
LAN.DHCPOption. 7 1 -
LAN.Stats. 0 0 -
LAN.Stats.ConnectionUpTime 3 0 -
LAN.Stats.TotalBytesSent 3 0 -
LAN.Stats.TotalBytesReceived 3 0 -
LAN.Stats.TotalPacketsSent 3 0 -
LAN.Stats.TotalPacketsReceived 3 0 -
LAN.Stats.CurrentDayInterval 3 0 -
LAN.Stats.CurrentDayBytesSent 3 0 -
LAN.Stats.CurrentDayBytesReceived 3 0 -
LAN.Stats.CurrentDayPacketsSent 3 0 -
LAN.Stats.CurrentDayPacketsReceived 3 0 -
LAN.Stats.QuarterHourInterval 3 0 -
LAN.Stats.QuarterHourBytesSent 3 0 -
LAN.Stats.QuarterHourBytesReceived 3 0 -
LAN.Stats.QuarterHourPacketsSent 3 0 -
LAN.Stats.QuarterHourPacketsReceived 3 0 -
LAN.IPPingDiagnostics. 0 0 -
LAN.IPPingDiagnostics.DiagnosticsState 1 1 -
LAN.IPPingDiagnostics.Host 1 1 -
LAN.IPPingDiagnostics.NumberOfRepetitions 3 1 -
LAN.IPPingDiagnostics.Timeout 3 1 -
LAN.IPPingDiagnostics.DataBlockSize 3 1 -
LAN.IPPingDiagnostics.DSCP 3 1 -
LAN.IPPingDiagnostics.SuccessCount 3 0 -
LAN.IPPingDiagnostics.FailureCount 3 0 -
LAN.IPPingDiagnostics.AverageResponseTime 3 0 -
LAN.IPPingDiagnostics.MinimumResponseTime 3 0 -
LAN.IPPingDiagnostics.MaximumResponseTime 3 0 -
LAN.TraceRouteDiagnostics. 0 0 -
LAN.TraceRouteDiagnostics.DiagnosticsState 1 1 -
LAN.TraceR

Open in new window

TKD

Author

Commented:
The latest data stored in database is "LAN.TraceR"
Most Valuable Expert 2013
Top Expert 2013

Commented:
Hmmm,
this might be completely offbeat, but - is'nt ctl-c SIGTERM and not SIGINT?
 
wmp
 
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Does an fflush(fp) in your close routine help?
Most Valuable Expert 2014
Top Expert 2015
Commented:
int close_filedb(char *filename, struct parameter_array *parameters)
{
        FILE *fp;
        int index, size;
 
        /* Open the database */
        fp = fopen(filename, "w");
        if (fp == NULL) {
                perror("Can't open database.");
                return -1;
        }
 
        size = parameters->size;
 
        // Write all parameters into database and release the allocated memory
        fprintf(fp, "%d\n", size);
        for (index = 0 ; index < size ; ++index) {
                fprintf(stderr, "index:%d, parameter:%s\n", index, parameters->ptrparameter[index].name);
                fprintf(fp, "%s %d %d %s\n",
                        parameters->ptrparameter[index].name,
                        parameters->ptrparameter[index].type,
                        parameters->ptrparameter[index].is_writable,
                        parameters->ptrparameter[index].value);
 
                free(parameters->ptrparameter[index].name);
                free(parameters->ptrparameter[index].value);
        }
 
        return 0;
}
 I don't see an fclose
TKD

Author

Commented:
I forget that. Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial