beautypg.com

Fseek(), Fwrite() – Echelon i.LON SmartServer 2.0 User Manual

Page 249

background image

i.LON SmartServer 2.0 Programming Tools User’s Guide

235

fclose(pFile);

}
}

fseek()

You can use the fseek()method to set the position indicator associated with the a file stream to a
new position.

SYNTAX

int

fseek (FILE * stream, long int offset, int origin);

The stream parameter specifies a pointer to a FILE object

that identifies the stream.

The offset parameter specifies the number of bytes to be offset from origin.

The origin parameter specifies the position from where offset is to be added. You can specify
the origin using one of the following three constants:

SEEK_SET

Beginning of the file

SEEK_CUR

Current position of the file pointer

SEEK_END

End of the file

If this position indicator has been moved successfully, this method returns a zero value.
Otherwise, it returns a non-zero value.

EXAMPLE

The following example demonstrates an fseek()method that moves the position indicator two
bytes from the beginning of an existing file.

FILE

* pFile;

fseek

(pFile , 2 , SEEK_SET);

fwrite()

You can use the fwrite()method to write an array of elements from a block of memory on the
SmartServer to the current position in a file stream.

SYNTAX

size_t

fwrite (const void * ptr, size_t size, size_t count, FILE *

stream);

The ptr parameter specifies the array of elements to be written to the file stream.

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

.

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

.

The stream parameter specifies a pointer to a FILE object

that specifies an output stream.

This method returns a size_t object, which is an integral data type that specifies the total
number of elements successfully written. If this number differs from count, an error has occurred.

EXAMPLE

The following example demonstrates an fwrite()method that writes some data to an existing
text file.

FILE *pFile;
char Filename[] = "/web/user/mUartTxFile.txt";
char buf[] = “Test String”; // output buffer
long len = 11;