473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Accessing files from various functions

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);
};
Nov 13 '05 #1
1 2213
# 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?
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JakeP | last post by:
how should you access the dos os from a c program whuch as djgpp? need to write an app that would for example get a list of all *.h files and store them in a linked list so the names need to be...
6
by: Bob Alston | last post by:
I am looking for Access reporting add-in that would be easy to use by end users. My key focus is on selection criteria. I am very happy with the Access report writer capabilities. As far as...
6
by: Wescotte | last post by:
I'm having an issue where what should be global variables are not in scope and I'm confused as to why. In Case 1 the variables are not in scope for functions in File2 In Case 2 the varibales are...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
37
by: Neo | last post by:
Hi, I want to write a program that essentially connects to web, access some web page and pass some parameters and fetch the return values and print like LWP in Perl Context. Can some one...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
5
by: johnnash | last post by:
Ok I have a doubt regarding .h files. I basically have two modules in my software program - a.c and b.c There is .h file called d.h. d.h contains prototypes of functions in a.c so whenever i...
29
by: Guillaume Dargaud | last post by:
Hello all, anybody knows if there's some ANSI-C conformant code around that can read Windows-style .ini files ? I don't care about writing to it but I need to be able to read it from various OSs,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.