Angel David Yaguana Hernandez
/
CPP_fread
fread, descripcion y ejemplo
main.cpp@0:2191a269e668, 2012-04-03 (annotated)
- Committer:
- sherckuith
- Date:
- Tue Apr 03 21:01:22 2012 +0000
- Revision:
- 0:2191a269e668
descripcion y ejemplo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sherckuith | 0:2191a269e668 | 1 | #include "mbed.h" |
sherckuith | 0:2191a269e668 | 2 | #include "SDFileSystem.h" |
sherckuith | 0:2191a269e668 | 3 | |
sherckuith | 0:2191a269e668 | 4 | Serial pc(USBTX, USBRX); |
sherckuith | 0:2191a269e668 | 5 | LocalFileSystem chip("uc"); |
sherckuith | 0:2191a269e668 | 6 | SDFileSystem sd(p5, p6, p7, p8,"sd"); |
sherckuith | 0:2191a269e668 | 7 | |
sherckuith | 0:2191a269e668 | 8 | int main () { |
sherckuith | 0:2191a269e668 | 9 | pc.baud(115200); |
sherckuith | 0:2191a269e668 | 10 | FILE * pFile; |
sherckuith | 0:2191a269e668 | 11 | long lSize; |
sherckuith | 0:2191a269e668 | 12 | char * buffer; |
sherckuith | 0:2191a269e668 | 13 | size_t result; |
sherckuith | 0:2191a269e668 | 14 | |
sherckuith | 0:2191a269e668 | 15 | pFile = fopen ( "/sd/Justin.mp3","r"); |
sherckuith | 0:2191a269e668 | 16 | if (pFile==NULL) { |
sherckuith | 0:2191a269e668 | 17 | fputs ("File error",stderr); |
sherckuith | 0:2191a269e668 | 18 | exit (1); |
sherckuith | 0:2191a269e668 | 19 | } |
sherckuith | 0:2191a269e668 | 20 | // obtain file size: |
sherckuith | 0:2191a269e668 | 21 | fseek (pFile , 0 , SEEK_END); |
sherckuith | 0:2191a269e668 | 22 | lSize = ftell (pFile); |
sherckuith | 0:2191a269e668 | 23 | rewind (pFile); |
sherckuith | 0:2191a269e668 | 24 | pc.printf("Tamanio: %d\n",lSize); |
sherckuith | 0:2191a269e668 | 25 | wait(3); |
sherckuith | 0:2191a269e668 | 26 | // allocate memory to contain the whole file: |
sherckuith | 0:2191a269e668 | 27 | buffer = (char*) malloc (sizeof(char)*lSize); |
sherckuith | 0:2191a269e668 | 28 | if (buffer == NULL) { |
sherckuith | 0:2191a269e668 | 29 | fputs ("Memory error",stderr); |
sherckuith | 0:2191a269e668 | 30 | exit (2); |
sherckuith | 0:2191a269e668 | 31 | } |
sherckuith | 0:2191a269e668 | 32 | // copy the file into the buffer: |
sherckuith | 0:2191a269e668 | 33 | result = fread (buffer,1,lSize,pFile); |
sherckuith | 0:2191a269e668 | 34 | if (result != lSize) { |
sherckuith | 0:2191a269e668 | 35 | fputs ("Reading error",stderr); |
sherckuith | 0:2191a269e668 | 36 | exit (3); |
sherckuith | 0:2191a269e668 | 37 | } |
sherckuith | 0:2191a269e668 | 38 | pc.printf("%s",buffer); |
sherckuith | 0:2191a269e668 | 39 | /* the whole file is now loaded in the memory buffer. */ |
sherckuith | 0:2191a269e668 | 40 | fclose (pFile); |
sherckuith | 0:2191a269e668 | 41 | free (buffer); |
sherckuith | 0:2191a269e668 | 42 | return 0; |
sherckuith | 0:2191a269e668 | 43 | } |
sherckuith | 0:2191a269e668 | 44 | |
sherckuith | 0:2191a269e668 | 45 | //This code loads myfile.bin into a dynamically allocated memory buffer, |
sherckuith | 0:2191a269e668 | 46 | //which can be used to manipulate the content of a file as an array. |
sherckuith | 0:2191a269e668 | 47 | |
sherckuith | 0:2191a269e668 | 48 | |
sherckuith | 0:2191a269e668 | 49 | /* |
sherckuith | 0:2191a269e668 | 50 | function: fread |
sherckuith | 0:2191a269e668 | 51 | Library: <cstdio> |
sherckuith | 0:2191a269e668 | 52 | |
sherckuith | 0:2191a269e668 | 53 | size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ); |
sherckuith | 0:2191a269e668 | 54 | |
sherckuith | 0:2191a269e668 | 55 | Read block of data from stream |
sherckuith | 0:2191a269e668 | 56 | 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. |
sherckuith | 0:2191a269e668 | 57 | The postion indicator of the stream is advanced by the total amount of bytes read. |
sherckuith | 0:2191a269e668 | 58 | The total amount of bytes read if successful is (size * count). |
sherckuith | 0:2191a269e668 | 59 | |
sherckuith | 0:2191a269e668 | 60 | Parameters |
sherckuith | 0:2191a269e668 | 61 | |
sherckuith | 0:2191a269e668 | 62 | ptr Pointer to a block of memory with a minimum size of (size*count) bytes. |
sherckuith | 0:2191a269e668 | 63 | size Size in bytes of each element to be read. |
sherckuith | 0:2191a269e668 | 64 | count Number of elements, each one with a size of size bytes. |
sherckuith | 0:2191a269e668 | 65 | stream Pointer to a FILE object that specifies an input stream. |
sherckuith | 0:2191a269e668 | 66 | |
sherckuith | 0:2191a269e668 | 67 | |
sherckuith | 0:2191a269e668 | 68 | Return Value |
sherckuith | 0:2191a269e668 | 69 | The total number of elements successfully read is returned as a size_t object, which is an integral data type. |
sherckuith | 0:2191a269e668 | 70 | If this number differs from the count parameter, either an error occured or the End Of File was reached. |
sherckuith | 0:2191a269e668 | 71 | You can use either ferror or feof to check whether an error happened or the End-of-File was reached. |
sherckuith | 0:2191a269e668 | 72 | */ |
sherckuith | 0:2191a269e668 | 73 | |
sherckuith | 0:2191a269e668 | 74 |