digitalmars.D.learn - File I/O - rawWrite issues
- Carl (17/17) May 08 2013 I am learning D so I thought this would be the correct place for
- Meta (3/8) May 08 2013 Since ubytes are pretty much the same as chars, I think writing 0
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/18) May 08 2013 True from the point of view of sizes: they are both 8-bit wide. However,...
- Carl (5/5) May 08 2013 Wow, I feel silly.
- H. S. Teoh (25/30) May 08 2013 [...]
- 1100110 (4/9) May 15 2013 +1
- Idan Arye (18/36) May 08 2013 Works fine on my Arch linux(64bit):
I am learning D so I thought this would be the correct place for this question (I assume I am making an error, not the compiler). I am writing arrays of ubytes using rawWrite to a file. Whenever I write arrays full of numbers I have no problems, but once I write an array with zeroes I can then no longer write to that file. For example: ubyte[] data = [5, 34, 9, 45]; file.rawWrite(data); // OKAY ubyte[] data = [0, 0, 0, 45]; file.rawWrite(data); // NOT OKAY I get no errors or exceptions, it just won't write the array. I can't even open a new file stream and write, not even after rerunning the program. My only guess is some kind of string related problem where '\0' causes it to terminate. Thanks in advance. P.S. DMD, Ubuntu 11
May 08 2013
For example: ubyte[] data = [5, 34, 9, 45]; file.rawWrite(data); // OKAY ubyte[] data = [0, 0, 0, 45]; file.rawWrite(data); // NOT OKAYSince ubytes are pretty much the same as chars, I think writing 0 to the file will actually write the null character, which probably isn't a good thing.
May 08 2013
On 05/08/2013 02:46 PM, Meta wrote:True from the point of view of sizes: they are both 8-bit wide. However, they are different beasts: char is merely a UTF-8 code unit. ubyte does not have such a meaning attached to it.For example: ubyte[] data = [5, 34, 9, 45]; file.rawWrite(data); // OKAY ubyte[] data = [0, 0, 0, 45]; file.rawWrite(data); // NOT OKAYSince ubytes are pretty much the same as chars,I think writing 0 to the file will actually write the null character, which probably isn't a good thing.I don't see any problem there. rawWrite() should just write 0. I think Carl should provide a short program that demonstrates the problem. :) Ali
May 08 2013
Wow, I feel silly. In the process of making sample code I found the problem: I have been reading the output with a text editor instead of a hex editor. To think I spent hours on this problem... *face palm* Thanks for your quick replies.
May 08 2013
On Thu, May 09, 2013 at 01:11:00AM +0200, Carl wrote:Wow, I feel silly. In the process of making sample code I found the problem: I have been reading the output with a text editor instead of a hex editor. To think I spent hours on this problem... *face palm*[...] No need to be embarrassed; it happens to the best of us. IME, some of the most frustrating, hair-pulling bugs that take hours (or days!) to get to the bottom of often turn out to be dead-easy trivial mistakes that got overlooked because they were *too* obvious. I remember an embarrassing occasion where I got so frustrated that the compiler is adamantly producing wrong code when the source code is *obviously* correct, that I reinstalled the entire GCC toolchain, C library, the entire works, only to find that it *still* didn't help. Finally I discovered that it had nothing to do with the compiler / libraries at all. It was a trivial typo substituting , for ; in the wrong place in my code, thus subtly changing its meaning while leaving only a few pixels difference in the appearance of the source code. I spent the better part of a day finding this problem. *sigh* Or another occasion where I got so frustrated when a code change deep inside some complicated algorithm mysteriously had no effect on the output whatsoever, even after inserting printf's into the code and verifying that the new code did actually get run. Many hair-pulling hours later, I suddenly realized that I had been looking at the wrong output file -- that containing stale output from an earlier test run before the code change. *face palm for 5 full minutes*. T -- Why do conspiracy theories always come from the same people??
May 08 2013
On 05/08/2013 07:29 PM, H. S. Teoh wrote:=20 No need to be embarrassed; it happens to the best of us. IME, some of the most frustrating, hair-pulling bugs that take hours (or days!) to get to the bottom of often turn out to be dead-easy trivial mistakes that got overlooked because they were *too* obvious.+1 I'm not even going to mention the times that one simply forgets to recompile... Now _that's_ embarrassing.
May 15 2013
On Wednesday, 8 May 2013 at 21:34:00 UTC, Carl wrote:I am learning D so I thought this would be the correct place for this question (I assume I am making an error, not the compiler). I am writing arrays of ubytes using rawWrite to a file. Whenever I write arrays full of numbers I have no problems, but once I write an array with zeroes I can then no longer write to that file. For example: ubyte[] data = [5, 34, 9, 45]; file.rawWrite(data); // OKAY ubyte[] data = [0, 0, 0, 45]; file.rawWrite(data); // NOT OKAY I get no errors or exceptions, it just won't write the array. I can't even open a new file stream and write, not even after rerunning the program. My only guess is some kind of string related problem where '\0' causes it to terminate. Thanks in advance. P.S. DMD, Ubuntu 11Works fine on my Arch linux(64bit): import std.stdio; void main(){ auto oFile = File("testfile","w"); ubyte[] data1 = [0, 0, 0, 45]; oFile.rawWrite(data1); ubyte[] data2 = [1, 2, 3, 4]; oFile.rawWrite(data2); oFile.close(); auto iFile = File("testfile","r"); ubyte[16] buffer; writeln(iFile.rawRead(buffer)); iFile.close(); } Prints: [0, 0, 0, 45, 1, 2, 3, 4] If you were working on windows I would have suggested to try opening the file in binary mode.
May 08 2013