cmpt130_09files

cmpt130_09files - File I/O File I/O Types of files Command...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
File I/O
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File I/O Types of files Command line arguments File input and output functions Binary files Random access
Background image of page 2
Introduction
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Introduction Data stored in main memory does not persist Most programs require that the user be able to store and retrieve data from previous sessions In persistent storage, such as a hard disk There are many forms of persistent storage Which suggests that the low level processes for accessing them is different
Background image of page 4
Reading Files Files have formats A set of rules that determine the meaning of its contents To read a file Know (or find) its name Open it for reading Read in the data in the file Close it
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Files A file represents a section of storage Files are viewed as contiguous sequences of bytes which can be read individually In reality a file may not be stored sequentially and may not be read one byte at a time Such details are the responsibility of the OS C has two ways to view files Text
Background image of page 6
Text and Binary Files There is a distinction between text files and binary files Text files store all data as text whereas binary files store the underlying binary representation In addition C allows for both text and binary views of files Usually the binary view is used with binary files
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Low Level I/O In addition to types of files and views of files, C has a choice of I/O levels Low level I/O uses the fundamental I/O given by the OS Standard high level I/O uses a standard package of library functions ANSI C only supports standard I/O since all OS I/O cannot be represented by one low level model
Background image of page 8
Text Files
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Standard Files C automatically assigns input and output to standard files for some I/O functions e.g. getchar() , gets() , scanf() , printf() , puts() There are three standard files for I/O Standard input is set to the keyboard Standard output is set to the display Standard error is set to the display Redirection causes other files to be used
Background image of page 10
Command Line Arguments Command line arguments are additional input for programs For example, gcc takes a number of command line arguments, such as in gcc -o hello helloworld.c C programs can also take command line arguments Another form of declaring the main function gives the main function two arguments
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int main(int argc, char *argv[]) int main(int argc, char* argv[]) number of arguments array of strings (the arguments) short for argument count, the count should be one more than the number of arguments the first string is the name of the command, the remaining strings are the additional arguments
Background image of page 12
Using Command Line Arguments When using command line arguments the spaces between arguments are counted To derive the value for argc The first element of argv is the name of the executable (the program name) On most systems The second and subsequent elements of argv are the arguments In the order in which they were entered
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/23/2011 for the course CMPT 120 taught by Professor Cuikerman during the Fall '08 term at Simon Fraser.

Page1 / 50

cmpt130_09files - File I/O File I/O Types of files Command...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online