Solved

Store a text file in memory - C++

Posted on 2010-11-08
6
685 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:sukhoi35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Expert Comment

by:vinaypatki
ID: 34090374
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
 
LVL 8

Expert Comment

by:guvera
ID: 34090392
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
 
LVL 4

Assisted Solution

by:vinaypatki
vinaypatki earned 200 total points
ID: 34090407
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Accepted Solution

by:
evilrix earned 300 total points
ID: 34090849
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
 
LVL 8

Expert Comment

by:guvera
ID: 34090921
Hi

 Did u check the code?  Is anything wrong to execute the code?
0
 

Author Closing Comment

by:sukhoi35
ID: 34096207
Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question