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
Using a C code block
-
- Posts: 265
- http://meble-kuchenne.info.pl
- Joined: Tue Jul 13, 2021 1:53 pm
- Has thanked: 36 times
- Been thanked: 29 times
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: Using a C code block
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?)
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
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?)
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
Re: Using a C code block
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
Thanks for the quick replay again Martin
-
- Valued Contributor
- Posts: 1002
- Joined: Wed Dec 02, 2020 10:53 am
- Has thanked: 191 times
- Been thanked: 217 times
Re: Using a C code block
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
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
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: Using a C code block
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
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
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: Using a C code block
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)
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
A simple example that creates a file (on my ramdisk R:\ - you might need to change the path)
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
Re: Using a C code block
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?