LocalFileSystem (read)

23 Feb 2011

Hi All... I'm sure this may be a stupid question but I've looked and Looked and cannot seem to find it...

I want to use Local file system to hold configuration information for my program. I understand how to declare (create) and write to a file. I also understand that you cannot simply open a file and change the contents of it... That the file would be overwritten if you want to use the same file name...

In no examples can I see how you read the contents of a file into the mbed?

If I decalred a string variable in the mbed, how would I open the local file and copy it's contents into the string variable?

I was hoping to use the iniparser app (that's really what I want) but I cannot figure out how to make it work...

Thanks in advance...

Tim

23 Feb 2011

open the file first

FILE *file = fopen("path", "read_write_append_mode");

count characters in file

for(i=0; 1; i++) if(fgetc(file) == EOF) break;

return file position pointer to start of file

fseek(file, 0, SEEK_SET);

allocate a pointer to hold contents of file

char *out = (char*) malloc(sizeof(char) * i+1); +1 to hold the end of string

copy contents to pointer

fscanf(file, "%s", out);

wait(0.5);

close file pointer

fclose(file);

print content

printf("%s", out);

23 Feb 2011

Thank you for this.... Let me chew on it a bit...

Any more detail on this? "read_write_append_mode";

I know you cam open the file as "r" or "w" but I do not know of append?

How would you append text to a file?

Actually, is there a document of all the file methods that are available?

--

23 Feb 2011

append basically means writing to the file starting from the end

so if you have something like

old_file_content

opening it in append mode and writing something to it

fprintf(file_pointer, "%s", "_new_content");

the final content is going to be

old_file_content_new_content

23 Feb 2011

You've been a great help... To open it in append mode is the code "a"?

And it would be a vbig help if I knew how to look at all the methods available for the file object... Are these C++ standard file methods?

--

23 Feb 2011

you can start here

http://www.exforsys.com/tutorials/c-language/file-management-in-c.html

most of the time, I use fprintf, fscanf, fseek. I believe these are standard C file operations

23 Feb 2011

Thanks again! Have a great day...

Please log in to post a reply.