Reading PHAT Files

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware
Comments
Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Ethernut 3.0 E Compiler Error OK
Binaries
NO
EIR 1.0 C Set jumper JP1
to DEBUG mode.
Compiler Error OK
Binaries
OK
Binaries
AT91SAM7X-EK NT NT
AT91SAM9260-EK NT NT
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Description

This example demonstrates how to reada file with the PHAT file system from a MMC (SD) card.

Note, that this example is consecutive to Writing PHAT Files. We will read a file, previously written by that example.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <dev/nplmmc.h>
  3. include <fs/phatfs.h>
  4. include <stdio.h>
  5. include <io.h>
  6. include <fcntl.h>
  7. include <errno.h>

char buff[256];

int main (void) {

   int hvol;
   FILE *fp;

   unsigned long baud = 115200;

   NutRegisterDevice(&DEV_DEBUG, 0, 0);

   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

   puts("File read test\n");

   if (NutRegisterDevice(&devPhat0, 0, 0)){
   	printf("PHAT file system error\n");
   }

   if (NutRegisterDevice(&DEV_MMCARD, 0, 0)){
   	printf("MMC block device error\n");
   }

   hvol = _open("MMC0:1/PHAT0", _O_RDWR | _O_BINARY);

   if (hvol == -1) {
       printf("Mount error %d\n", errno);
   }

   fp = fopen("PHAT0:/TEST.TXT", "r");
   fscanf(fp, "%s", &buff[0]);
   puts(buff);

   fclose(fp);
   _close(hvol);

   for(;;);

} </source>

Details

<source lang="c"> char buff[256]; </source>

defines an array of the type char of 256 bytes. An char array is also called string.

<source lang="c"> unsigned long baud = 115200;

NutRegisterDevice(&DEV_DEBUG, 0, 0);

freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

puts("File read test\n"); </source>

After setting our stout and printing out a banner,

<source lang="c"> if (NutRegisterDevice(&devPhat0, 0, 0)){

   printf("PHAT file system error\n");

} </source>

registers the PHAT file system and prints out an error message, if an error occured.

<source lang="c"> if (NutRegisterDevice(&DEV_MMCARD, 0, 0)){

   	printf("MMC block device error\n");
   }

</source>

registers the MMC block device and prints out an error message, if an error occured.

<source lang="c"> hvol = _open("MMC0:1/PHAT0", _O_RDWR | _O_BINARY); </source>

mounts a PHAT partition called "PHAT 0".

<source lang="c"> if (hvol == -1) {

   	printf("Mount error %d\n", errno);
   }

</source>

prints out an error message, if an error occured mounting the partition.

<source lang="c"> fp = fopen("PHAT0:/test.txt", "r"); </source>

opens a file "test.txt" and prepares it to be read. Now the pointer fp points to that text file.

<source lang="c"> fscanf(fp, "%s", &buff[0]); </source>

reads the string (&s) in the file, fp points to, and puts it into the "buff" array.

<source lang="c"> puts(buff); </source>

puts out the content of buff, namely the string read from the text file.

<source lang="c"> fclose(fp); _close(hvol); </source>

Finally, we close the file and unmount the partition.

Output

File read test
HelloFile


See also