#include <SPI.h>
#include <SD.h>
#include <LibPrintf.h>
#include <string.h>
//#include <SdFat.h>
File myFile;
char myChar;
// int index = 16;
// int count = 0;
// uint32_t pos;
// char zero = 0;
int intArray[32];
String buffer;
String buff = "date&time&Number$,"; // $=0x24
String buff2 = "%"; // %=0x25
String myData;
int num = 0;
int numm = 0;
String number;
String newBuf;
char terminator1 = 0x24; // ","
char terminator2 = '#';
int fileAv;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
char file1[] = "test.txt";
char file2[] = "vgt.txt";
int loopcount = 0;
int loopCnt =0;
int lpCount = 0;
int wCount = 0;
bool sWitch = 0;
int iNdex = 16;
int posn = 0;
int i;
uint32_t postn;
void loop()
{
while (wCount < 1)
{
if(wCount == 0)
{
// remove old test files
if (SD.exists(file1)) {
Serial.println(" file1 exists.");
SD.remove(file1);
Serial.println(" file1 removed.");
} else if (SD.exists(file2)) {
Serial.println("file2 exists.");
SD.remove(file2);
Serial.println(" file2 removed.");
}
else
{
Serial.println("files do not exist.");
}
}
// create test file and write:
myFile = SD.open( file1, FILE_WRITE );
delay(1000);
//int mySize = myFile.size();
printf("\n Line:%d myFile size: %d \n", __LINE__, myFile.size());
//Serial.print("Writing to file: ");
//Serial.println(buff); // prints buff contents, buff defined as a String class object.
if (myFile)
{
while( num < 10)
{
num++;
//number = String(num);
myData = (buff);
myFile.write(myData.c_str()); // appends each write to the exsiting file, does not overwrite.
delay(1000);
}
}
printf("\n Line: %d writing data: %s \n", __LINE__, myData.c_str());
num = 0;
myFile.close();
delay(1000);
Serial.print("\n reopen to print file: ");
myFile = SD.open(file1, FILE_READ);
printf("\n Line:%d posn: %ld myFAV: %d \n ", __LINE__, myFile.position(), myFile.available());
// posn = 0 on openning, myFAV = exact number of characters.
while (myFile.available())
{
Serial.write(myFile.read());
}
printf("\n Line:%d posn: %ld myFAV: %d \n ", __LINE__, myFile.position(), myFile.available());
myFile.close();
delay(1000);
//re-open the file for reading:
printf("\n\n reopening file for reading \n");
myFile = SD.open(file1, FILE_READ);
printf("\n Line:%d posn: %ld myFAV: %d \n ", __LINE__, myFile.position(), myFile.available());
// when reading the posn starts at 0 but the myFAV is again size of file in bytes. Upon successive reads
// the myFav is decremented till reaches 0, by when posn = eof and myFav = 0;
delay(1000);
if (myFile)
{
Serial.println("reading reopened file");
do
{
//Serial.write(myFile.readStringUntil(terminator1));
//myFile.readBytesUntil();
buffer = myFile.readStringUntil(terminator1);
Serial.println(buffer);
postn = myFile.position();
printf("\n postn: %ld ", postn);
printf("\n buffer = %s ", buffer.c_str());
//Serial.println(myChar);
myChar = buffer.charAt(int(postn-1)); // returns a char const at index
printf("\n char: %c ", myChar);
//Serial.println(myChar);
printf("\n Line:%d pos'n: %ld myFAV: %d \n ", __LINE__, myFile.position(), myFile.available());
// Serial.println(myChar);
//printf("\n myChar = %c \n", myChar);
if( myChar == 0x24 )
{
printf("\n Line: %d found One \n", __LINE__);
//posn = (iNdex + (lpCount*iNdex) );
intArray[numm] = postn; //store location in intArray
printf("\n Line: %d stored-intArray[numm]: %d numm: %d \n", __LINE__, intArray[numm], numm);
numm++;
}
lpCount++;
printf("\n Line: %d myFile.available(): %d \n", __LINE__, myFile.available()); // 163,127,109,73,37,19,0 [i.e increments of 18 [16 plus 2 ["\0"]]
fileAv = myFile.available();
} while ( fileAv != 0);
myFile.close();
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening file");
}
delay(1000);
Serial.println("\n*******************reopen file for writing ");
myFile = SD.open( file1, O_RDWR );
// open for writing myFav starts at 0; but posn remains at eof as it expecst to append only
//printf("\n Line:%d posn: %ld myFile.available(): %d \n", __LINE__, myFile.position(), myFile.available());
//mySize = myFile.size(); // size = 0 at this point.
//printf("\n Line: %d myFile size: %d \n", __LINE__, mySize);
//myChar = 0x30;
// postn = intArray[i];
// myFile.seek(postn);
// myFile.write(buff2.c_str());
// delay(1000);
for(i=0; i<numm; i++)
{
printf("\n Line:%d intArray[i]: %d numm:%d ", __LINE__, intArray[i], numm);
if (intArray[i] >= 0x31) //not equal to zero
{
postn = intArray[i];
myFile.seek(postn);
//printf("\n Line: %d filePos = %ld \n", __LINE__, myFile.position() );
myFile.write(buff2.c_str());
delay(1000);
//myFile.seekSet(intArray[i]);
//printf("\n Line: %d filePos = %ld \n", __LINE__, myFile.position());
//printf("\n Line: %d stored-arrayPos: %d i: %d \n",__LINE__, intArray[i], i);
//Serial.print("intArray[i]:");
//Serial.println(intArray[i]);
//printf("\n filePos = %ld \n", myFile.position());
}
} // end of for loop
myFile.flush(); // forces flush of sd buffer to disk permanently, a committ
printf("\n Line: %d filePos = %ld \n", __LINE__, myFile.position());
myFile.close();
wCount++;
}// end of while loop
if(sWitch == false){
myFile = SD.open(file1, FILE_READ);
Serial.print("printing file: ");
while (myFile.available())
{
Serial.write(myFile.read());
}
myFile.close();
delay(1000);
sWitch = true;
}
}