Reading line from flash

I have a very simple problem. How can I read a line from a text file stored on the flash memory of my RMC4200? I have written to the file using fat_write finishing each line with
When I read the text (128 bytes at a time, longer than length of text line) with fat_read some lines are read o.k. and some have funny numbers. I don’t necessary know the length of each line.
I tried reading character by character using a while loop and assembling the characters using strcat bat that didn’t work at all. The program timed out if I put the strcat statement in.
There must be a simple solution to this!

Thanks, Karsten

Figured it out myself. This is probably the dirty way of doing it and doesn’t require a function returning a string and messing around with pointers. I still have to completely get my head around pointers.

read_str reads the file character by character until it finds whatever is specified in the while statement. In my case I wrote a whole lot of ‘integer’ and string values separated by , to the file. Some of the strings get converted back to integer in the main program via atoi(buf). Because I don’t want the , after all the strings (wouldn’t matter for the integer conversion) I copied all the strings except the last one into a new variable and appended the null terminator.

Works a treat,


#class auto
#define FAT_BLOCK
#use “fat.lib”
#use RCM42xx.LIB

FATfile settings_file;

char buf[128];

char read_str()
char buf2[128];
char s[1];
int rc;

buf2[0] = '\0';
    rc = fat_Read(&settings_file, s, 1);
    sprintf(buf, "%*.*s", rc, rc, s);
}while(*buf != ',');

strncpy(buf, buf2, strlen(buf2) -1);
buf[strlen(buf2) -1] = '\0';