File opening and closing from different functions within a task.

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

File opening and closing from different functions within a task.

跳至解决方案
846 次查看
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 解答
647 次查看
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 回复数
647 次查看
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 项奖励
回复
648 次查看
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 项奖励
回复
647 次查看
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 项奖励
回复