access_file.pp 819 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. program access_file;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9, fat;
  5. var
  6. i: integer;
  7. size: cuint32;
  8. text: string;
  9. handle: P_FILE;
  10. begin
  11. consoleDemoInit();
  12. printf('fatInit()...');
  13. if (fatInitDefault()) then
  14. begin
  15. printf(#9 + 'Success' + #10);
  16. handle := fopen('/test1.txt', 'r');
  17. if handle = nil then
  18. begin
  19. printf('Cannot open file' + #10);
  20. end else
  21. begin
  22. fseek(handle, 0, SEEK_END); // Go to end of file
  23. size := ftell(handle); // Get current position in file, because it is the end it will be the size
  24. fseek(handle, 0, SEEK_SET); // Go to begining of file
  25. fread(@text, size, 1, handle); // Read all of file into memory
  26. printf(@text);
  27. fclose(handle); // Close file
  28. end;
  29. end else
  30. printf(#9 + 'Failure' + #10);
  31. while true do;
  32. end.