Accessing files from various functions

Discussion in 'C Programming' started by Lorenzo Mugnai, Aug 12, 2003.

  1. 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);
    };
     
    Lorenzo Mugnai, Aug 12, 2003
    #1
    1. Advertising

  2. Lorenzo Mugnai

    Derk Gwen Guest

    # 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?
     
    Derk Gwen, Aug 12, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oliver Wong
    Replies:
    12
    Views:
    1,461
    Roedy Green
    Dec 10, 2005
  2. SDZ

    Difference between various functions

    SDZ, Jun 14, 2004, in forum: C Programming
    Replies:
    18
    Views:
    5,266
    Joona I Palaste
    Jun 16, 2004
  3. Klaus Neuner
    Replies:
    14
    Views:
    456
    Paul McGuire
    Dec 15, 2004
  4. QQ

    Functions that accept various type?

    QQ, Dec 11, 2004, in forum: C Programming
    Replies:
    8
    Views:
    350
    Old Wolf
    Dec 14, 2004
  5. Michael Angelo Ravera

    Direct support for various Statistical functions

    Michael Angelo Ravera, Dec 17, 2009, in forum: C Programming
    Replies:
    1
    Views:
    989
    Dann Corbit
    Dec 18, 2009
Loading...

Share This Page