Reading & Printing from SD with Activity Board
John Kauffman
Posts: 653
Propeller Activity Board / C / SImpleIDE
I have code below which writes three data to the SD card. Each write is an independent open,write,close. Then contents of file read from file on SD card.
I know the writes work because I can move the SD to my PC and open with notepad and see three records.
But the read&print part only shows the first record. Perhaps adding \n to data string is problem when print?(although seemed to work on other projects)
Anyone see the flaw? Thanks.
<code>
/* logger to SD*/
#include "simpletools.h" // Include simple tools
int DO = 22, CLK = 23, DI = 24, CS = 25; // SD card pins on Propeller BOE
char FileName[10] = "Test25.txt"; // create char array with filename string
int RecordSize = 13; //#bytes per record. Comma,return and slash are one each
// record format: Recxx,xx,xx\n = 13
int RecordCount = 0;
int main(void)
{
sd_mount(DO, CLK, DI, CS); // Mount SD card
// create file + open + add record
FILE* fp = fopen(FileName, "a"); // Open a file for append
sleep(1);
//print("file open 1st time\n"); //diagnostic
fwrite("Rec01,12,34\n", 1, RecordSize, fp); // Add contents to the file
fclose(fp); // Close the file
RecordCount++;
// open + add record (2nd record)
fopen(FileName, "a"); // Open a file for append
//print("file open 2nd time\n"); //diagnostic
sleep(1);
fwrite("Rec02,56,78\n", 1, RecordSize, fp); // Add contents to the file
fclose(fp); // Close the file
RecordCount++;
// open + add record (3rd record)
fopen(FileName, "a"); // Open a file for append
//print("file open 3rd time\n"); //diagnostic
sleep(1);
fwrite("Rec03,90,12\n", 1, RecordSize, fp); // Add contents to the file
fclose(fp); // Close the file
RecordCount++;
// read whole file into a char array (string)
char s[RecordCount*RecordSize]; // Buffer for characters - size caculated
fp = fopen(FileName, "r"); // Reopen file for reading
fread(s, 1, RecordCount*RecordSize, fp); // Read characters into array "s"
fclose(fp); // Close the file
// print the string
printf("From %s\ with %i records\n",FileName,RecordCount); // Display heading
print("%s", s); // Display characters
print("done\n"); // With a newline at the end
}
</code>
I have code below which writes three data to the SD card. Each write is an independent open,write,close. Then contents of file read from file on SD card.
I know the writes work because I can move the SD to my PC and open with notepad and see three records.
But the read&print part only shows the first record. Perhaps adding \n to data string is problem when print?(although seemed to work on other projects)
Anyone see the flaw? Thanks.
<code>
/* logger to SD*/
#include "simpletools.h" // Include simple tools
int DO = 22, CLK = 23, DI = 24, CS = 25; // SD card pins on Propeller BOE
char FileName[10] = "Test25.txt"; // create char array with filename string
int RecordSize = 13; //#bytes per record. Comma,return and slash are one each
// record format: Recxx,xx,xx\n = 13
int RecordCount = 0;
int main(void)
{
sd_mount(DO, CLK, DI, CS); // Mount SD card
// create file + open + add record
FILE* fp = fopen(FileName, "a"); // Open a file for append
sleep(1);
//print("file open 1st time\n"); //diagnostic
fwrite("Rec01,12,34\n", 1, RecordSize, fp); // Add contents to the file
fclose(fp); // Close the file
RecordCount++;
// open + add record (2nd record)
fopen(FileName, "a"); // Open a file for append
//print("file open 2nd time\n"); //diagnostic
sleep(1);
fwrite("Rec02,56,78\n", 1, RecordSize, fp); // Add contents to the file
fclose(fp); // Close the file
RecordCount++;
// open + add record (3rd record)
fopen(FileName, "a"); // Open a file for append
//print("file open 3rd time\n"); //diagnostic
sleep(1);
fwrite("Rec03,90,12\n", 1, RecordSize, fp); // Add contents to the file
fclose(fp); // Close the file
RecordCount++;
// read whole file into a char array (string)
char s[RecordCount*RecordSize]; // Buffer for characters - size caculated
fp = fopen(FileName, "r"); // Reopen file for reading
fread(s, 1, RecordCount*RecordSize, fp); // Read characters into array "s"
fclose(fp); // Close the file
// print the string
printf("From %s\ with %i records\n",FileName,RecordCount); // Display heading
print("%s", s); // Display characters
print("done\n"); // With a newline at the end
}
</code>
Comments
Or you could stick with 13 characters but then you have to print each record individually (a two dimensional array may come in handy here).
Please use [noparse] [/noparse] instead of <code> ...
But there is a spurious character printing on the last line before the string "done" I think it is ASCII 195 ( T rotated -90 degrees).
Here is what I am picturing if it was 2 recs by 8 btes each (a little briefer example).
The data in the file would be "Rec1,11\nRec2,22\n" with \n replaced by the return character
Since the fread() is fetching exactly 16 characters then wouldn't it get exactly the above string?
Then the array dimmed s[16] would hold all 16 chars in the file and no more.
What is the role of a terminal zero?
Here is how I tried adding a terminal zero in code with two lines ***
The result is worse: three spurious characters on last row before "done"
Thanks for explaining the theory behind this.
You want either '\0' or simply 0 here. And - being 0 indexed - that should read s[(RecordCount*RecordSize)] = 0 (but allocation still needs to be +1).
Here is final code for future forum searchers.