Find the answer to your Linux question:
Results 1 to 8 of 8
Hi, When we are using RAM DISK - the files are stored on the RAM. from what I understand (and saw many examples) in order to read data from file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    7

    How to get pointer (example: char*) to Ram Disk Files ?


    Hi,

    When we are using RAM DISK - the files are stored on the RAM.
    from what I understand (and saw many examples) in order to read data from file (the file which locate on the RAM) - I need to use the read function.

    Is there a chance to get char* (or any pointer) to the content of the file without using the read function ?

    If the file locate on the RAM, it seem that it is like I have a buffer on the RAM (like an array which was dynamic allocated) and in the case of a buffer on the ram -> we can use pointers to the data without reading all the data.

    example:

    class CDATA
    {
    int nValue1;
    int nValue2;
    double dValue3;
    double dValue4;
    char achBuf[10];
    };

    void main ()
    {
    // for the example: suppose we have file which contains CDATA structures.

    fstream m_ramdiskFile;
    m_pBuffer = new char[1000000];
    m_ramdiskFile.open("./.ramdisk", ios::binary | ios::in | ios::out | ios::trunc);

    // in stead of reading the whole record
    CDATA cDATA;
    m_ramdiskFile.read((char*)cDATA, sizeof(CDATA));

    // can I get a pointer to the content of the file -> which already locate on the RAM (bacause
    // it is file on the RAM DISK)
    // can I do something like that:
    CDATA* pData = m_ramdiskFile.beg(); ???
    cout << pData->dValue4;

    }

  2. #2
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,211
    When you open a file for reading the file position indicator is set to the beginning of the file. You can then use mmap and map the file to a char*. You pass the open fd and the length of the map to the function mmap. You then use a char* as though the file was loaded into memory. You can see man mmap for details.

    Note - error checking omitted:
    Code:
    int fd = open (myfile, O_RDWR);
    struct stat st;
    fstat (fd, &st);
    char* thePtr = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);

  3. #3
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    932

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    7
    Hi,
    when using mmap function and loading the file into the memory -> you mean that in the background there is a read operation of all the data from the file ?
    (for example, if the file size is 10MB and using mmap -> we are reading 10MB to the memory ?)

  5. #5
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,211
    A view of the file or any portion of the file is created in virtual memory. Have a look at this for a pretty thorough explanation: File Mapping - ????, ????! - ???? - CSDN.NET

    For small files you're better loading the file into memory but for large files memory mapping can be very effective.

  6. #6
    Just Joined!
    Join Date
    Mar 2014
    Posts
    18
    Code not working

  7. #7
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,211
    Which code would that be?

  8. #8
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    367
    mmap can be *very* dangerous. Any changes made are reflected *immediately* on disk without any save operation needed. Perhaps is not relevent with ramdisk, but still...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •