[cs631apue] tcpm implementation

bzhang41 bzhang41 at stevens.edu
Sat Sep 17 23:22:31 EDT 2016


Dear Prof.

I have question about the tcpm implementation.

the mmap is like map the content of a file to a segment of memory, so we 
can access it more conveniently.
and memcpy is like cope the content from a segment of memory to another 
segment of memory.

But how can I use the `memcpy` to write to the target file ?

Based on my understanding, memory is temporary, however the content of 
the file is stored on the disk, how can I just use `memcpy` to write the 
content from the memory to the disk ?

So I tried:

//Try to open a new file, and then use memcpy
FILE * dest ;
if (( dest = fopen(pathname,"w+")) == NULL){
       fprintf(stderr,"tcp : can't create %s", fn);
       exit(EX_OSERR);
}

//get segmentation fault use malloc here.
/*if ( (dest = malloc(sbuf.st_size)) == NULL ){
       fprintf(stderr, "tcp: system error : %s\n",strerror(errno));
       exit(EX_OSERR);
}
*/
memcpy(dest,src,sbuf.st_size);


I can't write the content from memory to a file, since the file is 
empty.
My question may seem silly, but I want to figure it out.

Thanks
Bo


More information about the cs631apue mailing list