sdin.eof() reading CSV error..

Hello,

With my DUE I am recieving encoder positions and writing the to an SD card via .csv. Each entry contains 2 ints and 2 floats with commas and no spaces.

Now, I wish to read the created file with the Arduino and just display them through the Serial communication. I am getting an error at:

if (!sdin.eof()) error("readFile")

It seems like the created file is not ending with an EOF which is triggering this error.

The code is large, so I will post the interface between writing and reading.

Writing

void loop()
{
  err = slaveEncoder - masterEncoder;
  analogWrite(EnA, setPWM(err)); // Write PWM value as the encoder count of the Master (controller)
  
  if (recordState  == true){
    int m;
    m = millis();
  
    if (m % LOG_INTERVAL == 0) {

      // use buffer stream to format line
      obufstream bout(buf, sizeof(buf));  
     
      bout << m << ',' << ID << ',' << masterEncoder << ',' << slaveEncoder << endl;
      ID++;

    #if ECHO_TO_SERIAL
      cout << buf;
    #endif  // ECHO_TO_SERIAL

      // log data and flush to SD
      logfile << buf << flush;
      
      // check for error
      if (!logfile) error("!!WDF!!"); //Write Data Fail
      
      // don't log two points in the same millis
      if (m == millis()) delay(1);
    }
  }  
  
  if (Serial.available() > 0){
    command = Serial.read();
  if (command == 82){       //R - Record
    cout << pstr("Recording...") << endl;
    recordState = true;
    return;
  }
  else if (command == 84){  //T - Traverse
    cout << pstr("Stopped...") << endl;
    logfile.close();
    readFile();
    recordState = false;
    return;
  }
  else if (command == 83){  //S - Stop
    cout << pstr("Stopped...") << endl;
    logfile.close();
    recordState = false;
    return;
  }
  else if (command == 88){  //X - Exit
    logfile.close();
    cout << pstr("Done!");
    while (1);
  }
  else return; 
  }
}

Reading

void readFile() {
  int read_millis;
  int read_ID;
  float read_ME, read_SE;
  char comma1, comma2, comma3;
  
  // open input file
  ifstream sdin(fileName);
  
  // check for open error
  if (!sdin.is_open()) error("open");
  
  // read until input fails
  while (sdin >> read_millis >> comma1 >> read_ID >> comma2  >> read_ME >> comma3 >> read_SE) {
    
    // error in line if not commas
    if (comma1 != ',' || comma2 != ',' || comma3 != ',') error("comma");
    
    // print in six character wide columns
    cout << read_millis << setw(6) << read_ID << setw(6) << read_ME << setw(6) << read_SE << endl;
  }
  
  // Error in an input line if file is not at EOF.
  if (!sdin.eof()) error("readFile");
}

If anyone can suggest a reason why I cannot read the file properly, I would really appreciate a tip :slight_smile:

Daniel

I am getting an error at:

What error?

      // log data and flush to SD
      logfile << buf << flush;
      
      // check for error
      if (!logfile) error("!!WDF!!"); //Write Data Fail

Make sure that you can write to the file, after you write to it. Hmmm.

If you remove the SD card from the Arduino, and open the file on the PC, is there an incomplete line in the file, or a blank line?

How much of the file is read/not read before the error happens?

Ah! Stupid me, I appologize for the question... I also have a write in the setup() as a header. I forget the my reading function only takes care of 2 ints and 2 floats in sequence with commas.

Thank you for leading me to solve my issue.

Daniel