Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Accessing files from various functions

Reply
Thread Tools

Accessing files from various functions

 
 
Lorenzo Mugnai
Guest
Posts: n/a
 
      08-12-2003
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[i]);

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);
};
 
Reply With Quote
 
 
 
 
Derk Gwen
Guest
Posts: n/a
 
      08-12-2003
# 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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
If your job was as meaningless as theirs, wouldn't you go crazy too?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Direct support for various Statistical functions Michael Angelo Ravera C Programming 1 12-18-2009 07:32 AM
How does one locate the java executable and various JAR files in the classpath? Oliver Wong Java 12 12-10-2005 10:21 PM
Functions that accept various type? QQ C Programming 8 12-14-2004 11:49 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Difference between various functions SDZ C Programming 18 06-16-2004 06:22 AM



Advertisments