Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I currently have a VC6 app that takes a text file as input(records vary in length and have a unique field delimiter as well as CRLF). Below is the basic code:
string line;
ifstream infile("test.txt",ios::in);
while (!infile.eof())
{
   getline(infile,line);
}
...

The new input will have the same format(unique delimiters and LF retained), but be a binary file due to data requirements. If I change the above to open in binary mode, is this a valid means of reading a binary file line by line w/o data loss? The LF makes this possible even if it feels like it goes against the grain on how to read binary files.
Posted

Technically, it should still work. But there will always be a danger of some new binary data being inserted that includes new line characters, which will mess up the file read.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 19:14pm    
You see, you cannot give as answer without ambiguity like that. The final answer depends on what exactly is their idea behind the design of the representation of data in stream -- please see my answer. The issue is kind of "philosophical". (Oh, I'm afraid of aggressive reaction against my answer from some "readers", but... it's OK :-)
Thank you.
--SA
Here is the key: there is no such thing as non-binary data. "Text" data/file/mode is not an antonym of "binary", it's merely an indication that the data is processed in special way, which is system-dependent. So, this makes the statement binary file due to data requirements completely unclear.

In particular, making stream binary cannot harm unless you put end-of-line is some inconsistent way. But you're not going to! As I can see, you only want to complement your operation with binary stream output, nothing else.

The opposite is dangerous: it your "binary" format is designed to be system-independent and in strictly defined in the "every bit matters" stile, your text operation can mess up your system-independent "write line" operation. In this sense, it is not dangerous to use binary stream, it's dangerous to read/write it in system-dependent manner (and binary mode is designed to avoid it).

Another meaning of "text" is how numerical values are expressed -- this is obvious.

So, the final answer really depends on "data requirements" which you did not share with us. I would say, generally the idea of mixing up binary format with such text-specific options as "line" based on any kind of delimiters is extremely bad. This is a problem of data design, not implementation.

And this is much more important to clean up "data requirements" then choose a proper implementation. I would advice not even start implementation until possible issues of "data requirements" are made clear.

Based on my experience I would highly recommend to avoid using any delimiters in binary format. Every logical chunk of data should be prefixed in the stream by the length descriptor, so the implementation would know how many bytes to read from the stream at once into some structure. Usually the goal is to store/load of some data structure with a good performance because parsing is avoided — makes full sense.

Thank you.
—SA
 
Share this answer
 
Comments
JF2015 27-Jan-11 4:00am    
I like! 5+
Sergey Alexandrovich Kryukov 27-Jan-11 16:12pm    
Thank you!
--SA
You should use Read/Write function for binary mode and ReadString/WriteString for text mode.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900