Store a text file in memory - C++

I need to store a text file in memory as a backup and later delete it from the memory or write it to the disk as required. Which is the best way to temporarily store it in memory?
sukhoi35Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
You can either store each line as a separate string using a vector of strings or store the whole file as one vector of chars. You can also use a stringstream to create an in-memory stream.http://www.cplusplus.com/reference/stl/vector/http://www.cplusplus.com/reference/iostream/stringstream/
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

int main()
{
   std::vector<std::string> vector_of_strings;
   std::vector<char> vector_of_chars;
   std::stringstream memory_stream;

   std::fstream in("myfile.txt");

   std::string line;

   while(getline(in, line))
   {
      // save each line as a collection of lines
      vector_of_strings.push_back(line);

      // append each line to a buffer of chars
      std::copy(line.begin(), line.end(), std::back_inserter(vector_of_chars));

      // append each line to the in-memory stream
      memory_stream << line;
   }
}

Open in new window

0
 
vinaypatkiCommented:
Create a buffer in memory alloted to your application. Use that buffer as a storage of text/data that you will be writing to actual file. Use sprintf/memcopy for IO on buffer similar to writefile for files.
0
 
guveraCommented:
Create a txt file with the information in correct columns, as if you don't the information won't be right. This code should help.

ifstream inputFile;
ofstream outputFile;
inputFile.open("payInput.txt");
outputFile.open("outputFile.txt");

if(inputFile.fail())
cout << "Error opening inputfile!\n";
else if(outputFile.fail())
cout << "Error opening outputfile!\n";
else
{
outputColHeadings(outputFile);
readFileValues(inputFile, empIDArray, rateArray, hoursArray, dependArray);
then do calculations....
------FUNCTIONS--------------
void outputColHeadings(ofstream& outputFile)
{
outputFile <<" EmpId" << setw(10) <<"Rate"<< setw(9) <<"Hours" << setw(6)
<<"Dep" << setw(12) <<"Gross Pay" << setw(11) << "Fed. Tax"
<< setw(12) <<"State Tax" << setw(12) <<"Net Pay\n";
for (int i = 1; i <= 78; i++)
outputFile << "=";
outputFile << "\n";

return;
}//outputColHeadings
void readFileValues(ifstream& inputFile, long empID1Array[],
double rate1Array[], double hours1Array[],
int depend1Array[])
{
int i;

for(i=0; i < SIZE; i++)
inputFile >> empID1Array[i] >> rate1Array[i] >> hours1Array[i]
>> depend1Array[i];
return;
}//readFileValues

Hope this one help!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
guveraCommented:
Hi

 Did u check the code?  Is anything wrong to execute the code?
0
 
sukhoi35Author Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.