Angel David Yaguana Hernandez / Mbed 2 deprecated CPP_fread

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 
00004 Serial          pc(USBTX, USBRX);
00005 LocalFileSystem chip("uc");
00006 SDFileSystem    sd(p5, p6, p7, p8,"sd");
00007 
00008 int main () {
00009     pc.baud(115200);
00010     FILE * pFile;
00011     long lSize;
00012     char * buffer;
00013     size_t result;
00014 
00015     pFile = fopen ( "/sd/Justin.mp3","r");
00016     if (pFile==NULL) {
00017         fputs ("File error",stderr);
00018         exit (1);
00019     }
00020     // obtain file size:
00021     fseek (pFile , 0 , SEEK_END);
00022     lSize = ftell (pFile);
00023     rewind (pFile);
00024     pc.printf("Tamanio: %d\n",lSize);
00025     wait(3);
00026     // allocate memory to contain the whole file:
00027     buffer = (char*) malloc (sizeof(char)*lSize);
00028     if (buffer == NULL) {
00029         fputs ("Memory error",stderr);
00030         exit (2);
00031     }
00032     // copy the file into the buffer:
00033     result = fread (buffer,1,lSize,pFile);
00034     if (result != lSize) {
00035         fputs ("Reading error",stderr);
00036         exit (3);
00037     }
00038     pc.printf("%s",buffer);
00039     /* the whole file is now loaded in the memory buffer. */
00040     fclose (pFile);
00041     free (buffer);
00042     return 0;
00043 }
00044 
00045 //This code loads myfile.bin into a dynamically allocated memory buffer,
00046 //which can be used to manipulate the content of a file as an array.
00047 
00048 
00049 /*
00050 function: fread
00051 Library:  <cstdio>
00052 
00053 size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
00054 
00055 Read block of data from stream
00056 Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.
00057 The postion indicator of the stream is advanced by the total amount of bytes read.
00058 The total amount of bytes read if successful is (size * count).
00059 
00060 Parameters
00061 
00062 ptr         Pointer to a block of memory with a minimum size of (size*count) bytes.
00063 size        Size in bytes of each element to be read.
00064 count       Number of elements, each one with a size of size bytes.
00065 stream      Pointer to a FILE object that specifies an input stream.
00066 
00067 
00068 Return Value
00069 The total number of elements successfully read is returned as a size_t object, which is an integral data type.
00070 If this number differs from the count parameter, either an error occured or the End Of File was reached.
00071 You can use either ferror or feof to check whether an error happened or the End-of-File was reached.
00072 */
00073 
00074