I can't seem to figure out how to write a non const char to a file in PROS


#1

I’ve looked at the example code provided by the pros team and some other C++ file system tutorials.
All of them require the data to be a const char. What I want is to write a motors velocity to a file. Any ideas I’m stumped? Thanks.

This is what I have right now. I can change what is put in the file with strcpy
but it also only takes a const char.

#include <cstdio>
void opcontrol() {
  char str[128];
  FILE *fp;
  strcpy(str,"love is alweys better yoe\n");
  fp = fopen("/usd/file.txt","w");
  fputs(str,fp);
  strcpy(str,"testing\n");
  fputs(str,fp);
  fclose(fp);
}

This is a link to the pros docs I looked at.
https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html


#2

fprintf be your friend.

fprintf(fp, “Value was %d\n”, val);

You can also use snprintf and the likes to format first, write later.
man snprintf


#3
void opcontrol()
{
    FILE *fp = fopen("/usd/file.txt", "w");
    std::string str;
    str = "love is alweys better yoe\n";
    str += "testing\n";
    fprintf(fp, "%s", str.c_str());
    fclose(fp);
}