Quantcast
Channel: Learn with BlocklyProp — Parallax Forums
Viewing all articles
Browse latest Browse all 293

Learn C Tutorial: SD Minimal.side - Making file available to multiple functions

$
0
0
The sample SD Minimal.side here opens a file to write then opens same file again to read.
At time of write it creates the fp object and opens. For read it re-opens the existing fp object. Write and Read operations are both in one function main().
A function other than main() does not have access to fp. What if the write and read are in 2 functions other than main()?
How to write and place the creation of fp so it is available to any function?
- thanks
/*
  SD Minimal.side
  Create test.txt, write characters in, read back out, display.
*/

#include "simpletools.h"                      // Include simpletools header    

int DO = 22, CLK = 23, DI = 24, CS = 25;      // SD card pins on Propeller BOE

int main(void)                                // main function
{
  sd_mount(DO, CLK, DI, CS);                  // Mount SD card
  FILE* fp = fopen("test.txt", "w");          // Open a file for writing
  fwrite("Testing 123...\n", 1, 15, fp);      // Add contents to the file
  fclose(fp);                                 // Close the file
 
  char s[15];                                 // Buffer for characters
  fp = fopen("test.txt", "r");                // Reopen file for reading
  fread(s, 1, 15, fp);                        // Read 15 characters
  fclose(fp);                                 // Close the file

  print("First 15 chars in test.txt:\n");     // Display heading
  print("%s", s);                             // Display characters
  print("\n");                                // With a newline at the end
}

Viewing all articles
Browse latest Browse all 293

Trending Articles