-2

I'm writing a code where I have to generate a key.bin file containing 10000 keys(1 key = 16 bytes random no.), read the Key file, and store it in the RAM. Can anyone tell me how I got about it? I'm relatively new to this, so any guidance would be great.

Thanks.

seeker1234
  • 21
  • 6
  • 1
    https://idownvotedbecau.se/noattempt/ what have you done to search for this so far? It is a common task with plenty of tutorials avialable (e.g. https://www.cplusplus.com/doc/tutorial/files/ ) – SamBob Oct 26 '21 at 09:51
  • 1
    Does this answer your question? [How do you open a file in C++?](https://stackoverflow.com/questions/7880/how-do-you-open-a-file-in-c) – SamBob Oct 26 '21 at 09:53
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Oct 26 '21 at 09:56
  • 1
    It is not clear what do you mean by _store it in RAM_. Normally, RAM is not organized into files (an external memory, like disk, contains file system, but not RAM). Is this a regular PC or is it some special kind of device? Are you talking about regular RAM or some kind of NVRAM perhaps? – heap underrun Oct 26 '21 at 09:59

1 Answers1

2

As @heap-underrun has pointed out, it is unclear if you're talking about general RAM used in PCs or some special type of memory in some other device. I will just assume that you're talking about regular RAM in PC.

When you run a program, all the variables in the program are stored in the RAM until the program terminates. So, all you need to do is to read the file in your program and store the data in a variable. Then the data will be stored in RAM till your program terminates. https://www.cplusplus.com/doc/tutorial/files/ should be a good reference to file I/O in C++.

I will give a simple example here. Assuming your file looks like this

00001
00002
00003
...

A simple C++ code to read the file is,

#include<fstream>

using namespace std;

int main(){
    int n = 10000;          //number of keys in file
    long double keys[n];    //because you said 16 bit number
                            //and long double is 16 bit
    ifstream FILE;
    FILE.open("key.bin");   //include full path if the file is in
                            //a different directory
    for(int i = 0; i < n; i++){
        FILE >> keys[i];
    }
    FILE.close();

}

Now, all the keys are in keys array. You can access the data and do whatever you want. If you are still unclear about something, put it in the comments.

Abdur Rakib
  • 336
  • 1
  • 12