Like Tree1Likes
  • 1 Post By Bayint Naung

Help for file processing

This is a discussion on Help for file processing within the C Programming forums, part of the General Programming Boards category; The problem is :there are two files, a.bin of 1024B in size and b.bin of 512B in size,I need copy ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Question Help for file processing

    The problem is :there are two files, a.bin of 1024B in size and b.bin of 512B in size,I need copy the b.bin to the top 512B of a.bin and don't change the back 512B.
    it seems easy,but i failed,even tried several times.
    I open file like this:
    Code:
     FILE *fa=fopen("a.bin",wb);
     FILE *fb=fopen("b.bin","rb");
    the files can be opened successfully,but i finded the a.bin resized to 512B when i read binary from b.bin and writed to a.bin.
    so i used another way to open a.bin :FILE *fa=fopen("a.bin",ab);
    but this time the a.bin changed to 1.5K in size.

    what should I do to solve the problems?
    thank you

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    w - truncate the file and start over (start with the file pointer at the beginning)
    a - append on the end of the file (start with the file pointer at the end)

    You can use a, but you need to rewind so you can start at the beginning. Alternately, you could use something like r+ which would open for reading and writing, and start at the beginning of the file.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by quzah View Post
    w - truncate the file and start over (start with the file pointer at the beginning)
    a - append on the end of the file (start with the file pointer at the end)

    You can use a, but you need to rewind so you can start at the beginning. Alternately, you could use something like r+ which would open for reading and writing, and start at the beginning of the file.


    Quzah.
    Thanks for your reply.
    I have used rewind() function before,but it failed still.The b.bin is appended to the tail of a.bin.
    Code:
    FILE* fa,*fb;
    	if((fb=fopen("b.bin","rb")) == NULL )
          return false;
    	if((fa=fopen("a.bin","ab"))== NULL )
          return false;
    	unsigned char buff[512];
    	memset(buff,0,512);
    	fread(buff,512,1,fb);
    	rewind(fa);
    	fwrite(buff,512,1,fa);
    	fclose(fa);
    	fclose(fb);
    according to your tip,I open a.bin using "r+b",and it works this time.
    So thank u again.But i am still confused why the rewind function dose not work.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    ``r+'' Open for reading and writing. The stream is positioned at the
    beginning of the file.

    ``a'' Open for writing. The file is created if it does not exist. The
    stream is positioned at the end of the file. Subsequent writes
    to the file will always end up at the then current end of file,
    irrespective of any intervening fseek(3) or similar
    man page fopen section 3
    quzah likes this.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Most likely you need to open your A file in binary update mode...
    Code:
    FILE * fa = fopen("Filea.bin","rb+");
    FILE * fb = fopen("Fileb.bin","r");
    then just load the entire file B into a buffer and write it over top of file A.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bayint Naung View Post
    Subsequent writes
    to the file will always end up at the then current end of file,
    irrespective of any intervening fseek(3) or similar

    man page fopen section 3
    Ah. I forgot about that. Good catch. That's why rewind doesn't do anything useful on just "a".


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reentrant Message Processing (WndProc)
    By phantomotap in forum Windows Programming
    Replies: 7
    Last Post: 04-28-2009, 10:44 AM
  2. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 11:33 AM
  3. Sub Array Processing
    By GCNDoug in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2007, 03:41 PM
  4. Using a lot of processing time!
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2003, 03:44 AM
  5. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM

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