Data Structures in C - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Data Structures in C

Description:

struct defines a template for the data structure. Using Structs ... (so little hope for advancement ) Another way to read and write files. fread() and fwrite ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 16
Provided by: MikeH5
Category:
Tags: and | data | hope | structures

less

Transcript and Presenter's Notes

Title: Data Structures in C


1
Data Structures in C
  • struct and typedef

2
struct
  • Allows you to define a data structure composed of
    pre-defined types.
  • struct may include other structures.
  • structs may refer to themselves.
  • struct defines a template for the data structure.

3
Using Structs
  • Structs are generally globally defined. (Before
    the main function.)
  • Syntax
  • struct ltnamegt
  • ltdatatypegt ltnamegt
  • ltdatatypegt ltnamegt

4
Declaring Variables with Struct
  • Variables follow normal naming rules.
  • A struct is not recognized as a data type by
    default.
  • Structs may be made into datatypes using the
    typedef statement.

5
Declaring Variables with struct.
  • struct record
  • char name40
  • int age
  • void main(void)
  • struct record rec
  • struct record recref
  • printf(Name s, Age d\n, rec.name, rec.age)
  • printf(Names, Age d\n, recref-gtname,
    recref-gtage)

6
typedef
  • Adds a synonym for a data type.
  • typedef struct
  • char name40
  • int age
  • record
  • Defines a synonym record for the structure.
  • This allows you to define code like.
  • record rec
  • Which will allocate space for a record structure
    named rec.
  • typedef may also be used to rename internal data
    types.

7
typedef(cont.)
  • To rename a datatype.
  • typedef ltdatatypegt ltnewnamegt
  • If color is represented by an integer value it
    might be useful to define
  • typedef int color

8
File Handling
  • All file handling is based on the FILE
    datatype.
  • FILE is referred to a a file pointer.
  • FILE associates the information in a file with
    the data on disk.
  • To get a file pointer you must first open the
    file.
  • The fopen function returns a FILE for use in
    your program.

9
fopen()
  • Syntax
  • void main(void)
  • FILE fp
  • fp fopen( filename.ext, r)
  • // Make sure to close your file after processing
  • fclose(fp)

10
File handling
  • FILE fopen( char ltnamegt,char mode)
  • name is a string containing the name of the file
  • mode is a string containing one of the following
  • r - file is opened to read file must exist.
  • w file is opened for writing file destroyed.
  • r file is opened for read and write file
    must exist
  • w file opened for read and write file
    destroyed.
  • u file opened for update file may be read
    writing occurs at the end of the file.

11
File handling continued.
  • In addition to these modes a file may be opened
    either as a binary or text file.
  • Text files may only contain ASCII data.
  • Binary files contain images of the data.
  • Files are opened as text by adding a t to the
    open mode.
  • Files are opened as binary by adding a b to the
    open mode.

12
File handling continued again
  • Once the file is opened you need to read from it
    or write to it.
  • We can use formatted input and output for this
    purpose.
  • Standard output file stdout
  • printf( char format, var, ,varn)
  • Standard input file stdin
  • scanf( char format, var, ,varn)

13
File handling yet again(so little hope for
advancement )
  • Functions to deal with specific files.
  • fscanf( FILE , char format, var, , varn)
  • fprintf( FILE , char format, var, , varn)

14
File handling yet again(so little hope for
advancement )
  • Another way to read and write files.
  • fread() and fwrite()
  • fread( void buffer,int size, int count, FILE
    )
  • fwrite( void buffer, int size, int count, FILE
    )
  • Use sizeof() to calculate size.
  • Usually use 1 for the count unless you have a
    specific need.

15
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com