The problem here is that the function fopen only takes a string. It cannot format the string so it is not replacing the %d with the value of n.
This code compiles with out the error:
#include "mbed.h"
int main() {
int n=0;
char fileName[10];//Give us room for a file name upto 10 characters long.
while(1){
n=n+1;
sprintf(fileName, "/sd/%d.txt", n);
FILE *fp = fopen(fileName, "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
}
}
Thanks, that works well. But the result is not what I want. It opens the files automatelly, which is not expected. I am expecting to open a file once I press the reset button. Would you mind giving some suggestions after having looked through the following code?
#include "mbed.h"
#include "SDFileSystem.h"
AnalogIn xin(p20);
AnalogIn yin(p19);
SDFileSystem sd(p5, p6, p7, p13, "sd");
int main() {
float xval;
float yval;
int loop=0;
//int n=0;
//char fileName[10];//Give us room for a file name upto 10 characters long
//while(1){
//n=n+1;
//sprintf(fileName, "/sd/%d.txt", n);
//FILE *fp = fopen(fileName, "w");
FILE *fp = fopen("/sd/foo.txt", "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
while(1){
loop=loop+1;
xval=xin.read();
yval=yin.read();
fprintf(fp, "x: %f, y: %f\r\n", xval, yval);
if (loop>=10000) break;
}
fclose(fp);
//if (n>=100) break;
}
I am writing a few pieces of code for opening a couple of files in the SD card. But there is something wrong with that bold code below which could not be compiled.
Does anyone know how to correct the code so that I can open many files in the SD card?