how to fix fread error ?

13 visualizzazioni (ultimi 30 giorni)
MiMad
MiMad il 29 Mar 2016
Commentato: dpb il 30 Mar 2016
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

Risposta accettata

dpb
dpb il 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 Commenti
MiMad
MiMad il 29 Mar 2016
thanks a lot you really help me. this instruction told me that the error come from the name of file
dpb
dpb il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Parallel Computing in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by