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;

}

Didnt try this, however, if it works then its great,

do

if(data != NULL)
{
    x = getchar();
    while( x != NULL )
    {
        x = toupper(x);
        putchar(x);
        x = getchar();
    }
}
See comments in code
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <ctype.h>
//----------------------------------------------------------
// Added for fstat
#include <sys/stat.h>


int main(int argc, char * argv[]){

        //----------------------------------------------------------------------------
        // Added for fstat
        struct stat sb;
        int fd, pagesize,i;
        char *data, x;

        if (argc != 2){
          //--------------------------------------------------------------------------
          // Here you don't want to use perror because will reference 
          // the value of errno which is not set to an error code.  At 
          // this point the error code id Sucess
          // perror("wrong number of arguments") prints out:
          //
          // wrong number of arguments:  Success
          // Which doesn't make sense

          //--------------------------------------------------------------------------
          // Just use printf
          printf("Usage:  %s <filename>\n",argv[0]);

          return 1;
       }
       //------------------------------------------------------------------------------ 
       // You had this as read only -> O_RDONLY,  You want to save the modified buffer
       // so it has to be read and write
       fd = open(argv[1], O_RDWR);
       if(fd == -1){
           //--------------------------------------------------------------------------
           // You don't need to close the file, it never opened
           //close(fd);
           perror("Could not open file");
           return 1;
       }
       //------------------------------------------------------------------------------ 
       // Not sure why you are getting the pagesize here and then using
       // it as an offset in the mmap function call
       // pagesize = getpagesize();
       // data = mmap(NULL,512, PROT_WRITE | PROT_READ, MAP_SHARED, fd, pagesize);

       //------------------------------------------------------------------------------ 
       // 1. If you want to change all of the characters to upper case you want the
       //    offset to be zero, otherwise you would not convert the first 0 to offset
       //    characters to upper case
       // 2. You should get the size of the file using fstat and not a hard coded 512.
       //    man fstat
       //    int fstat(int filedes, struct stat *buf)
       //
       data = mmap(NULL,512, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
       if ( data == MAP_FAILED ) {
           perror("mmap failed");
           return 1;
       }
       close(fd);

       /*Change all words to uppercase. This part is incorrect.
       while(data != NULL){
                x = getchar();
                x = toupper(x);
                putchar(x);
       }*/

       //------------------------------------------------------------------------------ 
       // Now your data is in memory, so use the data pointer as if it is a pointer to
       // an array of characters.  Operate on thoses values.  Like:
       // data[0] = toupper(data[0]);
       // Loop thru all value in the file.

       // -----> You insert code <------

       //------------------------------------------------------------------------------ 
       // Again fix the hardcoded 512
       munmap(data, 512);
       return 0;
}

Thanks for all the help! It was much appreciated. =]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.