week 12 monday what did we talk about last time exam 2
play

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


  1. Week 12 - Monday

  2.  What did we talk about last time?  Exam 2!  Before that:  Review  Before that:  Binary search trees  Started file I/O

  3. 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

  4.  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

  5.  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 opened (for reading, writing, etc.) FILE* file = fopen("data.txt", "r");

  6.  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. Open for appending. Write all data to the end of the file, preserving anything that is "a" 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.

  7.  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);

  8.  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);

  9.  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);

  10.  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

  11.  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

  12.  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, '$');

  13.  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);

  14.  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

  15.  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

  16.  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, …)

  17.  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

  18.  Wouldn't it be easier to use all Bytes in text Integer representation human readable files? 0 1  Binary files can be more efficient 92 2  In binary, all int values are the same 789 3 size, usually 4 bytes 4551 4  You can also load a chunk of 10890999 8 memory (like a WAV header) into 204471262 9 memory with one function call -2000000000 11

  19.  To specify that a file should be opened in binary mode, append a b to the mode string FILE* file = fopen("output.dat", "wb"); FILE* file = fopen("input.dat", "rb");  On some systems, the b has no effect  On others, it changes how some characters are interpreted

  20.  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);

  21.  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);

  22.  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

  23.  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);

  24.  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

  25.  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

  26.  Bitfields  Unions

  27.  Start on Project 5  Form teams if you haven't!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend