Reputation: 173
Please tell me where I am going wrong.
I have a file which I need to copy last n bytes to an array.
char *buffer = (char *)malloc(sizeof(char)*n);
size_t result = fread(buffer,sizeof(char)*n,1,outptr);
The value of result is 0.
Everything up till here in my code works right( I have checked all the values with gdb). I am freeing the buffer too after some lines.
n is userinputted. The output ptr is used as
FILE *outptr = fopen(outfile,"w")
//The outfile name is also user inputted and is checked to make sure it is a bmp file.
Upvotes: 0
Views: 453
Reputation: 121961
You are attempting to read from a file you have opened in write mode.
Change:
FILE *outptr = fopen(outfile,"w");
to:
FILE *outptr = fopen(outfile,"r"); /* Use "rb", not "r", if 'outfile' is binary. */
Upvotes: 1
Reputation: 16597
size_t result = fread(buffer, n, 1, outptr);
should fine.
OTOH, is the file that you're working on is empty?
Please make sure that fopen()
was successful and also check whether malloc()
was successful!
On a completely different note, sizeof()
and fread()
returns size_t
and that should be cast to int
.
Upvotes: 1