| | |
map file in memory
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 18
Reputation:
Solved Threads: 0
hi there,
i try to map a file in memory, in linux, using mmap, so that what i write in memory will be written in the file as well.
the piece of code looks like this
i don't realy know how this works, but i think msync should update the file with the contents from memory.
what is wrong with the code?
i try to map a file in memory, in linux, using mmap, so that what i write in memory will be written in the file as well.
the piece of code looks like this
c Syntax (Toggle Plain Text)
fd = open(argv[1], O_RDWR); if (fd == -1) { error_message(__FILE__, __LINE__, "'open' failed "); return 1; } // find out the size of the file in bytes // we need the size, because we cannot write past the end of the file // so, we need to make sure not to write at an offset larger than 'info.st_size' if (fstat(fd, &info) == -1) { error_message(__FILE__, __LINE__, "'fstat' failed "); return 1; } printf("The file has %ld bytes\n", info.st_size); if (info.st_size == 0) { fprintf(stderr, "We cannot map a file with size 0\n"); return 0; } // TODO mapping = mmap(NULL, info.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0); if (mapping == MAP_FAILED) { perror("mmap:"); return -1; } else fprintf(stderr, "Successful mapping at the address %p\n", mapping); memcpy(mapping, "hello", 5); if (msync(mapping, info.st_size, MS_SYNC) == -1) { perror("msync:"); return -1; } // close the file if (close(fd) == -1) { error_message(__FILE__, __LINE__, "'close' failed "); return 1; }
i don't realy know how this works, but i think msync should update the file with the contents from memory.
what is wrong with the code?
•
•
Join Date: Nov 2007
Posts: 18
Reputation:
Solved Threads: 0
nevermind... i just found that the program was actually working...
but the changes i made were not visible unless i closed the file, and opened again. i don't know why this happens. if i change the file in another editor, and save it, i'm announced that the contents were changed outside the program, but if i modify the file using mmap it doesn't say anything
but the changes i made were not visible unless i closed the file, and opened again. i don't know why this happens. if i change the file in another editor, and save it, i'm announced that the contents were changed outside the program, but if i modify the file using mmap it doesn't say anything ![]() |
Similar Threads
- access Windows Address Book (C)
- how do i parse a custom delimited file (C++)
- GAAAH! Memory issue? (C++)
- Anyone can help?? (C++)
- Protected Mode Memory Map (C)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
Other Threads in the C Forum
- Previous Thread: Need help with errprs in my code i cant find them
- Next Thread: ATOI Problems
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi





