win32utils-devel@rubyforge.org
2004-Nov-16 11:54 UTC
[Win32utils-devel] win32-file: nread and nwrite
Hi all, I''ve got alpha versions of nread and nwrite now added to the win32-file class in CVS. They seem to work fine, although I''ve noticed one bug in the File#read method. Let''s say we have a text file with "hello world" in it. If I call nread with no arguments, it works fine: fh = File.nopen("C:\\test.txt") p fh.nread -> "hello world" fh.close But, if I try to specify an argument, it seems to always want to return 9 to 11 bytes for some reason: fh = File.nopen("C:\\test.txt") p fh.nread(5) -> "hello\001u\002\003\000\002" fh.close How is it picking up these extra bytes? Let''s modify test.txt to add the strings "this is a test" and "1 2 3" on their own lines. Now, if we modify our code again to read, say, 15 bytes, it works fine: fh = File.nopen("C:\\test.txt") p fh.nread(15) -> "hello world\r\nth" fh.close So, that works as expected. Why is it acting weird with strings under 12 bytes? Dan
win32utils-devel@rubyforge.org
2004-Nov-16 19:06 UTC
[Win32utils-devel] win32-file: nread and nwrite
Hi,> Hi all, > > I''ve got alpha versions of nread and nwrite now added to the win32-file > class in CVS. They seem to work fine, although I''ve noticed one bug in > the File#read method. Let''s say we have a text file with "hello world" > in it. If I call nread with no arguments, it works fine: > > fh = File.nopen("C:\\test.txt") > p fh.nread -> "hello world" > fh.close > > But, if I try to specify an argument, it seems to always want to return > 9 to 11 bytes for some reason: > > fh = File.nopen("C:\\test.txt") > p fh.nread(5) -> "hello\001u\002\003\000\002" > fh.close > > How is it picking up these extra bytes? > > Let''s modify test.txt to add the strings "this is a test" and "1 2 3" on > their own lines. Now, if we modify our code again to read, say, 15 > bytes, it works fine: > > fh = File.nopen("C:\\test.txt") > p fh.nread(15) -> "hello world\r\nth" > fh.close > > So, that works as expected. Why is it acting weird with strings under > 12 bytes? > > Dan >That is due to a common pitfall of malloc :) Insert memset(lpBuffer,0x00,dwBytesToRead+1); after lpBuffer = (char*)malloc(dwBytesToRead+1); Regards, Park Heesob --MIME Multi-part separator--
win32utils-devel@rubyforge.org
2004-Nov-17 10:33 UTC
[Win32utils-devel] win32-file: nread and nwrite
<snip>> That is due to a common pitfall of malloc :) > > Insert > memset(lpBuffer,0x00,dwBytesToRead+1); > after > lpBuffer = (char*)malloc(dwBytesToRead+1); > > Regards, > > Park HeesobThanks. Seems a strange thing to have to do. I understand what memset does, but I''m not entirely clear why I need it with malloc. I''ll read up on it some more. Dan