Download Data Structures in C

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Asynchronous I/O wikipedia , lookup

Disk formatting wikipedia , lookup

Design of the FAT file system wikipedia , lookup

Lustre (file system) wikipedia , lookup

File system wikipedia , lookup

File Allocation Table wikipedia , lookup

XFS wikipedia , lookup

Computer file wikipedia , lookup

Files-11 wikipedia , lookup

File locking wikipedia , lookup

Transcript
Data Structures in C
struct and typedef
struct
• Allows you to define a data structure
composed of pre-defined types.
• struct may include other structures.
• struct’s may refer to themselves.
• struct defines a template for the data
structure.
Using Structs
• Structs are generally globally defined.
(Before the main function.)
• Syntax –
struct <name>
{
<datatype> <name>;
<datatype> <name>;
};
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.
Declaring Variables with struct.
struct record
{
char name[40];
int age;
};
void main(void)
{
struct record rec;
struct record *recref;
…
printf(“Name: %s, Age: %d\n”, rec.name,
rec.age);
printf(“Name:%s, Age: %d\n, recref->name,
recref->age);
}
typedef
Adds a synonym for a data type.
typedef struct
{
char name[40];
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.
typedef(cont.)
To rename a datatype.
typedef <datatype> <newname>;
If color is represented by an integer value it might be
useful to define:
typedef int color;
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.
fopen()
• Syntax
void main(void)
{
FILE *fp;
fp = fopen( “filename.ext”, “r”);
// Make sure to close your file after processing
fclose(fp);
}
File handling
• FILE *fopen( char *<name>,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.
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.
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);
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);
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.