-1

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.

4
  • 1
    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. cplusplus.com/doc/tutorial/files ) Commented Oct 26, 2021 at 9:51
  • 1
    Does this answer your question? How do you open a file in C++? Commented Oct 26, 2021 at 9:53
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Oct 26, 2021 at 9: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? Commented Oct 26, 2021 at 9:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.