|
-
July 31st, 2005, 05:30 AM
#1
loading large file into buffer
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: ut);
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.
-
July 31st, 2005, 08:36 AM
#2
Re: loading large file into buffer
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.
Code:
#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;
}
-
July 31st, 2005, 01:56 PM
#3
Re: loading large file into buffer
Be aware, though, that there is a good chance that the product of 2 char values won't fit into a single char variable...
Old Unix programmers never die, they just mv to /dev/null
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|