Week 12 - Monday What did we talk about last time? Exam 2! Before - - PowerPoint PPT Presentation

week 12 monday what did we talk about last time exam 2
SMART_READER_LITE
LIVE PREVIEW

Week 12 - Monday What did we talk about last time? Exam 2! Before - - PowerPoint PPT Presentation

Week 12 - Monday What did we talk about last time? Exam 2! Before that: Review Before that: Binary search trees Started file I/O The key to performance is elegance, not battalions of special cases. The terrible


slide-1
SLIDE 1

Week 12 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  Exam 2!  Before that:

  • Review

 Before that:

  • Binary search trees
  • Started file I/O
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

The key to performance is elegance, not battalions of special cases. The terrible temptation to tweak should be resisted unless the payoff is really noticeable. Jon Bently and M. Douglas McIlroy Computer Scientists at Bell Labs

slide-6
SLIDE 6
slide-7
SLIDE 7

 Think of a file as a stream of bytes  It is possible to read from the stream  It is possible to write to the stream  It is even possible to do both  Central to the idea of a stream is also a file stream pointer,

which keeps track of where in the stream you are

 We have been redirecting stdin from and stdout to files,

but we can access them directly as well

slide-8
SLIDE 8

 To open a file, call the fopen() function  It returns a pointer to a FILE object  Its first argument is the path to the file as a null-terminated

string

 Its second argument is another string that says how it is being

  • pened (for reading, writing, etc.)

FILE* file = fopen("data.txt", "r");

slide-9
SLIDE 9

 The following are legal arguments for the second string

Argument Meaning "r" Open for reading. The file must exist. "w" Open for writing. If the file exists, all its contents will be erased. "a" Open for appending. Write all data to the end of the file, preserving anything that is already there. "r+" Open a file for reading and writing, but it must exist. "w+" Open a file for reading and writing, but if it exists, its contents will be erased. "a+" Open a file for reading and writing, but all writing is done to the end of the file.

slide-10
SLIDE 10

 Once you've got a file open, write to it using fprintf() the

same way you write to the screen with printf()

 The first argument is the file pointer  The second is the format string  The third and subsequent arguments are the values

FILE* file = fopen("output.dat", "w"); fprintf(file, "Yo! I got %d on it!\n", 5);

slide-11
SLIDE 11

 Once you've got a file open, read from it using fscanf() the

same way you read from keyboard with scanf()

 The first argument is the file pointer  The second is the format string  The third and subsequent arguments are pointers to the

values you want to read into

FILE* file = fopen("input.dat", "r"); int value = 0; fscanf(file, "%d", &value);

slide-12
SLIDE 12

 When you're doing using a file, close the file pointer using the

fclose() function

 It's a good idea to close them as soon as you don't need them

anymore

  • It takes up system resources
  • You can only have a limited number of files open at once
  • You can't open a file in one program when it's open in another
  • Data might not be written to a file unless you explicitly close it

FILE* file = fopen("input.dat", "r"); int value = 0; fscanf(file, "%d", &value); fclose(file);

slide-13
SLIDE 13

 Write a program that prompts the user for an integer n and a

file name

 Open the file for writing  Write the value n on the first line of the file  Then, print n random numbers, each on its own line  Close the file

slide-14
SLIDE 14

 Write a program that reads the file generated in the previous

example and finds the average of the numbers

 Open the file for reading  Read the value n so you know how many numbers to read  Read the n random numbers  Compute the average and print it out  Close the file

slide-15
SLIDE 15

 If you need to do character by character output, you can use

fputc()

 The first argument is the file pointer  The second is the character to output  putc() is an equivalent function

FILE* file = fopen("output.dat", "w"); for(int i = 0; i < 100; ++i) fputc(file, '$');

slide-16
SLIDE 16

 If you need to do character by character input, you can use fgetc()  The argument is the file pointer  It returns the character value or EOF if there's nothing left in the file  getc() is an equivalent function FILE* file = fopen("input.dat", "r"); int count = 0; while( fgetc(file) != EOF ) ++count; printf("There are %d characters in the file\n", count);

slide-17
SLIDE 17

 Lots of errors can happen with file I/O  If a file cannot be opened with the given mode, fopen()

returns NULL and errno is set to an appropriate error code

 The fprintf() function returns the number of characters

written

  • A value less than or equal to 0 indicates error

 The fscanf() function returns the number of items read

  • If that number is less than expected, it's an error
slide-18
SLIDE 18

 C programs that run on the command line have the following

file pointers open by default

  • stdin
  • stdout
  • stderr

 You can use them where you would use other file pointers

slide-19
SLIDE 19

 You can think of the input and output functions you've been

using as special cases of these file operations

  • They are often implemented that way

 For example:

  • getchar() is equivalent to fgetc(stdin)
  • printf(…) is equivalent to fprintf(stdout,…)
  • scanf(…) is equivalent to fscanf(stdin, …)
slide-20
SLIDE 20
slide-21
SLIDE 21

 Technically, all files are binary files

  • They all carry data stored in binary

 But some of those binary files are called text files because

they are filled with human readable text

 When most people talk about binary files, they mean files

with data that is only computer readable

slide-22
SLIDE 22

 Wouldn't it be easier to use all

human readable files?

 Binary files can be more efficient

  • In binary, all int values are the same

size, usually 4 bytes

 You can also load a chunk of

memory (like a WAV header) into memory with one function call

Integer Bytes in text representation 1 92 2 789 3 4551 4 10890999 8 204471262 9

  • 2000000000

11

slide-23
SLIDE 23

 To specify that a file should be opened in binary mode,

append a b to the mode string

 On some systems, the b has no effect  On others, it changes how some characters are interpreted

FILE* file = fopen("output.dat", "wb"); FILE* file = fopen("input.dat", "rb");

slide-24
SLIDE 24

 The fread() function allows you to read binary data from a file

and drop it directly into memory

 It takes

  • A pointer to the memory you want to fill
  • The size of each element
  • The number of elements
  • The file pointer

double data[100]; FILE* file = fopen("input.dat", "rb"); fread(data, sizeof(double), 100, file); fclose(file);

slide-25
SLIDE 25

 The fwrite() function allows for binary writing  It can drop an arbitrarily large chunk of data into memory at once  It takes

  • A pointer to the memory you want to write
  • The size of each element
  • The number of elements
  • The file pointer

short values[50]; FILE* file = NULL; //fill values with data file = fopen("output.dat", "wb"); fwrite(values, sizeof(short), 50, file); fclose(file);

slide-26
SLIDE 26

 Binary files can be treated almost like a big chunk of memory  It is useful to move the location of reading or writing inside

the file

  • Some file formats have header information that says where in the

file you need to jump to for data

 fseek() lets you do this  Seeking in text files is possible but much less common

slide-27
SLIDE 27

 The fseek() function takes

  • The file pointer
  • The offset to move the stream pointer (positive or negative)
  • The location the offset is relative to

 Legal locations are

  • SEEK_SET

From the beginning of the file

  • SEEK_CUR

From the current location

  • SEEK_END

From the end of the file (not always supported)

FILE* file = fopen("input.dat", "rb"); int offset; fread(&offset,sizeof(int),1,file); //get offset fseek(file, offset, SEEK_SET);

slide-28
SLIDE 28

 Write a program that prompts the user for an integer n and a

file name

 Open the file for writing in binary  Write the value n in binary  Then, write the n random numbers in binary  Close the file

slide-29
SLIDE 29

 Write a program that reads the file generated in the previous

example and finds the average of the numbers

 Open the file for reading  Read the value n in binary so you know how many numbers to

read

 Read the n random numbers in binary  Compute the average and print it out  Close the file

slide-30
SLIDE 30
slide-31
SLIDE 31

 Bitfields  Unions

slide-32
SLIDE 32

 Start on Project 5

  • Form teams if you haven't!