Hallo
Will work, but do you really want sizeof(buffer) or COPY_SLICE as
your intermediate buffer? In the program below, it malloc maybe 4
bytes and copy only 4 bytes. My suggestion is as follows:
...
buffer = malloc(COPY_SLICE);
...
while ((bytes = fread(buffer,1,COPY_SLICE,src)) > 0) {
fwrite(buffer,1,bytes,dst);
}
...
This will be faster if you copy COPY_SLICE bytes than only 4!
Jos
--- In [email protected], "kaffbeemer" <[EMAIL PROTECTED]>
wrote:
>
> Hi Roberto,
>
> no I want to move a file from memory to an usb memory stick. In
the
> meantime I solved the problem:
>
> /* move file from ... to ... */
> #include <stdio.h>
> #include <unistd.h>
> #include <errno.h>
> #include <syslog.h>
> #include <malloc.h>
>
> #define COPY_SLICE (100*1024)
>
> int movefile (char *srcfile, char *dstfile)
> {
> FILE *src, *dst;
> char *buffer;
>
> src = fopen (srcfile, "rb");
> if (src == 0) {
> syslog (LOG_ERR, "movefile: fopen(%s) failed %i",
> srcfile, errno);
> return errno;
> }
> dst = fopen (dstfile, "wb");
> if (dst == 0) {
> syslog (LOG_ERR, "movefile: fopen(%s) failed %i",
> dstfile, errno);
> fclose (src);
> return errno;
> }
> buffer = malloc (sizeof(buffer));
> if (buffer == NULL) {
> syslog (LOG_ERR, "movefile: malloc %u byte failed",
> sizeof(buffer));
> fclose (src);
> fclose (dst);
> return -2;
> }
> while (!feof (src)) {
> size_t readsize = fread(buffer, sizeof(buffer), 1,
> src);
> if (ferror(src)) {
> syslog (LOG_ERR, "movefile: fread failed %
> i", errno);
> fclose (src);
> fclose (dst);
> free(buffer);
> return -3;
> }
> else if (!feof(src)) {
> readsize = sizeof (buffer);
> }
> fwrite(buffer, readsize, 1, dst);
> if (ferror (src)) {
> syslog (LOG_ERR, "movefile: fwrite failed %
> i", errno);
> fclose (src);
> fclose (dst);
> free(buffer);
> return -4;
> }
> }
> fclose (src);
> fclose (dst);
> free(buffer);
> return unlink (srcfile);
>
> }
>