473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pipe: FILE(16bits) to FILRE(8,16,24bits)

I need to read a file as BINARY by 16 bits units, and write to another file
as BINARY by 8, 16 and 24 bits units. It is pratically a pipe. I'm looking
for a sample of C++ code simple, performing and portable. Possibly elegant
;-)))))) Thank you in advance.

Dario de Judicibus
Jul 22 '05 #1
9 3413


Dario de Judicibus wrote:

I need to read a file as BINARY by 16 bits units, and write to another file
as BINARY by 8, 16 and 24 bits units. It is pratically a pipe. I'm looking
for a sample of C++ code simple, performing and portable. Possibly elegant
;-)))))) Thank you in advance.


Aehm. You may not have expressed yourself correctly. But reading
a file in 16 bit chunks and writing the read data in 8 bit chunks
should produce the exact same file.
Writing in 24 bit chunks can add some bytes to the end of the file
if the original filesize is not divisable by 24, but otherwise
the created file is again identical to the original.

So what is your problem ?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
Aehm. You may not have expressed yourself correctly. But reading
a file in 16 bit chunks and writing the read data in 8 bit chunks
should produce the exact same file.
Writing in 24 bit chunks can add some bytes to the end of the file
if the original filesize is not divisable by 24, but otherwise
the created file is again identical to the original.

So what is your problem ?
--
Karl Heinz Buchegger
kb******@gascad.at


Well, I was probably too short. What I meant is to build a pipe that reads a
file by 16 bit chunks, do some encoding, and writes back to another file
variable length chunks (8, 16 and 24 bits). Of course there is encoding, but
I know how to do it. What I was looking for is the most performing and
elegant way to do it with the following constraint:

1. it should work on any platform
2. it should work for Little and Big Endian
3. it should be "int-size" independent

There are a lot of ways to read/write files in C++, cin/cout with
rederection, iostreams, file open/close, and many others. Too many. That's
why I ask some guru to show me, poor mortal, the light. ;-)

DdJ
Jul 22 '05 #3
Dario de Judicibus wrote:
Well, I was probably too short. What I meant is to build a pipe that reads a
file by 16 bit chunks, do some encoding, and writes back to another file
variable length chunks (8, 16 and 24 bits). Of course there is encoding, but
I know how to do it. What I was looking for is the most performing and
elegant way to do it with the following constraint:

1. it should work on any platform
2. it should work for Little and Big Endian
3. it should be "int-size" independent

There are a lot of ways to read/write files in C++, cin/cout with
rederection, iostreams, file open/close, and many others. Too many. That's
why I ask some guru to show me, poor mortal, the light. ;-)

DdJ


Probably the most portable method is to read data into an input area,
apply endianism, apply encoding, throw into an output area.

The most efficient method is to read a large chunk into an input area,
process the data then throw into an output area. When reading large
chunks, just make them multiples of 16-bits. This method is more
efficient since you are keeping the stream flowing. Many streams
have a "ramp-up" time, which is minimalized for larger chunks of
data. This method allows the process to be split into three
asychronous tasks: input, process, output.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:vj*******************@newssvr33.news.prodigy. com...
Probably the most portable method is to read data into an input area,
apply endianism, apply encoding, throw into an output area.

The most efficient method is to read a large chunk into an input area,
process the data then throw into an output area. When reading large
chunks, just make them multiples of 16-bits. This method is more
efficient since you are keeping the stream flowing. Many streams
have a "ramp-up" time, which is minimalized for larger chunks of
data. This method allows the process to be split into three
asychronous tasks: input, process, output.


Do you have a sample skeleton for building a pipe? Just READ and WRITE. I'll
add encoding myself, of course ;-)

DdJ
Jul 22 '05 #5
Dario de Judicibus wrote:
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:vj*******************@newssvr33.news.prodigy. com...
Probably the most portable method is to read data into an input area,
apply endianism, apply encoding, throw into an output area.

The most efficient method is to read a large chunk into an input area,
process the data then throw into an output area. When reading large
chunks, just make them multiples of 16-bits. This method is more
efficient since you are keeping the stream flowing. Many streams
have a "ramp-up" time, which is minimalized for larger chunks of
data. This method allows the process to be split into three
asychronous tasks: input, process, output.

Do you have a sample skeleton for building a pipe? Just READ and WRITE. I'll
add encoding myself, of course ;-)

DdJ

#include <stdio.h>
#include <stdlib.h>

#define INPUT_BUFFER_SIZE 2048 /* 1024 * 16-bits */
#define OUTPUT_BUFFER_SIZE 4096

unsigned char input_buffer[INPUT_BUFFER_SIZE];
unsigned char output_buffer[OUTPUT_BUFFER_SIZE];

int main(void)
{
unsigned char * p_input;
unsigned char * p_input_end;
unsigned char * p_output;
size_t bytes_read;
size_t bytes_written;
size_t bytes_to_write;
bytes_read = fread(input_buffer, 1, INPUT_BUFFER_SIZE, stdin);
p_input_end = input_buffer + bytes_read;
p_output = output_buffer;
p_input = input_buffer;
while (bytes_read)
{
while (p_input != p_input_end)
{
/* The Process_Data function will increment the */
/* p_output pointer. */
Process_Data(p_input, &p_output);
bytes_to_write = &output_buffer[OUTPUT_BUFFER_SIZE]
- p_output;
if (bytes_to_write < 3 /* 24 bits */)
{
bytes_written = fwrite(stdout,
output_buffer,
1,
bytes_to_write);
if (bytes_written != bytes_to_write)
{
return EXIT_FAILURE;
}
p_output = output_buffer;
}
p_input += 2 * sizeof(char); /* 16-bit increment */
}
p_input = input_buffer;
bytes_read = fread(input_buffer, 1,
INPUT_BUFFER_SIZE, stdin);
}
return EXIT_SUCCESS;
}

In the above skeleton, I have not checked to see if the
freads were successful.

The program fills up an input buffer using data from the
standard input stream (i.e. pipe). The data in the
buffer is processed until the end of the buffer is
reached or the number of bytes read. The processing
function updates the output buffer pointer (after writing
the data to the buffer). If the output buffer is near
full, it is written to the standard output stream (possible
pipe).

You should tweak the size of the buffers to find the most
efficient size for your platform.

The above program has not been compiled nor tested. Any
errors are for the reader correct. The program is just
an illustration of a concept.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #6

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:t9******************@newssvr31.news.prodigy.c om...
bytes_written = fwrite(stdout,
output_buffer,
1,
bytes_to_write);


Thank you. Just a question: isn't fwrite synopsis

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream);

? I thout stdout should go to the end. Is it a typo or there are
compiler-dependent versions of fwrite? I use BCC.

DdJ


Jul 22 '05 #7


Dario de Judicibus wrote:

"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:t9******************@newssvr31.news.prodigy.c om...
bytes_written = fwrite(stdout,
output_buffer,
1,
bytes_to_write);


Thank you. Just a question: isn't fwrite synopsis

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream);

? I thout stdout should go to the end. Is it a typo or there are
compiler-dependent versions of fwrite? I use BCC.


Having a newsgroup doesn't free you from looking in your documentation :-)
fwrite() is standard and yes, it's a typo (most regulars reply by typing
their knowledge from head, so prepare for some easy to fix typos).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...

Having a newsgroup doesn't free you from looking in your documentation :-)
fwrite() is standard and yes, it's a typo (most regulars reply by typing
their knowledge from head, so prepare for some easy to fix typos).


No offense was intended. C++ is a great and powerful language, but after
year you often discover with surprise something new, especially when you
work on different platforms. I wished just to be sure. That's all.

DdJ
Jul 22 '05 #9
Dario de Judicibus wrote:
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:t9******************@newssvr31.news.prodigy.c om...
bytes_written = fwrite(stdout,
output_buffer,
1,
bytes_to_write);

Thank you. Just a question: isn't fwrite synopsis

size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream);

? I thout stdout should go to the end. Is it a typo or there are
compiler-dependent versions of fwrite? I use BCC.

DdJ


Yes, my bad.
One of my issues with the C streams library is that some functions
the stream is first, others it is last.

As I said, I didn't compile it, it was from the top of my head.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: jenny | last post by:
Hi, I have a java socket program running on AIX 4.3.3.0 platform. It opens a socket and sends data to our customer over a leased fractional T1 line. The line is always connected. However,...
1
by: Brad Murdoch | last post by:
im trying to run the select.selct() on a unix pipe, my expectation is that it will block untill there is something there to read, then continue. It seems to do this untill the pipe is written to...
5
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
2
by: FB's .NET Dev PC | last post by:
I am writing two services in VB.NET, one of which needs to send text strings to the other. After reading, I decided (perhaps incorrectly) that named pipes would be the best interprocess...
2
by: Steve R. Hastings | last post by:
While studying iterators and generator expressions, I started wishing I had some tools for processing the values. I wanted to be able to chain together a set of functions, sort of like the...
3
by: Jeremy Sanders | last post by:
Hi - I have some code which works under linux. It starts a remote python process using subprocess and communicates to it via a pipe created by os.pipe. As far as I understand, child processes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.