PROS arrray to string

I need to append data to an array and then convert the array into a string and then decode the string back into an array again! I hope this makes sense!

Top Google searches for converting an array to a string and then vise versa, should give you what you need (if I understand your question).


Thank you, unfortunately, I meant an array of integers, not characters!

1 Like

To convert a number to a string, use std::to_string(number).
std::string implements an addition operator as well as a member function append, so you could do something like

int myArray[5];
std::string myString;
for(int i = 0; i < 5; i++) {
  myString = myString + std::to_string(myArray[i]);
  // OR you can do this

No, it doesn’t. Are you are trying to do a rerun thing?

1 Like

Like 4173cj was saying, if this is an attempt at rerun code then you may as well put the integers into a 2D array and (probably the best way) save it to an SD card in the brain, and then just read that file line for line to get the data for playback. If it was for something else, ignore what i said I guess :wink:

As others have said, I think you need to provide more information, such as:

  • What kind of data are you storing in the array?
  • Are you expecting the data to be in any particular format in the string?

Also, beware of the XY problem. What are you actually trying to do here? There may be a much better solution. (Manually serializing data to a text format can easily become an unnecessarily complex solution).


I am trying indeed to create an autonomous rerun system that stores the data on the sd card. Sorry I wasn’t clear at the start.

Whats the purpose in storing it as a string then? You can just save the array of integers as is and read back from it, it doesnt need to be a string. If there is a reason for using strings, please clarify it because i dont understand the need for converting if you are planning on converting it back anyway.


I didn’t know you could store non string types in a text file.

1 Like

Im not an expert by any means on SD card usage with V5 (ive never done it actually), but i know an old teammate who used iostream to just save a 2d array of integers to the sd card and it worked just fine

There’s different ways you could do it. In some text format is not a bad idea, and while others may technically more efficient, the easiest is probably some form of delimited text (such as CSV - comma-separated values).

I’d recommend planning out how you expect this stored data to look before writing the code. Think of some sample data, write down how you could store it, then figure out what code is needed to convert it to and from that format.

Hopefully this makes sense.

1 Like

I was thinking of having a new line for each value

In that case, to write the file, you can open it with a std::ofstream, and write to it in the same way you’d write to std::cout:

// Open the file
std::ofstream file{"/usd/filename.txt"};
// To write the integer variable `x` to the file (with a newline after):
file << x << std::endl;
// The file will be closed when the `file` variable goes out of scope

Then to read it in, you can do something similar with std::ifstream:

// Open the file
std::istream file{"/usd/filename.txt"};
// To read a number into the integer variable `x`:
file >> x;
// The file will be closed when the `file` variable goes out of scope

Hopefully this makes sense. If the reading and writing syntax doesn’t make sense, I suggest you review some tutorials on I/O in C++.

1 Like

I tried this but I am getting


You should learn more c++.
The compiler just told you everything that is wrong.

The example provided above had a small typo, but they linked you to the documentation where you could try to figure it out yourself. The typo was that istream was actually supposed to be ifstream. The documentation also showed that ifstream belongs to the <fstream> header, which you need to include into your project

#include <fstream>

Next, you have a error where it says “read was not declared in this scope”. This means that you did not create a variable name read, which you need to be able to write to it.

Finally, you have this error:

printf("Left: "%s, read);

Hopefully the syntax highlighting makes the error obvious: %s needs to be inside the string (inside the quotes), not outside.

Hopefully that solves your problem, and don’t be afraid to analyze the compile error, read docs, or do your own research on google.


I already had included and the error is still

Did you read my reply?

Thanks, it now compiles!
Unfortunately, the code:

std::string read;


std::ifstream file(filename, std::ios::app);
file >> read;

printf("Left: %s", read);

Gives the error:
in the terminal!

That probably has nothing to do with your code, that is a problem in the PROS cli.
Recompile your project using a clean build, upgrade your PROS installation, or restart your computer.

I’m not sure what else would be wrong, unless you are sending garbage to the terminal.

1 Like