Tuesday, February 5, 2008

CSE 2031 Lecture 9

Assignment FAQ

Output files as they are read. For first question : instead of outputting palendromes output 'line is too long' after a certain length.

file* ß fopen(…)

file* fp;

fp = fopen("temp","a");

fprintf(fp,'%s\n", "Hello");

.

.

.

fclose(fp);

printf == fprintf(stdout,…);

"r+" opens a file for reading and writing

"w+" creates a file or truncates it if it exists and reads and writes to it

File descriptor talks to offset talks to inode

When you give a command like write you specify a file descriptor, where you find the data and how much you want to write

A file is a sequence of bytes,

Fseek, frewind, fsetpos are ways to specify the file pointer and where in the file you would like to go in the offset

States of a stream

  • Opened
    • If you write you go to ..
      • Writing state
        • If you continue writing you stay in the write state
        • If you use seek or rewind you will go back to
          • Opened state
        • If you just finished writing then you read
          • Error state
    • If you read you go to
      • Reading state
        • If you continue reading you stay in the read state
        • I f you just finished reading then you write
          • Error state


 

Printf(format string; other args)

Format string

  • Literal text
  • Conversion specifier
    • Printf("hello world \n");
    • ^literal text
  • Printf("n=%d\n", n);
    • Literal text
      • "n=" + "\n"
    • Conversion specifier
      • "%d"
    • Other arguments
      • "n"
  • Using print f
    • A char and short is always converted to int
    • A float is always converted to double

Conversion specifier

  • Starts with "%"
    • 4 components (in order)
      • Flags
      • Field width
      • Precision
      • Conversion specifier
        • first 3 are optional


 

d,i

Int

[-]ddd (where d are digits)

u

Unsigned int

ddd

o

Unsigned int

ddd 0-7

x

Unsigned int

ddd 0-9 a-f

X

Unsigned int

ddd 0-9 A-F


 

f

Double

[-]ddd.ddd

E,e

Double

[-]d.ddd e/E +/- dd

G,g

Double

-4<= exponent <precision (life f or e,E

c

Int

Treats int as unsigned char

Prints the character

S

Char*

Output chars from array up to '\0'

P

Void*

System dependant

Prints an address, usually in hexidecimal

N

Int*

Stores in variable; # of characters printed so far. No conversion

%

 

Writes a '%' sign


 

Length specifier in conversion specifier

  • h,l,L
  • you can have the h in front on the integer conversion specifiers
    • conversion specifier applies to short int (unsigned short int)
    • remember char / short args already promoted to int
    • convert arg to short before printing
  • the L,f,e,E,g,G conversion specifier applies to long double argument
  • do now use lowercase l or h with f,e,E,g,G

flags

  • field width
    • %5d
      • Decimal no sign
      • Field width of size 5
    • %-d
      • Fiend width 5
      • '-' is a flag
        • Left jusitication
    • %*d
      • Take next arg as field width
      • Scanf("%*lf, …)
        • * in scanf the * says read a double from input

Precision

  • After the period
  • %.5d
    • Min # digits to output, pad left w/ 0's
  • %.5f
    • # of fraction digits
  • %.5g
    • Max # of significant digits
  • &.5s
    • Max # of characters to print from the string

Flags

  • -
    • Left justify
  • 0
    • Pads with 0's
  • +
    • Forces explicit plus sign
  • ' ' (space)
    • Use a space instead of plus sign in the output
  • #
    • Changes conversions
    • 0 à leading 0
    • %#F
      • The decimal sign is printed even if there is nothing after it

No comments: