0% found this document useful (0 votes)
16 views

Files and Data Structures

Files can be opened and closed using fopen() and fclose(). Binary files store data using fixed precision like int16 for 2 bytes. File writing uses fwrite() to write variables to files in binary format. File reading uses fread() to read binary data into variables. Text files use fprintf() for writing with formats and fscanf() for reading with formats. Feof() tests for end-of-file. Data structures include structs for non-homogeneous data and cell arrays to store differently sized elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Files and Data Structures

Files can be opened and closed using fopen() and fclose(). Binary files store data using fixed precision like int16 for 2 bytes. File writing uses fwrite() to write variables to files in binary format. File reading uses fread() to read binary data into variables. Text files use fprintf() for writing with formats and fscanf() for reading with formats. Feof() tests for end-of-file. Data structures include structs for non-homogeneous data and cell arrays to store differently sized elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FILES AND DATA STRUCTURES

OPENING A FILE (fopen)


fd = fopen (filename, permissions)

- Filename = string woth the file name


- Permissions = required access rights

fid if no error

-1 = error and cannot be opened

CLOSING A FILE (fclose)


e = fclose (fd)

- Fd = file descriptor returned by a previous fopen.


- O if no error | -1 an arreo has occurred

BINARY FILES
File size (precision)

- int16 → 2 bytes - single → 4 bytes

- int32 → 4 bytes - float → 8 bytes

FILE WRITING USING fwrite()

count=fwrite(fd,var,precision)

Writes the elements of the variable var to the specified file fd , translating the value to the
specified precision (optional).

Return the number of bytes that fwrite effectively wrote.


BINARY FORMATS

FILE READING USING fread ()

var= fread (fd,size,precision)

It reads the number of elements specified by size in the binary format specified by precision. The
read value is stored in var.

size: Number of elements to be read. Valid entries are:

- N: read N elements into a column vector.


- inf: read to the end of the file.
- [M,N]: read elements to fill an M-by-N matrix, in column order. N can be inf, but M can't.

TEXT FILES
FILE WRITING USING fprintf ()

Count = fprintf(fd,format,var1,…)

It applies the format to all elements of var1 (IN TEXT FORM) and any additional arguments and
writes the data to a text file.

FORMAT QUALIFIERS
FILE READING USING fscan ()

[var,count]=fscanf(fd,format,size)

Return value:

Var: the variable where the file read is stored.

Count: is an optional output argument that returns the number of elements successfully read.

FUNCTION feof ()

st=feof(fd)

Test for end-of-file. It returns 1 if the end-of-file indicator for the file with file identifier fd has
been set, and 0 otherwise. The end-of-file indicator is set when a read operation on the file
associated with the FID attempts to read past the end of the file.

DATA STRUCTURES
STRUCTS: they are compound of data types structured in
field → used to store non-homogeneous data types.

New fields can be created using assignmetns and DELETED


USING rmfield (students, ‘Others’).

ARRAYS OF STRUCTS: each element of an array can be an struct and can be accesses as follows:

And variables can be created:

CELL ARRAYS:

Problem with arrays : arrays of elements of different sizes → SOLUTION: using cell arrays, they are
defined using {}:

You might also like