-
December 26th, 2011, 03:42 PM
#1
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);
}
}
-
December 26th, 2011, 03:48 PM
#2
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.
-
December 26th, 2011, 03:57 PM
#3
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
-
December 26th, 2011, 04:29 PM
#4
Re: File Binary I\O
Look at the return value of fread ...
-
December 26th, 2011, 05:51 PM
#5
Re: File Binary I\O
Sorry but to me the information is not enough to lead me in the right direction
Originally Posted by Philip Nicoletti
Look at the return value of fread ...
-
December 26th, 2011, 06:03 PM
#6
Re: File Binary I\O
Originally Posted by gaar321
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.
-
December 26th, 2011, 06:06 PM
#7
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?
-
December 26th, 2011, 06:07 PM
#8
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?
-
December 26th, 2011, 06:11 PM
#9
Re: File Binary I\O
Originally Posted by gaar321
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
-
December 26th, 2011, 06:13 PM
#10
Re: File Binary I\O
ahhh, sizeof(char)!! if not then no
-
December 26th, 2011, 06:14 PM
#11
Re: File Binary I\O
Originally Posted by gaar321
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
-
December 26th, 2011, 06:17 PM
#12
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
-
December 26th, 2011, 06:18 PM
#13
Re: File Binary I\O
Forget that last message paul thanks alot
-
December 27th, 2011, 09:32 AM
#14
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.
-
March 18th, 2019, 03:35 PM
#15
Re: File Binary I\O
Originally Posted by gaar321
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|