fread with a pipe and a skip
John W. Eaton
jwe at octave.org
Thu Mar 12 09:56:23 CDT 2009
On 12-Mar-2009, Olaf Till wrote:
| On Thu, Mar 12, 2009 at 09:34:13AM +0100, Olaf Till wrote:
| >
| > BTW I can not see an effect of skipping in writing to a file in 3.0.2:
| >
| > octave:1> a = "1111111111111111";
| > octave:2> length (a)
| > ans = 16
| > octave:3> b = "2222222222222222";
| > octave:4> length (b)
| > ans = 16
| > octave:5> fid = fopen ("testfile", "w");
| > octave:6> fwrite (fid, a);
| > octave:7> fclose (fid);
| > octave:8> system ("cat testfile");
| > 1111111111111111octave:9>
| > octave:9> fid = fopen ("testfile", "w")
| > fid = 3
| > octave:10> fwrite (fid, b, "3*char", 1);
| > octave:11> fclose (fid);
| > octave:12> system ("cat testfile");
| > 2222222222222222octave:13>
|
| Sorry, this part was nonsense, of course opening the file this way a
| second time discards its previous contents. But:
|
|
| octave:1> a = "1111111111111111";
| octave:2> b = "2222222222222222";
| octave:3> fid = fopen ("testfile", "w");
| octave:4> fwrite (fid, a);
| octave:5> fseek (fid, 0)
| ans = 0
| octave:6> fwrite (fid, b, "3*char", 1);
| octave:7> fclose (fid);
| octave:8> system ("cat testfile");
| 2222222222222222octave:9>
|
| shows also no effect of skipping ...
Try "od -c testfile" instead of "cat testfile". You should see
0000000 \0 2 2 2 \0 2 2 2 \0 2 2 2 \0 2 2 2
0000020 \0 2 2 2 \0 2
0000026
which shows that skipping is incorrectly implemented by writing ASCII
NUL. What I think should be happening is that seeking should be used
provided that the skip is happening inside the current extent of the
file. If the skip is happening outside the current extent of the
file, it should skip by writing NUL.
So when writing to a pipe, skipping would normally be happening by
writing NUL, and I would not expect an fseek back to the
beginning of the output stream to be reliable. So maybe we should
just disable seeking on pipes, but I'd like to know what other people
think about this before doing anything.
The other bug (writing NUL when we should be seeking) should
definitely be fixed.
jwe
More information about the Bug-octave
mailing list