Local file system

27 Jan 2011

I have both LPC1768 & LPC2368,

I have found a curious anomaly with the LPC1768, i am basically using this as a data logger, generating a .CSV file, the file name can change periodically, so I test if the file exists, (by attempting to READ from it), if not, then I close the file, and set a flag, then RE-Open, and write a header, followed by the data.

this works with LPC2368, but crashes LPC1768

any thoughts ?

--------

Code ..

sprintf (FName, "NewFile.txt"); pc.printf ("\r\n - - Save name = %s<>",FName);

FILE * fpCSV; = fopen (FName , "a");

fpCSV = fopen (FName , "r"); Test if file Exists ??

if(!fpCSV) { pc.printf ("\r\n\r\nNO file Exists\r\n\r\n"); ****** Does this printf, and stops ** HeadderRequired = 1;

} else { pc.printf ("\r\n\r\nFile Exists\r\n\r\n"); HeadderRequired = 0; } fclose(fpCSV);

pc.printf ("\r\n\r\nFile CLOSED\r\n\r\n");

-----------

fpCSV = fopen (FName , "a");

pc.printf ("\r\n\r\nFile Opend\r\n\r\n");

if(!fpCSV) { printf( "\r\nError: The message file cannot be accessed .. CSV.TXT \r\n" ); GLCD_String (0,5, "Save WRITE FAIL:("); } else {

? Head if (HeadderRequired) { fprintf (fpCSV, "Press Name, User Name, Date, Time, Ch1, Ch2, Ch3, Ch4, Ch5, Ch6"); HeadderRequired = 0; } sprintf (Line,"\r\n%s, %s, %s, %s",StrPN, StrUN,DT, ADC_Txt); * My data *

pc.printf ("\r\n-> going to write this .. %s ..",Line); fprintf (fpCSV, Line);

fclose(fpCSV); }

15 Jul 2014

Hello,

I have found your post while searching for my solution. Basically in C library, if you are opening file with read the file must exist. What I've done was to create an empty file and put 4 spaces in it and I have checked if those spaces are present. If they are present then file must be empty and it's ready to fill. Also you're right it crashes on LPC1768.

Here is my code:

Code for files that doesn't exist

/*
Author : Baris Tanyeri
Function: opening()
Returns : None
Parameters : None
Preconditions : pass.txt must exist in the file system, must be used with newpass()
Remarks : 1st function to be called to save a password for new user
*/

void opening()
{

    char o,tw,th,fo;
    char check[4];

    o=0;
    tw=0;
    th=0;
    fo=0;
    FILE *fp = fopen("/local/pass.txt", "r"); // OPEN FILE FOR READ   
    lcd.cls(); // DELETE IF NOT USING LCD
    check[0] = fgetc(fp); //READ THE FIRST CHAR
    fseek ( fp ,1 , SEEK_SET ); //SET THE CURSOR 2 THE 2nd CHAR
    check[1] = fgetc(fp);
    fseek ( fp ,2 , SEEK_SET );
    check[2] = fgetc(fp);
    fseek ( fp ,3 , SEEK_SET );
    check[3] = fgetc(fp);
    rewind(fp);
    if(check[0]==' ' && check[1]==' ' && check[2]==' '  && check[3]==' ' ) { //IF THERE IS NO FILE, PROCEED FOR NEW PASS
        fclose(fp);
        newpass(&o,&tw,&th,&fo); //Function for filling the file
    } else {
        fclose(fp);
    }
}
14 Dec 2016

I know this post is old but i had the same problem with crashing on the LPC 1768. It seems the crash occurs only when fclose is called if there is no file. So my solution was:

bool checkSettingsExist(){
    FILE *fp = fopen("/local/settings.ini", "r");
    if(fp){
        //file exists
        fclose(fp);
        return 1;}
    else{
        //file does not exist
        return 0;}
}