Using Temporary Files

This is a discussion on Using Temporary Files within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to use temporary files in my C program but I am getting a Debug Assertion Failed ...

  1. #1
    Unregistered
    Guest

    Unhappy Using Temporary Files

    Hello,

    I am trying to use temporary files in my C program but I am getting a Debug Assertion Failed error.
    It happens when the program terminates. I am using ANSI C functions.

    I am posting test code below. I have taken out all error checks for clarity.

    #include <stdlib.h>
    #include <stdio.h>

    int main()
    {
    FILE * tempFile;
    FILE * fp;
    char string[80] = {0};
    long length = 0;

    fp = fopen("test.txt", "r");

    fseek(fp, 0l, SEEK_END);
    length = ftell(fp); /*get length of file*/
    fseek(fp, 0l, SEEK_SET);

    tempFile = tmpfile();

    fread((void *)tempFile, sizeof(char), length, fp); /*copy file to tempfile, this is the problem*/

    printf("%s\n", tempFile); /*prints out whole file*/

    fseek(fp, 0l, SEEK_SET);

    fgets(string, 80, fp);
    printf("%s\n", string);

    fseek(tempFile, 0l, SEEK_SET); /*fseek returns a -1 here meaning it can't seek*/

    fgets(string, 80, tempFile);
    printf("%s\n", string);

    fclose(fp);
    fclose(tempFile);

    return 0;
    }

    I know why the error happens, it is becuase of the fread, probably tring to free memory that isn't there,
    but how else do I copy the whole file? Am I using tmpfile wrong? If so can you please direct me to the correct
    way to use tmpfile.

    The reason I want to do this is for file safety, I don not want the file to get corrupted if the OS crashes.
    Open file, copy to buffer, close file, use buffer to parse and format data instead of the file.
    Any help is very much appreciated.

    Thank you,
    UC.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Not sure if I know what you want but...
    Code:
    int ch;
    FILE *file, *tempfile;
    
    file = fopen("test.txt", "r"); 
    tempfile = fopen("temp.txt", "w");
    
    while((ch = fgetc(file)) != EOF) /* this loop does it all */
        fputc(ch, tempfile);
    
    fclose(file);
    fclose(tempfile);
    This will write all of test.txt to temp.txt.
    Also you should add error checking to the fopen's.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Unregistered
    Guest
    Thanks for your suggestion C_Coder, but that still is creating a file on the drive.
    I would like to use temp files that are in memory and still use them as files with fgets() and other file functions.
    The tmpfile function creates a file in memory but I can't seem to access it as a file.
    The fread function copies the whole file to tempFile, I can print what tempFile is pointing to but I can't use file functions on it.

    Is your way the way they do it in released programs such as IE, etc.
    Or is there a other ways to do it.

    Regards,
    UC.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > fread((void *)tempFile, sizeof(char), length, fp); /*copy file to tempfile, this is the problem
    You're dealing with two FILE*s

    you fread from fp into a buffer
    you fwrite to tempFile from that buffer

    > but that still is creating a file on the drive
    Well if you wanted safety in case of OS crash - a good job it does

  5. #5
    Unregistered
    Guest
    >you fread from fp into a buffer
    >you fwrite to tempFile from that buffer

    I see, that makes sense.

    >> but that still is creating a file on the drive
    >Well if you wanted safety in case of OS crash - a good job it does

    So do you suggest creating a duplicate of the file on the drive to work from,
    instead of working from the memory?

    Is it good for small files?
    Is it good for large files?
    Is it good for graphic files?

    UC.

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    So do you suggest creating a duplicate of the file on the drive to work from,
    instead of working from the memory?
    If you have a duplicate in memory and the OS crashes you will lose that anyway.
    And you can delete one of the files with the remove function
    Code:
    int remove(char *filename);
    So you won't end up with loads of temporary files clutterring up your hard drive
    Last edited by C_Coder; 02-23-2002 at 03:46 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    There's no point creating a temp version, if all you're doing is reading from it.

    For writing (safely), you do
    fin = fopen("in","r");
    fout=fopen("out","w");

    When you're all done, you remove "in", and rename "out" to "in"

  8. #8
    Unregistered
    Guest

    Smile

    Thank you for your help guys. Its working know.

    fread((void *)wholeFile, sizeof(char), length, fp);
    fwrite((void *)wholeFile, sizeof(char), length, tempFile);

    I just wanted to read from the file and keep it memory just
    incase the user would like to reload the coordinates that it contains.

    Thanks again,
    UC.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Last one for a while ;0 - Index.dat files & internet cache
    By Robert_Sitter in forum C# Programming
    Replies: 1
    Last Post: 12-31-2005, 08:16 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21