File opening and closing from different functions within a task.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

File opening and closing from different functions within a task.

Jump to solution
930 Views
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 Kudos
Reply
1 Solution
731 Views
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!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
Reply
3 Replies
731 Views
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 Kudos
Reply
732 Views
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 Kudos
Reply
731 Views
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 Kudos
Reply