File opening and closing from different functions within a task.

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

File opening and closing from different functions within a task.

ソリューションへジャンプ
931件の閲覧回数
razed11
Contributor V

Hi Everyone,

If function A opens a device as a file then calls function B that opens this same device, writes, closes, and returns what is the state of the file that function A is referencing?

Something like:

f = open()

B(); // Opens same file, writes and closes.

write(f, ...)

close(f)

Thanks,

K

0 件の賞賛
返信
1 解決策
732件の閲覧回数
RadekS
NXP Employee
NXP Employee

In fact, open (fopen) creates data (file pointer) in RAM, therefore you have to call close (fclose) in both functions.

However current version of MFS doesn’t allow you to open one file twice for write. MFS isn’t prepared for simultaneous write to one file and this situation should be handled by error code when you try opening file for write for the second time (I didn’t test it).

For now, I do not see any reason why I would like to open one file twice in the same task.

Anyway, next version of MFS will contain this feature (you can write simultaneously) and this new version of MFS will be available in next MQX release (probably MQX 4.2).


Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

元の投稿で解決策を見る

0 件の賞賛
返信
3 返答(返信)
732件の閲覧回数
thiagow
Contributor III

And about a fopen for write and another fopen to read in the same file, is any problem is MQX4.1.1 ?

0 件の賞賛
返信
733件の閲覧回数
RadekS
NXP Employee
NXP Employee

In fact, open (fopen) creates data (file pointer) in RAM, therefore you have to call close (fclose) in both functions.

However current version of MFS doesn’t allow you to open one file twice for write. MFS isn’t prepared for simultaneous write to one file and this situation should be handled by error code when you try opening file for write for the second time (I didn’t test it).

For now, I do not see any reason why I would like to open one file twice in the same task.

Anyway, next version of MFS will contain this feature (you can write simultaneously) and this new version of MFS will be available in next MQX release (probably MQX 4.2).


Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 件の賞賛
返信
732件の閲覧回数
razed11
Contributor V

I had written a class for debug output that I might want to instantiate on the stack as any random point. I was trying to figure out how to handle the case where I might already have the file open.

Thanks for responding.

0 件の賞賛
返信