Why doesn't fopen work the first time, but work the second time?

I use Matlab to create a new file, calling

fid = fopen(filename,'w')

since the file name does not exist, it should create a new file and give me a valid file descriptor. Instead, it returns -1. If I run the code again, I get fid = 3.

This runs on ubuntu, but it seems to work fine on windows, and I can't figure out why.

-Mike

+3
source share
2 answers

Not sure if this helps, but note that if the folder does not exist, fopen with 'w' cannot create the file and therefore returns -1.

+2
source

fopen . ,

[fh, failmessage] = fopen( fname, 'wt' );
if fh == -1
    error( 'Failed to open %s: %s', fname, failmessage );
end
0

Source: https://habr.com/ru/post/1756584/


All Articles