11 years, 4 months ago.

Error in fopen

I have used "fopen" to open a file,when execute the "if" clause,success to open file,program keep going on.but execute the "else" clause,program is stop.please help me.

#include "mbed.h"
int set_base(char* base)
{
    FILE *file;
    sscanf(base,"%[^$]$%[^;];%s",BASE_NUM,ip,port);
    printf("BASE_NUM is %s\r\n",BASE_NUM);
    printf("ip is %s\r\n", ip);
    printf("port is %s\r\n", port);
    if((strncmp(ip,"0.0.0.0",7)==0)||(strncmp(port,"0000",4)==0)) {
        file=fopen("/local/base.txt","r");//program keep going on
        if(file==NULL) {
            printf("open file base.txt failed\r\n");
            return -1;
        }       
        fscanf(file,"%*[^$]$%[^;];%s",ip,port);
        printf("ip is %s\r\n", ip);
        printf("port is %s\r\n", port);
        fclose(file);
    } 
    else {       
        printf("ready to write file base.txt\r\n");
        file=fopen("/local/base.txt","w");//program stop at here
        if(file==NULL) {
            printf("open file base.txt failed\r\n");
            return -2;
        }   
        fprintf(file,"%s",base);
        //fwrite(base,strlen(base),1,file);
        fclose(file);
        printf("Success update base.txt\r\n");
    }
    return 0;
}

Put the code tags on a seperate line please, otherwise they dont work.

posted by Erik - 05 Dec 2012

int set_base(char* base) { FILE *file; sscanf(base,"%[^$]$%[^;];%s",BASE_NUM,ip,port); printf("BASE_NUM is %s\r\n",BASE_NUM); printf("ip is %s\r\n", ip); printf("port is %s\r\n", port); if((strncmp(ip,"0.0.0.0",7)==0)||(strncmp(port,"0000",4)==0)) { file=fopen("/local/base.txt","r");program keep going on if(file==NULL) { printf("open file base.txt failed\r\n"); return -1; } fscanf(file,"%*[^$]$%[^;];%s",ip,port); printf("ip is %s\r\n", ip); printf("port is %s\r\n", port); fclose(file); } else { printf("ready to write file base.txt\r\n"); file=fopen("/local/base.txt","w");program stop here if(file==NULL) { printf("open file base.txt failed\r\n"); return -1; } fprintf(file,"%s",base); fwrite(base,strlen(base),1,file); fclose(file); printf("Success update base.txt\r\n"); } return 0; }

posted by young shunfu 06 Dec 2012

1 Answer

8 years, 9 months ago.

I am trying to read and write files from SDcard using the RedBear BLE Nano (nRF51288). I was able to read and write files on the SDCard, using the library: "SDFileSystem.h".

But when I run the fopen function with BLE Heart Rate example, the code stops running. It seems that when the .hex is above 400KB fopen stops working.

Anyone faced this problem before?

Are there alternative for fopen function? I tried the sd.open but that didn't work with nRF51288.

Andre