Vexcode How to Save Output Values to Separate txt File

This is a rewrite of my previous post (now deleted) that gives more information about what the issue is. I’m trying to record motor values and speeds during driver control and write those values to a separate txt file. My plan was that since I already had the values printing to terminal as long as the controller stayed wired to my laptop, I could just output those values to a separate file and save them. I tried using “ofstream” in order to do this, but whenever I run the program, no output file is created and even if I manually create one beforehand, nothing is written to it. How would I go about doing this?

Note: I already have #include fstream and using namespace std at the top of my main.cpp
Otherwise, nothing has been touched other than motor ports and user control, both of which give no errors.

Here is the code in usercontrol:

void usercontrol( void ) {
  ofstream output("output.txt", ofstream::out);
  while (1) {
    //Reset motor rotation values

    //Drive Train
    rightdt.spin(vex::directionType::fwd, master.Axis2.value(), velocityUnits::pct);
    leftdt.spin(vex::directionType::fwd, master.Axis3.value(), velocityUnits::pct);

      driveTrain.spin(vex::directionType::fwd, 20, velocityUnits::pct);

      driveTrain.spin(vex::directionType::rev, 20, velocityUnits::pct);
      intake.spin(directionType::fwd, 100, velocityUnits::pct);
    else if(master.ButtonL2.pressing()){
      intake.spin(directionType::rev, 100, velocityUnits::pct);
    //Angle Adjuster
      angleAdj.spin(directionType::fwd, 50, velocityUnits::pct);
    else if(master.ButtonDown.pressing()){
      angleAdj.spin(directionType::rev, 50, velocityUnits::pct);
      lift.spin(directionType::fwd, 100, velocityUnits::pct);
    else if(master.ButtonR2.pressing()){
      lift.spin(directionType::rev, 65, velocityUnits::pct);
    vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources. 

    //Writes change in motor revolution at certain speed to "output.txt"
    output << "rightdt.rotateFor(directionType::fwd, " << rightdt.rotation(rev) << " ,rotationUnits::rev, " << rightdt.value() << ", velocityUnits::pct, false);" << endl; 
    output << "leftdt.rotateFor(directionType::fwd, " << leftdt.rotation(rev) << " ,rotationUnits::rev, " << leftdt.value() << ", velocityUnits::pct, false);" << endl; 
    output << "angleAdj.rotateFor(directionType::fwd, " << angleAdj.rotation(rev) << " ,rotationUnits::rev, " << angleAdj.value() << ", velocityUnits::pct, false);" << endl; 
    output << "intake1.rotateFor(directionType::fwd, " << intake1.rotation(rev) << " ,rotationUnits::rev, " << intake1.value() << ", velocityUnits::pct, false);" << endl; 
    output << "intake2.rotateFor(directionType::fwd, " << intake2.rotation(rev) << " ,rotationUnits::rev, " << intake2.value() << ", velocityUnits::pct, false);" << endl; 
    output << "liftLeft.rotateFor(directionType::fwd, " << liftLeft.rotation(rev) << " ,rotationUnits::rev, " << liftLeft.value() << ", velocityUnits::pct, false);" << endl; 
    output << "liftRight.rotateFor(directionType::fwd, " << liftRight.rotation(rev) << " ,rotationUnits::rev, " << liftRight.value() << ", velocityUnits::pct, true);" << endl; 
    output << "vex::task::sleep(20)" << endl;

    //Comments to space out individual blocks of code
    for(int i = 0; i < 4; i++) {
      output << "//" << endl;

You have an SD Card in the brain ?

try adding a output.flush(); after the last write.

1 Like

I was trying to save to the laptop itself. Would this not be possible? Also, would the output.flush be outside of the while loop (right above output.close)?

you cannot use streams to save to the laptop, only the SD Card.

output.close() will never be reached as you have an infinite while loop.

1 Like

In order to fix that would I just put“output.txt”) and output.close() inside the while loop? What happens when the driver control gets close mid-run? Like when I manually end driver control and the loop isn’t done with a complete cycle. Does that effect this somehow?