Using a C code block

For general Flowcode discussion that does not belong in the other sections.
WingNut
Posts: 254
http://meble-kuchenne.info.pl
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Using a C code block

Post by WingNut »

Good evening all. I want to use a C code block to write data to specific locations in a file. The below is what I would like to use (with all the error checking removed - just the basic functionality. So what is intended is that the variable FCV_Samplerate is written to the first byte of the file
I understand the basics oc but converting to something flowcoe will understand is confusing me
Do i need the directive?
Does this look like it should work?
I'm struggling with the c code use in flowcode and any videos I've seen are very old and the tutorials look so very different then FC9
Any help would be great

#include <stdio.h>
{
FILE *f;
f = fopen("CFG_DAT.DAT", "r+b")
fseek(f, 0, SEEK_SET);
fputs(FCV_SampleRate,f);

}

Cheers
N

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Using a C code block

Post by mnfisher »

Depends what you are trying to do here...

FC doesn't seem to allow C code blocks in App developer files. So if that is the target then no go.

However - App developer does have file operations - and it would be easy to use these to set the position and write SampleRate (note that fputs takes a 0 terminated string - is sample rate a string?)
file ops.png
file ops.png (79.63 KiB) Viewed 1637 times
You can use Open, SetPos and WriteArray for example.

On an mcu - there is no disk (sadly open etc are not that device independent) - so you 'll need to write to EEPROM or SD card as appropriate.

Martin

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Using a C code block

Post by WingNut »

It is a string. It was originally written so as to be read from the file as a default set of settings. That works a treat but i need the flexibility to over write the settings for someone to change the usable parameters via the buttons and menu ive made. I'll take a look at app developer again (think i did some months ago). Just thought the c- code block would be easier as I do have an understanding of c

Thanks for the quick replay again Martin

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: Using a C code block

Post by stefan.erni »

Hi Wingnut

Can You have a look to the last two post?
Open just a buffer(512byte) from a file and write it again back with the new edited values.
Maybe its help.

https://flowcode.co.uk/forums/viewtopic ... +open+file

regards

Stefan

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Using a C code block

Post by WingNut »

Doesn't work :(
Don't know why :(
Help :(

Maaaartiin :)
Attachments
MSaveCfg.JPG
MSaveCfg.JPG (19.12 KiB) Viewed 1606 times

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Using a C code block

Post by WingNut »

It does work!!!!!
Think i need to experiment with it as it seems hit or miss atm

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Using a C code block

Post by mnfisher »

Should use Length$(CfgStr) to write out the string (or Length$(CfgStr) + 1 to include the 0 terminator) Otherwise you will get some 'cruff' from the uninitialized memory locations

You've used setPos to set the position to 10 in the file - From can be ::File.Seek_begin, Seek_curr or Seek_end (click on the little 'triangle' at the end - I guess seek_begin should correspond to 0...

However - if the file is empty (newly created) - seeking to position 10 may not work (should it pad the file to 10 bytes ?) - so it will probably work as you expect on the second run - when there are some bytes in the file.

Martin

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Using a C code block

Post by mnfisher »

Also you might need to create the file first - if it doesn't exist..

A simple example that creates a file (on my ramdisk R:\ - you might need to change the path)
file_test.fcsx
(7.94 KiB) Downloaded 43 times
Note that you need to use Open instead of create if the file exists... It would be helpful if there were Close and Exists functions? You can mimic exists by using open first - then checking if handle is 0 (doesn't exist)

Martin

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Using a C code block

Post by WingNut »

The file is already created and has a default config array in place. Ive tried it with the file abs empty and with some random characters too. It seems that when i open the file to check if the array has been written it can be one of 2 things -successfully written or blank. If i delete any white space and try to save it says another procees is using the file as if flowcode hasnt closed the file. I setpos to 10 to see if it would write to that location and it did but again hit or miss. So i then started wondering where it would look for the file. The fat component specifies the application root but this has no means to do that. I see from your note above that you specified your r: drive. If i do that how will it work with my sd card on my project?

mnfisher
Valued Contributor
Posts: 953
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 507 times

Re: Using a C code block

Post by mnfisher »

Can you upload/pm your code? I don't think it will work with sd card unless you use the sdfile component?

Martin

Post Reply