-1

I have file1.txt

This is my file

and file2.txt

This is my second file

and I want copy file2.txt content to file1.txt using memcpy

int main( int argc, char * argv[] ){
    int d;
    int d2;
    int p;
    FILE *f1;
    FILE *f2;
    if(argc == 3){
        f1 = fopen(argv[1], "r+");
        f2 = fopen(argv[2], "r+");
            d = da_open(argv[1]);
            d2 = da_open2(argv[2]);
            p = da_cp(f2, f1, 10); 
            da_map(d, 10);
            da_map(d2, 10);

            close(p);
            //closef(d2);
    }

} I don't know why but when I run this I get file2.txt with lots of random symbols. Why? what I'm doing wrong?

EDIT: my full code

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <string.h>

int da_open(const char *name);
int da_open2(const char *name);
void *da_map(int d, int size);
int da_cp(void *str1, const void *str2, size_t n);

int da_open(const char *name){
   int dskr;
   dskr = open( name, O_RDONLY, 0640 );
   if( dskr == -1 ){
      perror( name );
      exit( 255 );
   }
   printf( "dskr = %d\n", dskr );
   return dskr;
}

int da_open2(const char *name){
   int dskr;
   dskr = open( name, O_RDWR, 0640 );
   if( dskr == -1 ){
      perror( name );
      exit( 255 );
   }
   printf( "dskr = %d\n", dskr );
   return dskr;
}

void *da_map(int d, int size){
   void *a = NULL;
   a = mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, d, 0 );
   if( a == MAP_FAILED ){
      perror( "mmap failed" );
      abort();
   }
   return a;
}

int da_cp(void *str1, const void *str2, size_t n){
         memcpy(str1, str2, n);
}

int main( int argc, char * argv[] ){
        int d;
        int d2;
        int p;
        FILE *f1;
        FILE *f2;
        if(argc == 3){
            f1 = fopen(argv[1], "r+");
            f2 = fopen(argv[2], "r+");
                d = da_open(argv[1]);
                d2 = da_open2(argv[2]);
                 p = da_cp(f2, f1, 10);
                da_map(d, 10);
                da_map(d2, 10);

                close(p);
                //closef(d2);
        }
}
David
  • 3,055
  • 4
  • 30
  • 73

2 Answers2

2

memcpy doesn't work over a file descriptor, you can store the content of file1 in an array of chars (using fgets or fread) and then copy to file2 (using fputs or fwrite)

Also note that you need to use fclose() instead of close() with fopen()

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
2

d and d2 are not files, they're just file descriptors (something that stores the information about the file and its input/output states).

If you want to copy from 1 file to another, you have to read the first file first. That's the example of how you can do this:

#include <stdio.h>
#include <stdlib.h>

// Open the first text file
FILE *f = fopen("textfile1.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);

// Read it into buffer
char *buffer = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);

buffer[fsize] = 0;

// Now write the string buffer into your second text file
FILE *f2 = fopen ("textfile2", "wb");
fwrite (buffer, sizeof(char), sizeof(buffer), f2);
fclose (f2);

So, as you see, the memcpy can only perform with memory in RAM and it has nothing to do with files in hard drive (unless you read it into your RAM memory)

Chan Kha Vu
  • 9,834
  • 6
  • 32
  • 64