Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-20-fileprocessing

 ics212-20-fileprocessing

Avatar for William Albritton

William Albritton

November 01, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  File Basics  Sequential Access Files 

    Text Files and Binary Files  Random-Access Files
  2. Data Hierarchy  Byte: 8 bits (ASCII character ‘A’ =

    01000001)  Field: Group of characters (character string “Fred”)  Record: Composed of related fields (struct)  File: Group of related records (student record file)  Database: Group of related files (students, faculty, and staff files)
  3. What is a Record?  Record is a group of

    related fields  For example, in the C language, a structure is the way to define a record  Special field called a “record key” or “primary key” is used to help retrieve a specific record
  4. What is a File?  File is a group of

    related records  A file stores records ordered by the record key field or primary key field  The C language views a file as a sequential stream of bytes  Files end with an end-of-file (EOF) marker
  5. End-of-File Marker  File ends with a end-of-file (EOF) marker

     EOF key combinations on different systems:  UNIX and Mac: Ctrl-D; PC: Ctrl-Z  Often used to loop through a file (see write.c) while(EOF != character){ fputc(character, filePointer); character = fgetc(stdin); }
  6. Streams  Three file streams (file pointers) are automatically provided

    in the C language  Standard input stream to keyboard (stdin)  Standard output stream to screen (stdout)  Standard error stream to screen (stderr)
  7. Read a Character from a File  int fgetc(FILE *filePointer)

     Reads one character at a time from a file  Similar to function getchar()  On success, returns character  If EOF or error, returns EOF char character1 = fgetc(stdin); char character2 = getchar();
  8. Write a Character to a File  int fputc(int character,

    FILE *filePointer);  Writes one character at a time to a file  If successful, returns the character  If not successful, returns EOF  Similar to function putchar() error = fputc(character1, stdout); error = putchar(character1);
  9. Write Formatted Output to a File  int fprintf(FILE *filePointer,

    const char *format, arg1, arg2, arg3, …);  Writes formatted output to a file  Similar to function printf() fprintf(stdout,“%c”,character1); printf(“%c”,character1);
  10. Example Program  See example code at: streams.c  Can

    redirect the input (<) and output (>) to files  Can use append (>>) to output % cp streams.c program.c % make –f makefile-program % ./program <char.txt >out.txt % more out.txt
  11. Sequential Access Files  Individual records are not fixed in

    length  Have to do a search through the entire file to find something
  12. File Structure  FILE is a structure defined in <stdio.h>

     Contains data used to process a file  Location of a buffer  Current character position in the buffer  Whether file is being read or written  Whether errors or EOF have occurred
  13. Function fopen() to Open a File  FILE *fopen(char *fileName,

    char *fileMode);  1st argument: name of the file  2nd argument: how to use the file (read, write, etc.)  Function fopen() returns a pointer to a FILE structure  If error (such as no such file exists) will return NULL FILE *filePointer = NULL; filePointer = fopen(“file.txt”,“w”);
  14. File Modes  Mode "r"  Open a file for

    reading  Mode "w"  Open or create a file for writing  If file already exists, erase the contents
  15. File Modes  Mode "a"  Append: open or create

    a file  Write to the end of the file  Mode "r+"  Open a file for updating (reading and writing)
  16. File Modes  Mode "w+"  Open or create a

    file for updating  Erase contents of existing file  Mode "a+"  Append: open or create a file for updating  All writing done at end of file
  17. Function fclose() to Close a File  int fclose(FILE *filePointer);

     Closes a file  Returns 0, if successful  Returns EOF, if not successful  Will be done automatically after a program finishes int fileClosed = fclose(filePointer);
  18. Example Program  See example code at: write.c  Reads

    text from the keyboard and writes the text to a file called “file.txt” % cp write.c program.c % make –f makefile-program % ./program (type some text) % more file.txt
  19. Function fputs() Writes String to File  int fputs(const char

    *string, FILE *filePointer);  Writes a string to a file  Returns a nonnegative integer for success  Returns EOF for error int error = 0; error = fputs(“abc”, filePointer);
  20. Function fgets() Reads String from File  char *fgets(char *string,

    int max, FILE *filePointer);  Reads a line from a file, until reads max-1 characters, newline, or EOF  Stores all the characters (including '\n') from the file into the string, which ends with NULL character ('\0')  If error, returns NULL; if success, returns pointer to string
  21. Buffer Overflow Attack  Do NOT use: char* gets(char *string);

     Reads one line from standard input, and puts it into array “string”, including blank spaces  Should not be used because of the potential for “buffer overflow” attack  Entering too many characters can crash the program, write over memory, and/or run code on your computer
  22. Buffer Overflow Attack Prevention  char *fgets(char *string, int max,

    FILE *filePointer);  Use the fgets() function instead, which limits the number of characters that can be entered  See example code at: inputline.c char string[MAX] = {‘\0’}; fgets(string, MAX, stdin); printf(“%s”, string);
  23. Function feof() Detects End of File  int feof(FILE *filePointer);

     Returns non-zero, if end-of-file (EOF)  Returns 0, if not end-of-file (EOF) while(0 == feof(filePointer)){ //file processing code . . . }
  24. Function rewind() Used to Reread a File  void rewind

    (FILE *filePointer);  Once a file is read, the file position pointer is at the end of the file, so this resets it to the beginning of the file  File position pointer is an integer value, which is the byte location in the file for next read or write rewind(filePointer);
  25. Example Program  See example code at: read.c  Reads

    from a file called “file.txt” and displays contents of file on the screen two times % cp read.c program.c % make –f makefile-program % ./program
  26. Text Files  Store ASCII characters  Such as character

    ‘A’ = 01000001  ASCII uses only 7 bits, 8th bit is for error control  Easy to read, but take up more space  Example text files: file.txt, webpage.html, program.c
  27. Binary Files  Store character and non-character data  Compact,

    but more complex to read  Example binary files: photo.gif, program.exe, paper.docx, slides.pptx
  28. Binary Files  Add a ‘b’ for binary file modes

     rb, wb, ab, rb+, wb+, rb+  Possible errors  Reading/transferring a binary file as a text file may corrupt the 8th bit
  29. What are Random-Access Files?  Files that are organized into

    records, which are fixed in length  Can be access a specific record without searching though other records  Much quicker to access data than sequential access files
  30. How Do Random-Access Files Work?  Use a record key

    to locate a record by calculating the distance from the beginning of the file  Can update and delete data in the file without overwriting other data in the file
  31. Function fwrite()  size_t fwrite(const void *buffer, size_t size, size_t

    count, FILE *filePointer);  buffer = address of variable or 1st element in array  size = size of each object in bytes  count = number of objects to be written  filePointer = the file to send the data to  Returns the number of objects written successfully
  32. Creating Random-Access Files  Use function fwrite() to create the

    file and to set up the file, so records can be easily added later on  See example program: creating.c  Creates 20 student records in a file called “students.txt” % cp creating.c program.c % make –f makefile-program % ./program
  33. Function fseek()  int fseek(FILE *stream, long int offset, int

    whence)  stream = file pointer to be set to a given offset  offset = number of bytes from “whence”, which is:  SEEK_SET (from beginning of file)  SEEK_CUR (from current location)  SEEK_END (from end of file)  If success, return zero; if error, return non-zero
  34. Writing to Random-Access Files  Use fseek() to position the

    file position pointer when writing to a file  See example program: writing.c  Writes student records to a file % make –f makefile-writing % ./program
  35. Function fread()  size_t fread(void *buffer, size_t size, size_t count,

    FILE *filePointer);  buffer = address of array or variable that stores objects  size = size of each object in bytes  count = number of objects to be read  filePointer = the file from which the memory is read  Returns number of objects read successfully
  36. Reading from a Random-Access Files  Use fread() to read

    a specific number of bytes from a file into memory  See example program: reading.c % cp reading.c program.c % make –f makefile-program % ./program
  37. Memory Management  File Basics  Sequential Access Files 

    Text Files and Binary Files  Random-Access Files