dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: File Binary I\O

  1. #1
    Join Date
    Apr 2010
    Posts
    172

    Post File Binary I\O

    The code works fine and writes the read in bytes to the file, however because the array list size is 100 bytes it will always output 100 bytes even if the actual file contents is 70 bytes. Is there a way i can stop these extra bytes being added (y)
    Code:
    {
                    Output = fopen("2.txt","wb+");
    		for(offset = 0;offset < FileLength;offset+=100)
    
    		{
    	         fread(input,100,sizeof(char),Input);
    	         fwrite(input,100,sizeof(char),Output);
    		}
    	
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: File Binary I\O

    Is there a way i can stop these extra bytes being added (y)
    Code:
    fwrite(input,100,sizeof(char),Output);
    You are writing 100 bytes. Simply write less bytes.

  3. #3
    Join Date
    Apr 2010
    Posts
    172

    Re: File Binary I\O

    is there a way were i can find out how many bytes are left to be read into the file so i can switch the buffer to write 1 byte at time. i dont want to write less bytes untill the end of the file is approaching because if i write 100 bytes at a time its lots quicker than writing 1 byte at a time

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    Re: File Binary I\O

    Look at the return value of fread ...

  5. #5
    Join Date
    Apr 2010
    Posts
    172

    Re: File Binary I\O

    Sorry but to me the information is not enough to lead me in the right direction
    Quote Originally Posted by Philip Nicoletti View Post
    Look at the return value of fread ...

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File Binary I\O

    Quote Originally Posted by gaar321 View Post
    Sorry but to me the information is not enough to lead me in the right direction
    Why not? The return value of fread() is the number of bytes read. You know exactly how many bytes were read, and then you adjust your char array (by placing a NULL terminator) at the position where the read stopped, or you just call fwrite with the number that was returned (instead of 100).

    http://www.cplusplus.com/reference/c.../cstdio/fread/

    Also, your code does no checking for any return values. Why? What if the file cannot be opened? Where is the check for those file pointers being NULL?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 26th, 2011 at 06:06 PM.

  7. #7
    Join Date
    Apr 2010
    Posts
    172

    Re: File Binary I\O

    when i set the return value it just returns 1, however could this be because the return variable is a int?

  8. #8
    Join Date
    Dec 2011
    Location
    Bucharest, Romania
    Posts
    29

    Re: File Binary I\O

    first think I would think of is to use writeprofile functions to be kind of organizd programmer. I`m not shure why you use random storage and not use sections w plain text.

    what is the function you read the data in a 70byte long file?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File Binary I\O

    Quote Originally Posted by gaar321 View Post
    when i set the return value it just returns 1,
    Read the documentation for fread() carefully at the link I posted. Look at the example program. Look at your second and third parameters.
    Code:
    fread(input,100,sizeof(char),Input);
    Do you see something wrong?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 2010
    Posts
    172

    Re: File Binary I\O

    ahhh, sizeof(char)!! if not then no

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: File Binary I\O

    Quote Originally Posted by gaar321 View Post
    ahhh, sizeof(char)!! if not then no
    Look at both the second and third parameters, and read the description as to what those parameters mean. Don't you think you have them mixed up?

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 2010
    Posts
    172

    Re: File Binary I\O

    Okay this then means what if the file is so big it cant all fit in the memory, thats what i was trying to avoid in the first place

  13. #13
    Join Date
    Apr 2010
    Posts
    172

    Re: File Binary I\O

    Forget that last message paul thanks alot

  14. #14
    Join Date
    Dec 2011
    Posts
    2

    Re: File Binary I\O

    Best thing to do if one has a binary to use io with is make a ring in binary in the file. and load it up in the same binary. there is a way to load a program instead of just a little binary. because when it loads through the ring it can access the binary easily. and already knows where the binary is.

  15. #15
    Join Date
    Dec 2011
    Location
    Bucharest, Romania
    Posts
    29

    Re: File Binary I\O

    Quote Originally Posted by gaar321 View Post
    The code works fine and writes the read in bytes to the file, however because the array list size is 100 bytes it will always output 100 bytes even if the actual file contents is 70 bytes. Is there a way i can stop these extra bytes being added (y)
    Code:
    #include "windows.h"
    
    WinMain(){
    	DWORD dwi;
    	DWORD offset;
    	char chr;
    	FILE* Output = fopen("2.txt","wb+");
    	FILE* Input = fopen("1.txt","rb+");
    
    	DWORD FileLength=GetFileSize(Intput, 0);
    
    	for(offset = 0;offset < FileLength;offset++){
    		fread(input,1,sizeof(char),Input);
    		output=input;
    		fwrite(output,1,sizeof(char),Output);
    	};
    
    	fclose(Input);
    	fclose(Output);
    }
    Sorry, I hope this will work. To avoid adding extrabytes you can try to read your input file sequencially (byte over byte).
    You may be needing to liniarize the input before writing it to Output because when it comes to binary reading,
    sizeof(char) may be 2.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)