Re: "fread" help

Hi Ivan,
Your file contents are the string "1,2,3,4,5".
In ASCII this is the bytecodes 0x312c322c 0x332c342c 0x350a

You are then asking fread() to read in that string as integers. Each
integer is 4 bytes long, so it can only read 2 of them.

A subsequent call to fread() picks up on the position in the file where
the last fread() left off, so it still can't read any more.

You might want to have a look at fscanf(), or if you have control over
the way the numbers are formatted in the file, it's probably best to
use "load" (try "a = [1 2 3 4 5]; save a").

Best wishes,
Hamish
On Apr 25, 2005, at 3:51, ivan wrote:

Hi,
I am a newhand to the Octave. I saw from the document that the built-in
function "fread" can read data from file. And I bulit a data file named
decimal.txt . The file contents are 1,2,3,4,5 .
I type the following command under Octave:
octave:11> index=fopen("decimal.txt")
index =
{
id = 3
name = decimal.txt
mode = r
arch = native
status = 1
}
octave:12> [data,count]=fread(index,5,"int")
data =
171051569
171182643
count = 2
octave:13> [data,count]=fread(index,5,"int")
data =
0
0
0
0
0
count = 0
Obviously the result is different from that I imagine.
The first question of mine is why the first time "fread" get such
resulst ? If I have wrongly used the arguments of the function ?
The second question of mine is why the two times result are different ?
I will appreciate for your help and advice !
Thanks !
B/R
Ivan
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------