Click to See Complete Forum and Search --> : loading large file into buffer


abul
July 31st, 2005, 05:30 AM
ifstream file1 ("1.txt", ios::in|ios::binary|ios::ate);
ifstream file2 ("2.txt", ios::in|ios::binary|ios::ate);
b1size = file1.tellg();
b2size = file2.tellg();
file1.seekg (0, ios::beg);
file2.seekg (0, ios::beg);
buffer1 = new char [b1size];
buffer2 = new char [b2size];
file1.read (buffer1, b1size);
file2.read (buffer2, b2size);
file1.close();
file2.close();
//cout << "the complete file is in a buffer";
fo.open("out.txt",ios::out);
for (i=0;i<b1size;i++)
//fo <<buffer1[i]<<"\t"<<"*"<<"\t"<<buffer2[i]<<"\t"<<"="<<"\t"<< buffer1[i]*buffer2[i]<<"\t";
fo <<buffer1[i]*buffer2[i]<<"\t";
fo.close();
delete[] buffer1;
delete[] buffer2;

by the above code i am handling 2 large file(1.txt and 2.txt) is both of 109 mb.i m trying to element by elemnet multiplication of the both file.but when i am loading both file into buffer its not a wise way.so i need to do it by taking one elements from both file and manipulate(*) it and then delete the buffer to load to load anoter element to manipulate.

plz send me by changing my code.

SuperKoko
July 31st, 2005, 08:36 AM
Please use code tags.

Actually you are multiplying each byte of 1.txt with each byte of 2.txt, maybe you want to multiply 4 byte integers, instead of 1 byte integers.

This code use streaming.

#include <fstream>

using std::ios;
using std::char_traits;

int main()
{
std::ifstream file1("1.txt",ios::in|ios::binary);
std::ifstream file2("2.txt", ios::in|ios::binary);
std::ofstream ofile("out.txt",ios::out|ios::binary|ios::trunc);

char c1,c2;
while(file1.get(c1).good() && file2.get(c2).good())
{
ofile.put(c1*c2);
}
ofile.close();
file2.close();
file1.close();

return 0;
}

HighCommander4
July 31st, 2005, 01:56 PM
Be aware, though, that there is a good chance that the product of 2 char values won't fit into a single char variable...