Saving files

Tagged: 

This topic contains 7 replies, has 3 voices, and was last updated by Author Image Simon Stochholm 3 years, 2 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #4429
    Author Image
    Simon Stochholm
    Participant

    How can I save a file to the EV3 while my program is running?

    #4432
    Author Image
    Tcm0
    Participant

    Why do you want to do that?

    #4433
    Author Image
    Helmut Wunder
    Participant

    JFYI, 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….

    #4434
    Author Image
    Tcm0
    Participant

    Okay, 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.

    #4436
    Author Image
    Simon Stochholm
    Participant

    I 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

    #4440
    Author Image
    Simon Stochholm
    Participant

    [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

    #4441
    Author Image
    Helmut Wunder
    Participant

    how 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)
    structures

    like 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); 
    #4442
    Author Image
    Simon Stochholm
    Participant

    What 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);
    }

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

Posted in

Make a donation