Hello,
I was curious to how I would go about modifying an existing text file (to all upper case) by the use of mapped memory.
I think I have done the mapping correct, however I am stuck on how to modify the file. Help would be much appreciated, thanks!
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <ctype.h>
int main(int argc, char * argv[]){
int fd, pagesize;
char *data, x;
if (argc != 2){
perror("wrong number of arguments\n");
return 1;
}
fd = open(argv[1], O_RDONLY);
if(fd == -1){
close(fd);
perror("Could not open file");
return 1;
}
pagesize = getpagesize();
data = mmap(NULL,512, PROT_WRITE | PROT_READ, MAP_SHARED, fd, pagesize);
close(fd);
/*Change all words to uppercase. This part is incorrect.*/
while(data != NULL){
x = getchar();
x = toupper(x);
putchar(x);
}
munmap(data, 512);
return 0;
}