beautypg.com

Fread() – Echelon i.LON SmartServer 2.0 User Manual

Page 248

background image

234

Appendix A - Programmer’s Reference

EXAMPLE

The following example demonstrates an fopen()method that creates a new text file for writing
and assigns it to a file stream.

FILE * pFile;

pFile = fopen ("myfile.txt","w");

fread()

You can use the fread()method to read a block of data from a file stream. This method reads an
array of elements from the file stream and stores the data in a block of memory on the SmartServer.

Note: The SmartServer does not have memory protection. As a result, an FPM can write to any
memory area on the SmartServer, which could cause the SmartServer to fail or corrupt the data of
another embedded applications on the SmartServer. Therefore, you should use the greatest possible
care when implementing your freely programmable modules.

SYNTAX

size_t fread (void * ptr, size_t size, size_t count, FILE *
stream);

The ptr parameter specifies the block of memory to be used to store the data read from the file
stream with a minimum size of (size*count) bytes.

The size parameter specifies the size (in bytes) of each element to be read

.

The count parameter specifies the number of elements to be read, each one with a size of size
bytes

.

The stream parameter specifies a pointer to a FILE object

that specifies an input stream.

This method returns a size_t object, which is an integral data type that specifies the total
number of elements successfully read. If this number differs from count, either an error occurred
or the internal End-of-File internal indicator

was reached.

EXAMPLE

The following example demonstrates an fread()method.

FILE *pFile;
int iTemp;
long FilePtr = 0; // starting index of file
char buf[200] ; // input buffer
char len = 200;
char Filename[] = "/web/user/mUartTxFile.txt";

if((pFile = fopen(Filename,"r")) == NULL)
{

printf("Can't open i.LON file = %s \n", Filename);

}
else
{

iTemp = fseek(pFile, FilePtr, SEEK_SET);

if(iTemp)
{
printf("FSEEK

failed\n");

}

else

{

iTemp = fread(buf, 1, len ,pFile);