CPS 125: Digital Computation and Programming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CPS 125: Digital Computation and Programming

Description:

Functions for input will use infp as argument, those for output will use outfp. Opening a File ... fclose(infp); fclose(outfp); Standard Files ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 17
Provided by: Che6158
Category:

less

Transcript and Presenter's Notes

Title: CPS 125: Digital Computation and Programming


1
CPS 125 Digital Computation and Programming
  • Text and Binary File Processing

2
Outline
  • Input/Output Files
  • Binary Files
  • Search a Database
  • Case Study
  • Common Programming Errors

3
Files
  • Any collection of information stored in the
    computers secondary storage
  • Has a name, usually divided into two parts
  • filename.ext
  • Certain extensions have predefined meanings
  • c - C source lib - library file exe -
    Program txt - text file

4
Text Files
  • A named collection of characters saved in
    secondary storage
  • It appears as two-dimensional (line by line), but
    actually it is stored as a one-dimensional
    sequence of chars
  • Special printing characters
  • New line character \n
  • End-of-file character EOF (normally as a negative
    number returned from scanf function, have to
    catch it to stop reading the file)

5
String vs. Text File
  • String Text File
  • sequence of characters sequence of characters
  • usually short usually long
  • ends with null character ends with EOF character
  • temporary in memory permanent on disk
  • direct access si sequential access

6
Escape Sequences
  • \n new line
  • \t tab
  • \f form feed (new page)
  • \r return (go back to column 1 of current
    output line)
  • \b backspace
  • \\ the actual backslash \

7
Formatting Output with printf
  • c a single character
  • s a string
  • d an integer (base 10)
  • o an integer (base 8)
  • x an integer (base 16)
  • f a floating-point number
  • e a floating-point number in scientific notation
  • E a floating-point number in scientific notation
  • a sign

8
Using Files in C
  • Operations in ltstdio.hgt
  • Steps
  • Declare variable of type FILE
  • Open file using fopen
  • Perform I/O using functions in ltstdio.hgt
  • Close file using fclose

9
FILE Pointer
  • C Standard type FILE avoids system differences in
    file systems
  • Must declare separate variable for each file that
    is open simultaneously
  • For example, for input and output, use
  • FILE infp, outfp
  • Functions for input will use infp as argument,
    those for output will use outfp

10
Opening a File
  • Opening a file means associating the FILE
    variable with an actual file on disk
  • Use call of form
  • file-pointer fopen(filename, mode)
  • If the call unsuccessful, return NULL
  • if (file-pointer NULL) /handle error/
  • printf(Cannot open file \n)
  • The mode specifies type of data transfer
  • r - read w - write a - append

11
File Modes
  • r - open for reading (input operations) -
    file must already exist
  • w - open for writing (output operations) -
    new file created (erases old one)
  • a - open for appending (output operations) -
    new file created if none already exists - info
    put at end of existing file otherwise

12
Performing I/O Operations
  • Character by character
  • getc and putc
  • Line by line
  • fgets and fputs
  • Formatted data
  • fscanf and fprintf

13
Closing Files
  • Be sure to close files when finished with them
  • Helps prevent errors, and having too many files
    open at once
  • Exiting from program closes files, but avoid it
  • Use
  • fclose(infp)
  • fclose(outfp)

14
Standard Files
  • The standard I/O library defines three special
    identifiers as FILE constants
  • stdin - standard input file (usually keyboard)
  • stdout - standard output file (usually screen)
  • stderr - standard error file for error
    messages (usually screen)

15
scanf use
  • scanf returns the number of values correctly
    converted, or EOF if at the end-of-file marker
  • Each argument after the format string must be a
    pointer
  • For simple variables, use for address
  • For arrays, name is automatically a pointer
  • scanf(d s, num, name)

16
Binary Files
  • A file containing binary numbers that are the
    computers internal representation
  • FILE bi_fp
  • int i
  • bi_fp fopen("nums.bin", "wb")
  • for (i 2 i lt 500 i 2)
  • fwrite(i, sizeof (int), 1, bi_fp)
  • fclose(bi_fp)
  • If array score is an array of 10 integers,
  • fwrite(score, sizeof(int), 10, binaryp)
  • fread has the same form (arguments) as fwrite
Write a Comment
User Comments (0)
About PowerShow.com