L
Lorenzo Mugnai
Hi everyone,
Below is my code which basically opens a file and appends some text if
the file is below a certain size, if not it archives the file and
begins a new one. The problem is i have four files with identical code
(noted in the code below) which i would like in my general header
file. When i try this the compiler complains that the remaining code
in the c files needs to access the file to but can't.
Could somebody point out how to go abouts doing this?
Another problem I have is using the system function. Is there any
other ways of doing this?
Thanks
CODE:
VOID_EXPORT EXITSUP(unsigned char Interface)
{
/* IDENTICAL CODE STARTS HERE */
/* Global Variables */
FILE * pRecorderFile; /* File to save records */
FILE * pArchiveHistory; /* File to save last archive number */
FILE * pArchiveFile; /* Archived File */
/* Variables used for checksum calculation */
MD5_CTX context;
int len = 0;
int i = 0;
unsigned char buffer[1024], digest[16];
char versionNumber[5] = "0000";
char szFileRecorderPath[35] = {0};
char szFileArchivePath[35] = {0};
char szFileArchiveHisPath[35] = {0};
char szArchiveNumber[4] = {0};
char szCopyArchive[80] = {0};
char szMachineNo[7] = {0};
int archiveNo = 0;
long fileSize;
/* FileRecorder path */
sprintf(szFileRecorderPath, "C:\\DSPMRECS\\test.txt");
/* Assign path for arhive history file */
sprintf(szFileArchiveHisPath, "C:\\DSPMRECS\\ARCHIVE.HIS");
if(!FileExists("C:\\DSPMRECS"))
{
system("mkdir c:\\DSPMRECS");
}
if(FileExists(szFileRecorderPath))
{
/* Open file and append data */
pRecorderFile = fopen(szFileRecorderPath, "a");
/* If the file has been opened */
if(pRecorderFile)
{
/* Check size of file */
fseek(pRecorderFile, 0L, SEEK_END);
fileSize = ftell(pRecorderFile);
/* if the file is larger than 1.3MB, archive */
/* file and create new file*/
if(fileSize > iMaxFileRecorderSize)
{
/* Check to see if a file has been archived in the past */
if(FileExists(szFileArchiveHisPath))
{
/* Open file for reading */
pArchiveHistory = fopen(szFileArchiveHisPath, "r");
/* Get the number of the last archived file */
fgets(szArchiveNumber, 4, pArchiveHistory);
fclose(pArchiveHistory);
/* Empty the file archive history file*/
pArchiveHistory = fopen(szFileArchiveHisPath, "w");
/* Increment the archive number in the history file */
/* and write the new number to the archive history file */
archiveNo = atoi(szArchiveNumber);
archiveNo = archiveNo + 1;
sprintf(szArchiveNumber, "%03d", archiveNo);
fputs(szArchiveNumber, pArchiveHistory);
fclose(pArchiveHistory);
}
else
{
/* Create a file to keep history number for the */
/* first time and add number 000 */
pArchiveHistory = fopen(szFileArchiveHisPath, "w+");
sprintf(szArchiveNumber, "%03d", 0);
fputs(szArchiveNumber, pArchiveHistory);
fclose(pArchiveHistory);
fclose(pRecorderFile);
}
/* Build the path for the new archive file */
sprintf(szFileArchivePath, "C:\\DSPMRECS\\test.txt);
/* Copy the .REC file to the new archive file */
sprintf(szCopyArchive, "Copy %s %s", szFileRecorderPath,
szFileArchivePath);
fclose(pRecorderFile);
system(szCopyArchive);
/* Open file for reading */
pArchiveFile = fopen(szFileArchivePath, "rb");
/* Perform Checksum */
MD5Init(&context);
while (len = fread (buffer, 1, sizeof(buffer), pArchiveFile))
MD5Update (&context, buffer, len);
MD5Final (digest, &context);
fclose(pArchiveFile);
/* Open file to append checksum */
pArchiveFile = fopen(szFileArchivePath, "a");
fprintf(pArchiveFile, "&");
for (i = 0; i < 16; i++)
fprintf (pArchiveFile, "%02x", digest);
fclose(pArchiveFile);
//COPY_FILE(szFileRecorderPath, szFileArchivePath);
/* Empty the original recorder file */
pRecorderFile = fopen(szFileRecorderPath, "w");
/* Write version number to top of file */
fprintf(pRecorderFile, "%s%c\n", versionNumber, '\0');
}/* END - if(fileSize > iMaxFileRecorderSize) */
}/* END - if(pRecorderFile)*/
}/* END - if(FileExists(szFileRecorderPath))*/
else
{
/* Create file as it does not exist */
pRecorderFile = fopen(szFileRecorderPath, "w");
/* Write version number to top of file */
fprintf(pRecorderFile, "%s%c\n", versionNumber, '\0');
}
/* IDENTICAL CODE ENDS HERE */
fputs(BuildRecord(3, 0), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 1), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 2), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 3), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 9), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 10), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fclose(pRecorderFile);
};
Below is my code which basically opens a file and appends some text if
the file is below a certain size, if not it archives the file and
begins a new one. The problem is i have four files with identical code
(noted in the code below) which i would like in my general header
file. When i try this the compiler complains that the remaining code
in the c files needs to access the file to but can't.
Could somebody point out how to go abouts doing this?
Another problem I have is using the system function. Is there any
other ways of doing this?
Thanks
CODE:
VOID_EXPORT EXITSUP(unsigned char Interface)
{
/* IDENTICAL CODE STARTS HERE */
/* Global Variables */
FILE * pRecorderFile; /* File to save records */
FILE * pArchiveHistory; /* File to save last archive number */
FILE * pArchiveFile; /* Archived File */
/* Variables used for checksum calculation */
MD5_CTX context;
int len = 0;
int i = 0;
unsigned char buffer[1024], digest[16];
char versionNumber[5] = "0000";
char szFileRecorderPath[35] = {0};
char szFileArchivePath[35] = {0};
char szFileArchiveHisPath[35] = {0};
char szArchiveNumber[4] = {0};
char szCopyArchive[80] = {0};
char szMachineNo[7] = {0};
int archiveNo = 0;
long fileSize;
/* FileRecorder path */
sprintf(szFileRecorderPath, "C:\\DSPMRECS\\test.txt");
/* Assign path for arhive history file */
sprintf(szFileArchiveHisPath, "C:\\DSPMRECS\\ARCHIVE.HIS");
if(!FileExists("C:\\DSPMRECS"))
{
system("mkdir c:\\DSPMRECS");
}
if(FileExists(szFileRecorderPath))
{
/* Open file and append data */
pRecorderFile = fopen(szFileRecorderPath, "a");
/* If the file has been opened */
if(pRecorderFile)
{
/* Check size of file */
fseek(pRecorderFile, 0L, SEEK_END);
fileSize = ftell(pRecorderFile);
/* if the file is larger than 1.3MB, archive */
/* file and create new file*/
if(fileSize > iMaxFileRecorderSize)
{
/* Check to see if a file has been archived in the past */
if(FileExists(szFileArchiveHisPath))
{
/* Open file for reading */
pArchiveHistory = fopen(szFileArchiveHisPath, "r");
/* Get the number of the last archived file */
fgets(szArchiveNumber, 4, pArchiveHistory);
fclose(pArchiveHistory);
/* Empty the file archive history file*/
pArchiveHistory = fopen(szFileArchiveHisPath, "w");
/* Increment the archive number in the history file */
/* and write the new number to the archive history file */
archiveNo = atoi(szArchiveNumber);
archiveNo = archiveNo + 1;
sprintf(szArchiveNumber, "%03d", archiveNo);
fputs(szArchiveNumber, pArchiveHistory);
fclose(pArchiveHistory);
}
else
{
/* Create a file to keep history number for the */
/* first time and add number 000 */
pArchiveHistory = fopen(szFileArchiveHisPath, "w+");
sprintf(szArchiveNumber, "%03d", 0);
fputs(szArchiveNumber, pArchiveHistory);
fclose(pArchiveHistory);
fclose(pRecorderFile);
}
/* Build the path for the new archive file */
sprintf(szFileArchivePath, "C:\\DSPMRECS\\test.txt);
/* Copy the .REC file to the new archive file */
sprintf(szCopyArchive, "Copy %s %s", szFileRecorderPath,
szFileArchivePath);
fclose(pRecorderFile);
system(szCopyArchive);
/* Open file for reading */
pArchiveFile = fopen(szFileArchivePath, "rb");
/* Perform Checksum */
MD5Init(&context);
while (len = fread (buffer, 1, sizeof(buffer), pArchiveFile))
MD5Update (&context, buffer, len);
MD5Final (digest, &context);
fclose(pArchiveFile);
/* Open file to append checksum */
pArchiveFile = fopen(szFileArchivePath, "a");
fprintf(pArchiveFile, "&");
for (i = 0; i < 16; i++)
fprintf (pArchiveFile, "%02x", digest);
fclose(pArchiveFile);
//COPY_FILE(szFileRecorderPath, szFileArchivePath);
/* Empty the original recorder file */
pRecorderFile = fopen(szFileRecorderPath, "w");
/* Write version number to top of file */
fprintf(pRecorderFile, "%s%c\n", versionNumber, '\0');
}/* END - if(fileSize > iMaxFileRecorderSize) */
}/* END - if(pRecorderFile)*/
}/* END - if(FileExists(szFileRecorderPath))*/
else
{
/* Create file as it does not exist */
pRecorderFile = fopen(szFileRecorderPath, "w");
/* Write version number to top of file */
fprintf(pRecorderFile, "%s%c\n", versionNumber, '\0');
}
/* IDENTICAL CODE ENDS HERE */
fputs(BuildRecord(3, 0), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 1), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 2), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 3), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 9), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fputs(BuildRecord(3, 10), pRecorderFile);
/* print EOR marker and new line */
fprintf(pRecorderFile, "%c\n", '\0');
fclose(pRecorderFile);
};