how to fix fread error ?
13 views (last 30 days)
Show older comments
Hello when l run the code above l got this error . l don't where is the error, my codes (.m) and training examples are in the same working directory.
*Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in MNISTParser (line 3)
magic_number = fread(fp,1,'uint32',0,'b');
Error in MNIST_gen (line 2)
train_labels = MNISTParser('./MNIST/train-labels-idx1-ubyte');*
code 1 : MNIST_gen.m
...
**train_labels = MNISTParser('./MNIST/train-labels-idx1-ubyte');**
...
[Irrelevant code elided for brevity...dpb]
code2: MNISTParser.m
function res = MNISTParser(filename)
fp = fopen(filename,'r');
**magic_number = fread(fp,1,'uint32',0,'b');**
items_number = fread(fp,1,'uint32',0,'b');
...
[ditto]
Thanks for helps
0 Comments
Accepted Answer
dpb
on 29 Mar 2016
In function MNISTParser
fp = fopen(filename,'r');
magic_number = fread(fp,1,'uint32',0,'b');
you didn't test the return value from fopen. The problem is that the file './MNIST/train-labels-idx1-ubyte' was not successfully opened before the subsequent attempt to read from it. A plausible reason could be that the use of the relative path is not correct and the file isn't actually where that implies it must be. ALWAYS test the return value in code; it can help if you also return the optional error message and report it if it fails...
[fp,msg] = fopen(filename,'r');
if fp<1, error([msg ' File: ' filename]), end
...
would be a minimalist error check with the extra info on what the actual failure was.
2 Comments
dpb
on 30 Mar 2016
Well, to be clear it told you whatever the error in msg happened to be but for aid in determining where, I added the file name that was used for the call as well so knew which particular file it was that failed. It may or may not have been that there was something wrong in the name itself.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!