File handling examples

Can anyone point me at some good file handling examples for the SD card on the Yun? I don't have a lot of linux experience so I'm struggling to know what sort of paths I should be using.

Many thanks

David

My experience is also limited, so I just follow some tutorials. For example this Python script will write a file to the SD card:

#!/usr/bin/python


# Open a file
fo = open("/mnt/sd/test.txt", "wb")
fo.write( "Line 1\n");
fo.write( "Line 2\n");
fo.write( "Line 3\n");
fo.write( "Line 4\n");

# Close opend file
fo.close()

If you need to do simple things, consider taking a look at the datalogger example (one of the Bridge examples)

A rule of thumb is to have a SD card with an "arduino" folder plugged in, so that the Yun makes folder /mnt/sd available, and use yun flash as least as possible

Thanks. That's really useful. I'll give it a try.

David

for Read

  File dataFile = FileSystem.open("/mnt/sd/Arduino/www/sample/file.txt");
  text="";
  while (dataFile.available()) {
        text=text + (char)dataFile.read();
                                  }
    dataFile.close();

for write

File dataFile = FileSystem.open("/mnt/sd/Arduino/www/Sample/file.txt", FILE_WRITE);
                           dataFile.seek(0);          //only to write from the beginning of file
                           dataFile.print("Hello!");
                           dataFile.close();

if you have to pass a string as the file path use this code

 String path="/mnt/sd/Arduino/www/Sample/" + STRING +".txt";
  File dataFiles = FileSystem.open(path.c_str());

All your advice really helped. I've got the programme working now.

Many thanks

David

2 years later and still helpful!

Thank you, I needed:

String path="/mnt/sd/Arduino/www/Sample/" + STRING +".txt";
File dataFiles = FileSystem.open(path.c_str());

And you beat me to it!