Accessing files from various functions

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);
};
 
D

Derk Gwen

# 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.

It's not clear to me what you are saying.

Where the common code was in each file, you have a
#include "commoncode-filename"
with common code put into commoncode-filename? And the compiler
is complaining about the #include directive?

If so, you're going to have to consult your compiler specific method
of listing include paths. On unix compilers, you usually do that with
a -Ipath parameter, but I don't know about windows. You do have to
get the correct file name; any backslashes in the include file string
should be escaped (doubled) like other strings.

# Another problem I have is using the system function. Is there any
# other ways of doing this?

Windows should provide an os specific interface to do this. How tied to
specific os you want your program to be is your decision: however if
the system() call does what you want without any problems, I wouldn't
be worried about efficiency or aesthetics. Otherwise you might find
better information about the file system api on a microsoft grup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top