473,396 Members | 1,929 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,396 software developers and data experts.

stream converting

Hi there!

I have a problem concerning the file input to a c++ program: the
expected input is either in binary or in some kind of text format;
what I finally want is the binary version.
I know how to translate between these two versions and would like to
write some kind of wrapper. This wrapper should open the text file and
provide an istream which behaves exactly like the ifstream of an
equivalent binary file would. Is there a smart way to do this?
Greetings,
Moritz
Jul 19 '05 #1
3 2966

"Moritz Tacke" <ta**@informatik.uni-freiburg.de> wrote in message
news:23**************************@posting.google.c om...
Hi there!

I have a problem concerning the file input to a c++ program: the
expected input is either in binary or in some kind of text format;
what I finally want is the binary version.
I know how to translate between these two versions and would like to
write some kind of wrapper. This wrapper should open the text file and
provide an istream which behaves exactly like the ifstream of an
equivalent binary file would. Is there a smart way to do this?


No 'wrappers' or 'smartness' required:

std::ifstream input("myfile.txt", std::ios::binary);

-Mike
Jul 19 '05 #2
>
No 'wrappers' or 'smartness' required:

std::ifstream input("myfile.txt", std::ios::binary);

-Mike


Well, it looks like I didn't express myself correctly: the binary
format and the "text"-format are qualitatively different. The text
format is some kind of encoding of the binary one, where four bytes of
"text" are used to encode three bytes of the "binary".
What I am trying to do right now is to write some kind of wrapper
which reads the four "text"-bytes and converts them to the three
corresponding binary-bytes; this wrapper should be used by the other
parts of the program as if it was an ifstream which directly reads the
binary file.

Hm. I hope I did succeed in formulating the point this time...
Greetings!
Moritz
Jul 19 '05 #3
"Moritz Tacke" <ta**@informatik.uni-freiburg.de> wrote in message
news:23**************************@posting.google.c om...
....
| > std::ifstream input("myfile.txt", std::ios::binary);
....
| Well, it looks like I didn't express myself correctly: the binary
| format and the "text"-format are qualitatively different. The text
| format is some kind of encoding of the binary one, where four bytes of
| "text" are used to encode three bytes of the "binary".
| What I am trying to do right now is to write some kind of wrapper
| which reads the four "text"-bytes and converts them to the three
| corresponding binary-bytes; this wrapper should be used by the other
| parts of the program as if it was an ifstream which directly reads the
| binary file.
Hi Moritz,

The way to do what you are looking for is to implement a custom
streambuf class (the polymorphic buffering class which handles
the i/o behind any standard stream). Your implementation
of this file would generate the buffer by translating data
read from another stream.
For some examples, you may check Dietmar Kühl's website:
http://www.informatik.uni-konstanz.de/~kuehl/
Dietmar authored part of Josuttis' (excellent) book
"The C++ Standard Library - A Tutorial and Reference" (in particular
sections related to the i/o stream library), a valuable reference.

But I think that you would be picking the wrong level of
abstraction by working at the level of the streams.
It would probably be better to define a higher-level
interface (abstract base class) to read the contents
of a file, and implement it for both the ascii and binary
file formats.
Then, if needed, you can do the same to write both
formats, and -- if needed -- perform a translation
on-the-fly.
Regards,
Ivan
--
http://ivan.vecerina.com
Brainbench MVP for C++ <> http://www.brainbench.com

Jul 19 '05 #4

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

Similar topics

2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
1
by: Rasika | last post by:
I have zip file called "PPC_DATA.zip" that contains four jpg images. Using the code below I can get a ZipEntry, which can be converted into a Stream by using zFile.GetInputStream(e). I want to...
1
by: Guy Korland | last post by:
Hi, 1. How can I read stream in blocking mode? Meaning stop the thread till it get a new line sign? Or getting an event from the stream on new income data? 2. How can I convert 2 bytes to...
5
by: cs | last post by:
I need to get the position and also seek in a network stream, is there an easy way to do that? (I am converting some java code so thats the easy path).
1
by: Martin Widmer | last post by:
Hi Folks In my object I am trying to implement a function in order to render the picture to XML (to generate RDL for SQL Server). That Function causes the error mentioned in the subject at the...
3
by: DC | last post by:
Hi, I am trying to load a pdf file into a memorystream and upload this file to the browser. However, wenn I run the attached code (the ftpStream is a valid stream of the pdf file) I receive an...
6
by: rh1200la | last post by:
Hey All. I'm trying to download an image from a URL and display it in my page. I'm getting an error converting the Stream to Memory Stream. I'm getting a Specified cast is not valid error for...
7
by: The Cool Giraffe | last post by:
I have created the following conversion in the header file for my struct called SomeStruct. operator std::string () {return "some useless text";} I'm using it as follows. SomeStruct someS;...
2
by: Mike P2 | last post by:
I made a Stream-inheriting class that just removes the tabs (actually the 4 spaces VS prefers to use) from the beginning of lines and empty lines. At first I was having trouble with it adding a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.