ccc
ccc

Reputation: 93

read from binary file and copy into array

its time over but anyway i want finish this problem. I want read binary file to buffer and later i want copy this buffer to array. I'm doing like that;

int i=0;
char *buffer;

buffer=(char *)malloc(filelen+1);  //filelen is length of binary file

while()
{
fread(buffer,100,1,filepointer);   //filepointer is input binary file pointer,i wanna read 100 byte
strcpy(tup[i],buffer); //tup[] is char array.i want copy buffer to in this array 
i++;
}

i got error at strcpy line you can not copy pointer to integer something like that.

thanx.

Upvotes: 0

Views: 4322

Answers (2)

Johnny Mnemonic
Johnny Mnemonic

Reputation: 3912

It must be:

strcpy(tup,buffer);

if tup is char* tup.

Also you can use buffer[filelen]=0; after you've used malloc() to allocate the memory for buffer, this will take care of the '\0' termination.

Upvotes: 1

doron
doron

Reputation: 28892

I think you want to write:

strcpy(&tup[i],buffer);

There are however a number of other issues.

  1. How do you know that tup and buffer are null terminated. buffer is unlikely to be null terminate. You therefore should memcpy instead (with a known calculated length)
  2. Are you sure that it is impossible to overwrite then end of tup?
  3. since your temporary buffer is only reading 100 bytes at a time you do not need to allocate the full filesize of memory.

Upvotes: 3

Related Questions