Home→Forums→MonoBrick EV3 Firmware→Saving files
Tagged: File
- This topic has 7 replies, 3 voices, and was last updated 10 years, 3 months ago by Simon Stochholm.
-
AuthorPosts
-
June 19, 2014 at 09:08 #4429
Simon StochholmParticipantHow can I save a file to the EV3 while my program is running?
June 19, 2014 at 14:14 #4432
Tcm0ParticipantWhy do you want to do that?
June 19, 2014 at 14:59 #4433
Helmut WunderParticipantJFYI, to me it’s also important to create files by a running program, e.g., to store settings a/o data which can be reloaded and rewritten/updated .
Examples:
sound (rhythm) patterns for spoken words,
x,y,z coordinates for teached-in robot arm positions,
look-up tables of different kind,
log files of different kind,
recognized patterns by a neural net,
2D environment maps of rooms and mazes….June 19, 2014 at 16:55 #4434
Tcm0ParticipantOkay, I thought that you wanted to upload a program during running another program.
You can test the “general” file access options of visual studio. They might work.June 19, 2014 at 21:43 #4436
Simon StochholmParticipantI need it for saving settings, so that I can read those settings if the ev3 has been turned off. I have tried the following ways:
1) private static string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(AbstractMemory)).CodeBase);
2) static string filePath = “/home/root/apps/test.dat”;
3) static DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
4)private static string filePath = “test.dat”;
5)private static string filePath = Path.Combine(Environment.CurrentDirectory, “test.dat”);
But none of them seems to work for me.Can anyone give a good example of saving to a file on the ev3, or just the correct way to find the correct path to save the file
June 20, 2014 at 07:07 #4440
Simon StochholmParticipant[SOLVED]
It turned out that I had forgotten to serialize part of my classes.
In order to save files use this path:
private static string filePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), “test.dat”);And then just read and write bytes to and from this filePath
June 20, 2014 at 07:38 #4441
Helmut WunderParticipanthow is it possible to read/write variables other than bytes dircty,
without having to rearrange and convert data from basic bytes, e.g.
int16,
int32,
float64,
strings
arrays of single data type (e.g., arrays[6] of float)
structureslike fprintf, fscanf, and fputs etc. in C:
FILE * pFile; int n; char name [100]; pFile = fopen ("myfile.txt","w"); for (n=0 ; n<3 ; n++) { puts ("please, enter a name: "); gets (name); fprintf (pFile, "Name %d [%-10.10s]\n",n,name); } fclose (pFile); //... pFile = fopen ("myfile.txt","w+"); fprintf (pFile, "%f %s", 3.1416, "PI"); rewind (pFile); fscanf (pFile, "%f", &f); fscanf (pFile, "%s", str); fclose (pFile); printf ("I have read: %f and %s \n",f,str);
June 20, 2014 at 08:58 #4442
Simon StochholmParticipantWhat I have done is simply to serialize and deserialize an object, and then just return whatever object I need: (I had to do it this way, because the file was generated on the fly)
private static string filePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), “test.dat”);public static void Serialize(IDictionary<string, Action> combos)
{
var formatter = new BinaryFormatter();
var stream = new MemoryStream();formatter.Serialize(stream, combos);
stream.Position = 0;
var bytes = stream.GetBuffer();using (var streamer = File.Create(filePath))
{
streamer.Write(bytes, 0, bytes.Length);
}
}public static IDictionary<string, Action> Deserialize()
{
var bytes = File.ReadAllBytes(filePath);
var formatter = new BinaryFormatter();
var stream = new MemoryStream(bytes);
var action = (IDictionary<string, Action>)formatter.Deserialize(stream);
return action;
}If the file already exists on the EV3 you can simply do like this:
string text = System.IO.File.ReadAllText(filePath);// Display the file contents to the console. Variable text is a string.
LcdConsole.WriteLine(“Contents of WriteText.txt = {0}”, text);// Example #2
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(filePath);// Display the file contents by using a foreach loop.
LcdConsole.WriteLine(“Contents of WriteLines2.txt = “);
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
LcdConsole.WriteLine(“\t” + line);
} -
AuthorPosts
You must be logged in to reply to this topic.
Follow