6 years, 10 months ago.

Create a .csv File

Hi I am trying to create a .csv file from values recorded by one of the Analog Pins and I am having issues with finding the file's pathway. I have a Mac and I am trying to create and write the .csv file in Documents. I opened the Document folder and selected GotInfo by right-clicking it and in the Where section it said: Machintosh HD > Users > steveezuniga > Documents. I copied and pasted that and got: /Users/steveezuniga/Documents. I built this and got: ERROR. Failed to open output file. I have attempted to put: /Macintosh HD/Users/steevezuniga/Documents and /Macintosh Hd/steevezuniga/Documents. But same error. Here is the code:

#include "mbed.h"

AnalogIn ain(A0);
DigitalOut led(LED1);

int main(){
    FILE *fp = fopen("/Users/steveezuniga/Documents", "w");
    if (fp) {
        for (int i = 0; i < 100; i++){
            fprintf(fp, "%f\n", ain.read());
            wait(0.05);
        }    
        fclose(fp);
        } else {
        printf("ERROR. Failed to open output file\r\n");
      }
}

1 Answer

6 years, 10 months ago.

hi Jessica,

When inserting code, you should enclose it in <<code>> and <</code>> tags as described in the "Editing tips" info in the bottom right corner of this edit box.

I don't have the nucleo mbed module, so the answer might be a bit different. If it has the local file system, then you would need a construct like:

LocalFileSystem local("local"); 

At least on the LPC1768, the local file system is also visible from the PC into which it is plugged.

Then the path in your fopen would be something like:

    FILE * fp = fopen("/local/file.csv", "w");

After this, you can browse to the mbed from your mac and find the csv file.

Here's a link that may be helpful.

Thank you David. I implemented the <<code>> tags. I am using a NUCLEO_F303RE. So I cannot use the LocalFileSystem. Do you have any other suggestions?

posted by Jessica Ballard 07 Jun 2017

Jessica,

Does your hardware have (or can you add) a micro SD card, or a USB memory stick. There have been some drivers for each of these as a Fat File System - I've used them with mbed os 2, but I can't yet find a combination that works for mbed os 5.

Alternately, and more complex, you could host a web server on your mbed, and get the files with your browser.

I don't know if anybody has created an mbed library to access a file server via Ethernet, so I've been focusing more on "self-contained" to the mbed apps.

With USB to your mac, perhaps a custom app on the mac could be created - I've not done that either.

posted by David Smart 07 Jun 2017