Rewriting an opened file.

This is a discussion on Rewriting an opened file. within the C Programming forums, part of the General Programming Boards category; I'm trying to append a CRC to a file. However, there is one problem (as always ). The file is ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question Rewriting an opened file.

    I'm trying to append a CRC to a file. However, there is one problem (as always ). The file is already open so I can't write to it.

    Let me explain what i'm trying:
    I'm opening the same file as executed and read it's contents into a buffer. Then I calculate the CRC over this buffer and append it to the end. The next thing I want to do is rewrite the file with the CRC information at the end. (Yes it will still execute, i've tested it by writing the buffer to a new file with another name)

    Is there a way to make the OS (w2k in this case) think the file isn't opened to be able to rewrite it?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    What do you mean with rewrite the file? When opening a file in "r+" mode, you have access to the file for both reading and writing.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    That's correct, but I want to open the same file as executed and that I can't do with "r+" or "r+b".
    Code:
    if ((pIn = fopen(argv[0], "r+b")) != NULL)
    {
       - read content into buffer
       - calculate and add CRC to buffer
       - write buffer back to argv[0] 
    }
    Well, maybe it's just not possible. I was assuming that the code would be executed in RAM and it would be possible for me to make changes to the file.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Ah, I see, an executable. I don't think the OS willl allow you to open such a file while it is running. At least not for updating.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    you could try

    Code:
    if ((pIn = fopen(argv[0], "rb")) != NULL)
    {
       - read content into buffer
         fclose(pIn);
       - calculate and add CRC to buffer
        if ((pIn = fopen(argv[0], "wb")) != NULL) 
       - write buffer back to argv[0] 
    }
    
    /*or*/
    if ((pIn = fopen(argv[0], "r+b")) != NULL)
    {
       - read content into buffer
         rewind(pIn);
       - calculate and add CRC to buffer
       - write buffer back to argv[0] 
    }

  6. #6
    Unregistered
    Guest
    hehe, the fun things you will find in the MSDN. I had a similar problem a few days ago, so I gave it a quick search. Here's what I found:

    freopen()

    Yep, handy little thing, file reopen.

    FILE* freopen(path, mode, FILE*)

    nnfpw = freopen("config/notnames.txt", "w", nnfp);

    nnfpw is the new file pointer, then you have the path, the open mode, and the old file pointer. Now, the old file pointer (say it was open in "r") will still be in read mode. So, you could read with nnfp and write with nnfpw (I think haven't tested that). Just keep in mind though, whenever opening anything in "W" mode, you will destroy whatever is currently there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

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