Read/write to memory and SD card

I have a text file to store all of the relevant messages used during runtime. I created a file on the SD card and run a routine in setup to read the file into flash memory. It is designed to take individual lines of text less than 63 characters and store them in sequential blocks of 64 bytes each. It then takes the character count (plus the null) and stores that in a byte whose address is offset by 0x10000 from the original data. I just set the starting address and open the file before calling the routine. The code looks like this:

void loadtextfile() {
  int n;
  char line[64];
  File rdfile;
  rdfile = SD.open(textfile, FILE_READ);
  if(!rdfile) {
    error("File not opened");
  }
  else {
    while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
      if (line[n - 1] == '\n') {
        cout << line << F("Line length = ") << dec << n;
      } 
      else {
        cout << line << F("\nLine length = ") << dec << n << endl;
      }
      flash.writeCharArray(address3, line, n);
      flash.writeByte((address3 + 0x10000), n);
      n = flash.readByte(address3 + 0x10000);
      flash.readCharArray(address3, line, n);
      cout << F("  Address = ") << hex << address3  << F("  Stored text: ") << line << endl;
      address3 += 0x40;
    }  
  }
}

The second half of this is of course designed to read the data into a char array and then use cout to print it to serial. I just set the printindex pointer to the line I want printed first and call the routine. That code looks like this:

void memprint() {
  int n;
  char line[64];
  address3 = 0x40000 + (0x40 * printindex);
  n = flash.readByte(address3 + 0x10000);
  flash.readCharArray(address3, line, n);
  cout << F("Line length = ") << dec << n;
  cout << F("  Address = ") << hex << address3  << F("  Stored text: ") << line << endl;
}

Right now it has lines of code I am using to debug it with. When I run it initially I get the line read from the file printedperfectly and then after it is stored to memory I read it back and print the memory location, length and the text itself. This part works perfectly. When I run a for loop to increment the printindex pointer to examine the whole enchilada, it (sometimes) prints spurious characters on the following line like little squares, something that looks like a capital “A” only with a hat on it, small f’s, capital Y, and capital Z. Not every time but enough, you get the picture. I’ve experimented with trailing spaces maybe causing this and eliminated them from the text file with minor improvement but not a complete fix. What has me baffled is that I’m cutting and pasting code that works in the 1st subroutine into the second where it fails.

Please excuse the post if it doesn’t display correctly as this is my 1st time using the “code” button. I also attached a txt file of what it prints out. Top half is from the transfer code and the last half from the print from memory routine. Also please don’t laugh too much if my code looks like I taught myself C++.

What it prints out.txt (21.2 KB)

OK, I’m sorry but I figured it out. I inserted a single line memset(line, 0, sizeof(line); just before the command to read the memory address into the buffer. It dawned on me the variable was being initialized in the subroutine but unlike the fgets command I wasn’t flushing the garbage out first.