I wish to implement a simple C++ filter that reads from a file byte by byte
and writes to an output files two bytes for each byte read according to some
hardcoded table. I am looking for a very straightforward sample. It is
pratically a binary filter.
Thank you in advance.
DdJ 6 3510
"Dario de Judicibus" <no****@nowhere.it> wrote... I wish to implement a simple C++ filter that reads from a file byte by
byte and writes to an output files two bytes for each byte read according to
some hardcoded table. I am looking for a very straightforward sample. It is pratically a binary filter.
What book on C++ do you have that doesn't describe file I/O?
Dario de Judicibus wrote: I wish to implement a simple C++ filter that reads from a file byte by byte and writes to an output files two bytes for each byte read according to some hardcoded table. I am looking for a very straightforward sample. It is pratically a binary filter.
Thank you in advance.
DdJ
How about this:
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;
class filter{
map<char, pair<char, char> > table;
public:
filter(){
// Add your own initialization here.
table['a'] = make_pair('b', 'c');
table['d'] = make_pair('e', 'f');
}
void operator()(char c){
static ofstream out("my_output.txt");
out << table[c].first << table[c].second;
}
};
int main()
{
ifstream input("my_input.txt");
istream_iterator<char> current(input), end;
for_each(current, end, filter());
}
"Dario de Judicibus" <no****@nowhere.it> wrote in message
news:lk********************@news3.tin.it... I wish to implement a simple C++ filter that reads from a file byte by
byte and writes to an output files two bytes for each byte read according to
some hardcoded table. I am looking for a very straightforward sample. It is pratically a binary filter.
Thank you in advance.
#include <fstream>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <string>
void show_table(const std::map<unsigned char,
std::basic_string<unsigned char> >&t)
{
std::map<unsigned char,
std::basic_string<unsigned char> >
::const_iterator it(t.begin());
while(it != t.end())
{
std::cout << unsigned int(it->first) << ": ";
for(std::string::size_type i = 0; i != it->second.size(); ++i)
std::cout << unsigned int(it->second[i]) << ", ";
std::cout << '\n';
++it;
}
}
int main()
{
std::map<unsigned char, std::basic_string<unsigned char> > table;
unsigned char hi(std::numeric_limits<unsigned char>::max());
unsigned char c(hi);
while(c)
{
table[c] = std::basic_string<unsigned char>(2, hi - c);
--c;
}
table[c] = std::basic_string<unsigned char>(2, hi - c);
show_table(table);
std::basic_ifstream<unsigned char> input("input", std::ios::binary);
std::basic_ofstream<unsigned char> output("output", std::ios::binary);
if(input && output)
{
unsigned char c(0);
while(input.get(c))
{
std::basic_string<unsigned char> s(table[c]);
if(!output.write(s.c_str(), s.size()))
break;
}
}
return 0;
}
:-)
-Mike
On Sat, 03 Jan 2004 03:39:09 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote in comp.lang.c++: "Dario de Judicibus" <no****@nowhere.it> wrote in message news:lk********************@news3.tin.it... I wish to implement a simple C++ filter that reads from a file byte by byte and writes to an output files two bytes for each byte read according to some hardcoded table. I am looking for a very straightforward sample. It is pratically a binary filter.
Thank you in advance.
#include <fstream> #include <ios> #include <iostream> #include <limits> #include <map> #include <string>
void show_table(const std::map<unsigned char, std::basic_string<unsigned char> >&t) { std::map<unsigned char, std::basic_string<unsigned char> > ::const_iterator it(t.begin());
while(it != t.end()) { std::cout << unsigned int(it->first) << ": ";
for(std::string::size_type i = 0; i != it->second.size(); ++i) std::cout << unsigned int(it->second[i]) << ", ";
std::cout << '\n'; ++it; } }
int main() { std::map<unsigned char, std::basic_string<unsigned char> > table; unsigned char hi(std::numeric_limits<unsigned char>::max()); unsigned char c(hi);
while(c) { table[c] = std::basic_string<unsigned char>(2, hi - c); --c; }
table[c] = std::basic_string<unsigned char>(2, hi - c);
show_table(table);
std::basic_ifstream<unsigned char> input("input", std::ios::binary); std::basic_ofstream<unsigned char> output("output", std::ios::binary);
if(input && output) { unsigned char c(0);
while(input.get(c)) { std::basic_string<unsigned char> s(table[c]);
if(!output.write(s.c_str(), s.size())) break; } }
return 0; }
:-)
-Mike
Since the table needs to be initialized at program start-up, can you
seriously suggest that in this particular case a map, or even a
vector, is better than:
const unsigned char lut[UCHAR_MAX][2] =
{
{ 0x00, 0x00 },
{ 0x01, 0x01 },
/* ... */
{ UCHAR_MAX, UCHAR_MAX}
};
??
That is, assuming the constants are pre-defined.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:8m********************************@4ax.com... On Sat, 03 Jan 2004 03:39:09 GMT, "Mike Wahler" <mk******@mkwahler.net> wrote in comp.lang.c++:
[snip] int main() { std::map<unsigned char, std::basic_string<unsigned char> > table; unsigned char hi(std::numeric_limits<unsigned char>::max()); unsigned char c(hi);
while(c) { table[c] = std::basic_string<unsigned char>(2, hi - c); --c; }
table[c] = std::basic_string<unsigned char>(2, hi - c);
[snip] Since the table needs to be initialized at program start-up, can you seriously suggest that in this particular case a map, or even a vector, is better than:
const unsigned char lut[UCHAR_MAX][2] = { { 0x00, 0x00 }, { 0x01, 0x01 }, /* ... */ { UCHAR_MAX, UCHAR_MAX} };
Yes, that would be the 'hard-coded table' OP referred to.
But I'm not gonna type (at least) 256 initializers (which
I'd need for a full test). So I programmatically created
the data. I leave it to OP to adjust the code to his needs
(or, of course, ignore it, and use somebody else's suggestions).
??
That is, assuming the constants are pre-defined.
Right.
-Mike
"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:jp********************@news02.tsnz.net... How about this: ....
Great code Jacques. It was exactly what I was looking for. Small, advanced.
I wouldn't be able to write it. Thank you again.
DdJ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: suzy |
last post by:
Hello,
I am trying to write a generic tool that accesses a SQL server and
reads/updates/deletes/creates records. I want to reference this tool from
my asp.net pages to talk to my db. by the...
|
by: serge calderara |
last post by:
Dear all,
I have 2 tables from which I create a simple data relation:
Table1 has a field name ID which is retrive from a database
Table2 has fields ID and Name which is retrived from database
...
|
by: fniles |
last post by:
I have a collection inside a class, sometimes when I add to the collection,
I get the error "At least one object must implement IComparable".
What does the error mean ?
Thanks.
Public Class...
|
by: =?Utf-8?B?TWlrZTAwMg==?= |
last post by:
I am really sorry if this is not the right place, or my question is too
simple; I am not a C++ developer (last time I coded in c++ was 5 years ago
:-),
-I want to write a SAPI filter for IIS6, and...
|
by: Jedora |
last post by:
Hi all,
In Matlab, we use fir1 function to calculate fir filter coefficients.
For example,
fir1(10,0.375)
Outputs are:
ans =
|
by: Girish Kanakagiri |
last post by:
How to write simple isapi filter code in C++
Just to Add "Hello World" to the Response ?
Can any one please help with initial start up so that I can build up further.
It is Urgent...
...
|
by: Bob Altman |
last post by:
Hi all,
I want to write a generic class that does this:
Public Class X (Of T)
Public Sub Method(param As T)
dim x as T = param >3
End Sub
End Class
|
by: John |
last post by:
Hi, gurus,
How can I implement the following feature in C#:
Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ", group")
For Each objMember In objGroup.Members...
|
by: =?Utf-8?B?U2hhdW5P?= |
last post by:
Hi,
I am looking to build a deepzoom centric application that works exactly like
the hardrock cafe app.
<http://memorabilia.hardrock.com/>
I need to be able to filter a total set of images to...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |