0

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.

ccc
  • 93
  • 1
  • 3
  • 10
  • `buffer` isn't a proper terminated string, so you can't use `strcpy`. You could try `memcpy` instead, after checking the return value from fread (to see how many elements were actually read). – Bo Persson Apr 03 '13 at 16:35

2 Answers2

3

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.
doron
  • 27,972
  • 12
  • 65
  • 103
1

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.

Johnny Mnemonic
  • 3,822
  • 5
  • 21
  • 33
  • There is *no* reason to `memset()` and entire buffer with zeros only to immediately overwrite all but the last index with file content. A simple `buffer[filelen] = 0;` will suffice. – WhozCraig Apr 03 '13 at 16:52