Save and load text file onto/from SD card

so currently i have an auton that reads the drivers inputs and replicates them. the problem is i use a text file to save it and i have only saved it in RAM. How can i either A) save it to an SD card or B) prevent the RAM from being dumped until the robot turns off, not when the code turns off?

Edit: scroll down, my new question is further down

What is your current code?

Most RAM will be dumped away after any computer turns off. The reasoning is simply that most RAM is a fast method for storing data temporarily but requires pretty much continuous electricity to stay on. So when you turn off the brain chances are the RAM is all gonna be flushed if not sooner then later.

That being said, you should consider storing your auton choice in ROM, which is like an SD card, or use a simple limit switch or potentiometer on your robot.

1 Like

No, it’s not the brain turning off I know that can’t work, but the code. But I may have figured out how to save a file on the sd Card? I can’t send my code because my code is unreadable and spread out very far throughout everything.

How about this: save and loadtextfile function. How do I write a 15000 character array to a text file? I have all the code for the array done don’t worry about that I just need to know how to save and load the text file, since load is coming up with an error (expected parameter declarator, expected ‘)’ )

Vex V5 SD card API is quite simple. If you already have the data in a character array (say char[15000] myData;), all you need to do to save it in a file on SD card is to call vex::brain::sd::savefile(const char *name, unit8_t *buffer, uint32_t len); function, co in your case something like:

Brain.SDcard.savefile("run1.txt", (uint8_t *)myData, sizeof(myData));

Loading would be similar:

Brain.SDcard.loadfile("run1.txt", (uint8_t *)myData, sizeof(myData));

In both cases, you should also handle return values to verify it worked the way you expected, or handle failures sensibly.

1 Like

It’s telling me it cannot use a dot operator on type. It’s a 2d array, but I can make it 1d while I’m saving, however neither 1d nor 2d arrays solve that error.

Could you share that part of your code? It seems like you are putting a dot after uint8_t or something, which you can’t do.

1 Like

I fixed the error, but I’m sure I will need more help in a bit when I test this out. I hope to god this works though first try

Not sure why you are struggling with this so much, I thought you had it solved on Friday.

Here is another example.

#include "vex.h"

using namespace vex;

uint8_t   myarray[2][1024];

int main() {
    // Initialize some values in the array
    for(int i=0;i<1024;i++) {
      myarray[0][i] = i;
      myarray[1][i] = 255-i;

    // save the array
    int nWritten = Brain.SDcard.savefile("test.bin", (uint8_t *)myarray, sizeof(myarray));
    Brain.Screen.printAt(10, 20, "wrote to file %d bytes", nWritten );

    // clear array
    memset(myarray, 0, sizeof(myarray));

    // read back from file
    int nRead = Brain.SDcard.loadfile("test.bin", (uint8_t *)myarray, sizeof(myarray));
    Brain.Screen.printAt(10, 40, "read from file %d bytes", nRead );

    // show some data we read
    Brain.Screen.printAt(20, 60,"%02X %02X %02X %02X", myarray[0][0], myarray[0][1], myarray[0][2],myarray[0][3] );
    Brain.Screen.printAt(20, 80,"%02X %02X %02X %02X", myarray[1][0], myarray[1][1], myarray[1][2],myarray[1][3] );

This initializes a 2D array with some non-zero data, writes the data to a file on the SD card, clears the 2d array to 0 and then reads back the data it wrote. Finally, it prints some data to the screen to verify it was written.


The array I used for the test was 2048 bytes long (2 x 1024) so the file on the SD card should be of that size, remember this is just raw binary data, there’s no formatting into human readable strings or anything. If you looked at the file in a hex editor it would start like this. Your file will obviously be different.


Well the problem was it saved to ram. This isn’t saving to ram though so it’s fine! Thank you for your help, since this is going to actually give us a state championship qualifier.

It immediately broke. Like after the first run thst it worked immediately. I hate this code so much. My laptop has no WiFi because it’s a long story and I can’t upload rhe code. It loads the files, it’s just returning unknown value. It’s probably because it’s in binary or hex (as you said), and I have to convert it. Now I have to go do that. The amount of time I’ve wasted on this thing…

Ok I have found nothing. How do I do this? Thank you for the help you’ve given me already. God I wish that vex had better documentation on this, but there is literally nothing on it on the internet except one little entry on robot that’s the bare minimum

There’s probably a better way of accomplishing this, but I use this for loading a text file from the sd card

#include "vex.h"
#include <fstream>

int main() {
  //open the file
  std::ifstream myfile("myfile.txt");

  //store the contents of the file in a string
  //std::string has a range constructor that copies a sequence of characters
  //the istreambuf_iterator<char>(myfile) creates an iterator for ifstream myfile
  //and the default constructed ifstreambuf_iterator is the end of stream iterator
  std::string data((std::istreambuf_iterator<char>(myfile)),

  //then you can just copy the data into your char array
  char charArray[data.length() + 1];
  strcpy(charArray, data.c_str());

Ok this has been going on for some time. My suggestion is use io and just print everything you want to the terminal then turn that into a text file and then read that file.

1 Like

Very close, jsut one error of no matching conversion for functional style cast from const char [9] to std::istreambuf_iterator. I’ll look for a solution while you’re sleeping (im sure you aren’t awake so fast and in the vex forums so).

You probably just have a typo or something. It would make it a lot easier to help you if you shared your code.

Yeah, my WiFi is broken on my laptop though. It’s really stupid. I’m gonna just type it out by hand though. Not sure how much I should write, but…

  std::ifstream Auton(“test.txt”);
  std::string doot((std::isstreambuf _iterator<char>(“test.txt”), //where the error is
  char shadow_auton[doot.length() +1][1];
  strcpy(shadow_auton, doot.c_str());

Shadow auton is a 2d array.

Aside from the typos, which I assume are from you manually typing your code, the biggest issue I noticed was here

The istreambuf_iterator constructor needs the ifstream Auton that you created as a parameter, not the name of the file. So you would do it like this


No matching constructor for initialization of string is the error now. But this seems like a simple fixable error… I’ll do some research (unless you answer before I find the solution).