简体   繁体   中英

How can I write array of integers properly to file by using mmap in c++

I am currently trying to write the integers from an array to a .txt file by using mmap. However, I am facing with an unexpected problem that I was not able to solve it. First of all, here is the code where I'm trying to write the array of integers to a file.

bool writeFileFromArrayByMemoryMap( int *&arrayToWriteInto, int size, char *output_file_name){ 
    int sizeForOutputFile = size * sizeof(int);
    int openedFile = open(output_file_name, O_RDWR | O_CREAT); //openning the file with the read&write permission
    lseek (openedFile, sizeForOutputFile-1, SEEK_SET);
    write (openedFile, "", 1);

    int *memoryBuffer = (int *)mmap(NULL, sizeForOutputFile, PROT_READ | PROT_WRITE, MAP_SHARED, openedFile, 0); //creating a memory mapping

    int currentIndex = 0; //the current index to put currentIntegerToPutArray to the array
    int *currentByte = memoryBuffer;
    while(currentIndex < size) {

        sprintf((char *)currentByte, "%d\n", arrayToWriteInto[currentIndex]);
        currentByte++;
        currentIndex++;
    }
    close(openedFile); //closing the file
    munmap(memoryBuffer, sizeForOutputFile); //remove the maping*/
return true;}

Array and the path of the file is passed by the caller, and the size is 100 for now. Actually the file size that I want to write on is much bigger than 100*sizeof(int) but to test the writing I just made it smaller. Nevertheless, I am not able to write the integers properly. The output file writes some results properly but after a moment it does not get into a new line and then it writes the all the integers without separating them with a new line. Where could be the reason doing that ? As far as I see I set the size of the file properly but it seems the problem is likely about using the bytes of the file incorrectly.

Edit: I also found out that if the program tries to write a value which is bigger than 999, then it freaks out. If the array is full of the values smaller than 1000, there is no problem to write it properly. Why it does not write properly the values greater than 999 ?

After reading through this more, I think one core issue is the %d\\n in the sprintf.

%d writes a variable number of bytes. For example, 1\\n produces 2 bytes. 315\\n produces 4. 1024\\n produces 5. Your loop increment (currentByte++) assumes four bytes are written each and every time. That's not the case.

You might want something like this.

char *pc = (char*)memoryBuffer
for(int i=0;i<size;++i) {
    pc+=sprintf(pc, "%d\n", arrayToWriteInto[i]);
}

However, the name of your variable, arrayToWriteInto is very misleading. The code appears to only read from it. Is arrayToWriteInto a source or a destination?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM