Hi Tony,
It looks like you are trying to create a "global" file pointer, so the basic code you are looking for is:
LocalFileSystem local("local");
FILE *fp;
and then in main, as your file pointer is already defined, you'd just do:
int main() {
fp = fopen("/local/out.snd", "w"); // open file, and store pointer in global FILE*
I expect this will do what you want.
As an aside, if you wanted to avoid a global FILE pointer, you could always pass the file pointer to your functions. Something like:
#include "mbed.h"
LocalFileSystem local("local");
void do_something_to_file(FILE *fp) {
fprintf(fp, "Hello World\n");
}
int main() {
FILE *p = fopen("/local/out.txt", "w");
do_something_to_file(p);
}
Hope that helps!
Simon
Simon suggested I open a new topic for this. I am trying to convert the sound recorder cookbook code to record to an internal filing system file for later re-use. I havn't yet been able to find a variable type which will allow the calls which use the file pointer to compile. The problem is that the sub-routines using the pointer come before it is created in the fopen command. If you swap the order round - putting main() first, then the calls to the subroutines wont compile.
Tony
Code here:
/* afa 3 Sep 20009 - uses sound recorder code to write to a local file */ #include <stdint.h> #include "mbed.h" DigitalOut l1(LED1); DigitalOut l2(LED2); DigitalOut l3(LED3); DigitalOut l4(LED4); AnalogIn ain(p20); Ticker ticker; Timer t; LocalFileSystem local("local"); int * fp; // have tried several different variable types here - none work /* Sampling routine; records a single sample */ void sample() { static const float ALPHA = 0.999f; static float v; float l, s = ain.read(); /* Convert raw value to sample */ int8_t b = (s - 0.5f) * 127.0f * 2.0f; /* Compute next bar graph value */ l = abs(s - 0.5f) * 2.0; v = l > v ? l : v * ALPHA; /* Snazzy bar graph */ l1 = v > 0.08; l2 = v > 0.16; l3 = v > 0.32; l4 = v > 0.62; fprintf(fp, "%c", b); } /* Write a 32 bit value in correct byte order for SND files */ void write32(uint32_t v) { uint8_t *b = (uint8_t *)&v; fprintf(fp, "%c%c%c%c", b[3], b[2], b[1], b[0]); } int main() { FILE *fp = fopen("/local/out.snd", "w"); // Open "out.snd" on the local file system for writing /* Au format header: http://en.wikipedia.org/wiki/Au_file_format */ fprintf(fp, ".snd"); write32(24); write32(-1); write32(2); write32(8000); write32(1); /* Sampler */ ticker.attach(sample(fp), 1.0f / 8000.0f); t.start(); while(t.read < 10.0) { wait(1); } t.stop(); fclose(fp); }