Formatted Output - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Formatted Output

Description:

Formatted Output – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 27
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Formatted Output


1
Formatted Output
Lect15
  • Tabs
  • include ltstdio.hgt
  • int main()
  • printf(" 111111111122222222223\n")
  • printf("123456789012345678901234567890\n")
  • printf("W\tX\tY\tZ\n")
  • system("PAUSE") return 0
  • Output
  • 111111111122222222223
  • 123456789012345678901234567890
  • W X Y Z

2
int and float Output
  • printf(" 111111111122222222223\n")
  • printf("123456789012345678901234567890\n")
  • printf("d\td\td\td\n",1, 10, 100, 1000)
  • printf("f\tf\tf\n",1.0, 10.0, 100.0)
  • Output
  • 111111111122222222223
  • 123456789012345678901234567890
  • 1 10 100 1000
  • 1.000000 10.000000 100.000000

3
Table
  • printf("Time\tVelocity\tAcceleration\n")
  • printf("f\tf\tf\n",1.0, 10.0, 100.0)
  • Output
  • 111111111122222222223
  • 123456789012345678901234567890
  • Time Velocity Acceleration
  • 1.000000 10.000000 100.000000
  • Cure
  • printf("Time\t\tVelocity\tAcceleration\n")

4
Ragged Tables
  • printf("f\tf\tf\n",1.0, 10.0, 100.0)
  • printf("f\tf\tf\n",200.0, 2.0, 20.0)
  • Output
  • 1.000000 10.000000 100.000000
  • 200.000000 2.000000 20.000000

5
Field Definitions
  • printf("7.2f\t7.2f\t7.2f\n",1.0, 10.0, 100.0)
  • printf("7.2f\t7.2f\t7.2f\n",200.0, 2.0, 20.0)
  • Output
  • 1.00 10.00 100.00
  • 200.00 2.00 20.00
  • Definition T.Df
  • T total number of characters
  • D number of characters after the decimal point.

6
Rounding
  • float pi3.14159
  • printf("f\n",pi) 3.141590
  • printf(".5f\n",pi) 3.14159
  • printf(".4f\n",pi) 3.1416
  • printf(".3f\n",pi) 3.142
  • printf(".2f\n",pi) 3.14

7
Minimum Field Size
  • int i123
  • printf("123456789\n") 123456789
  • printf("d\n",i) 123
  • printf("6d\n",i) 123
  • printf("5d\n",i) 123
  • printf("4d\n",i) 123
  • printf("3d\n",i) 123
  • printf("2d\n",i) 123

8
and -
  • int i123
  • printf("123456789\n") 123456789
  • printf("6d\n",i) 123
  • printf("6d\n",i) 123
  • printf("-6d\n",i) 123

9
e, E, g, G
  • float f11234567.0, f2123.4567
  • printf("f, f\n",f1, f2)
  • printf("e, 9.3E\n",f1, f2)
  • printf("g, G\n",f1, f2)
  • Output
  • 1234567.000000, 123.456703
  • 1.234567e06, 1.235E02
  • 1.23457e06, 123.457

10
Files
  • Files are used for permanent and temporary
    storage of various items
  • Text circle.cpp, report.wpd, essay.doc
  • Executables circle.exe
  • Data readings.dat

11
Disk Format
  • A file is made up of many sectors on many tracks
    of the disk.

Track
Disk
Sector
12
System
  • The system has a directory that lists files by
    name.
  • The system maintains a file allocation table
    (FAT) to associate the filename to the physical
    locations on the disk.
  • While a file is active, a file control block
    (FCB) or handle is also active.

13
C and Files
  • C allows for the access to files, to store
    (write) data and retrieve (read) data.
  • C communicates with the system, allowing for a
    minimum of operations that must be carried out by
    the programmer
  • fopen
  • read write
  • fclose

14
A Simple File Writing Program
  • include ltstdio.hgt
  • int main()
  • FILE fp //FILE type fp is a
  • // pointer to a file
  • // essentially the FCB
  • fpfopen("myfile.txt","w")
  • fprintf(fp,"A line in a file\n")
  • fclose(fp)
  • system("PAUSE") return 0

Lect1501.c
15
fopen, fclose
  • fopen returns the pointer to the file or the file
    handle.
  • fopen requires the filename and mode
  • w write
  • r read
  • a append, write to the end of the file
  • preserving the old contents.
  • fclose closes the file pointed to by fp.

16
Append
  • fpfopen("myfile.txt","a")
  • fprintf(fp,"A second line\n")
  • fclose(fp)
  • Now the file has two lines
  • A line in a file
  • A second line
  • Using fpfopen("myfile.txt","w")
  • would have erased the old contents and the file
    would have just
  • A second line

17
ASCII Data File
  • include ltstdio.hgt
  • int main()
  • FILE fp int i
  • fpfopen("myfile.txt","w")
  • fprintf(fp,"d\n", 23)
  • fclose(fp)
  • fpfopen("myfile.txt","r")
  • fscanf(fp,"d",i)
  • fclose(fp)
  • printf("d\n",i)
  • system("PAUSE") return 0

18
Advantages - Disadvantages
  • ADVANTAGE We can read and write an ASCII data
    file with a text editor. It is easy to check.
  • DISADVANTAGE Each integer, which takes 2 bytes
    in the regular C data representation, can take up
    to 6 bytes in ASCII (-32768).
  • Hence we wish to write in the binary format of C.

19
Binary Data File
  • include ltstdio.hgt
  • int main()
  • FILE fp int i23
  • fpfopen("myfile.bin","w")
  • fwrite(i, sizeof(int), 1, fp)
  • fclose(fp)
  • fpfopen("myfile.bin","r")
  • fread(i, sizeof(int), 1, fp)
  • fclose(fp)
  • printf("d\n",i)
  • system("PAUSE") return 0

20
fread, fwrite
  • The parameter lists for both fread and fwrite are
    the same
  • (pointer to variable, size of variable, number of
    variables, file pointer)
  • include ltstdio.hgt
  • define SIZE 1000
  • int main()
  • FILE fp int aSIZE
  • fpfopen("myfile.bin","w")
  • fwrite(a, sizeof(int), SIZE, fp)
  • fclose(fp)
  • system("PAUSE") return 0

21
Same File Reading and Writing
  • include ltstdio.hgt
  • define SIZE 10
  • int main()
  • FILE fp int aSIZE, bSIZE, i
  • for(i0 iltSIZE i) aii
  • fpfopen("myfile.bin","w")
  • fwrite(a, sizeof(int), SIZE, fp)
  • rewind(fp)
  • fread(b, sizeof(int), SIZE, fp)
  • fclose(fp)
  • for(i0 iltSIZE i) printf("d ", bi)
  • system("PAUSE") return 0

22
rewind and fseek
  • rewind gets us to the beginning of the file.
  • We can use fseek to move around the file.
  • We can use fseek to find our new required
    position
  • fseek(FILE stream, long offset,
  • int whence)
  • SEEK_SET seek from start of file.
  • SEEK_CUR seek from current location.
  • SEEK_END seek from end of file.

23
whence
  • include ltstdio.hgt
  • int main()
  • FILE fp int i
  • fpfopen("myfile.bin","r")
  • fseek(fp, sizeof(int)2, SEEK_SET)
  • fread(i, sizeof(int), 1, fp)
  • fclose(fp)
  • printf("d\n", i)
  • system("PAUSE") return 0
  • i2 fseek passes over the first two integers.

24
Error Checking
  • If fopen cannot open a file fopen returns the
    NULL pointer
  • if((fpfopen("myfile.txt","r"))NULL)
  • printf("File does not exist\n")
  • If there are any errors when closing a file,
    fclose returns EOF, else zero
  • if(fclose(fp)EOF)
  • printf("Errors closing file\n")
  • Check the return values on the other functions.

25
Variable Filename
  • include ltstdio.hgt
  • int main()
  • FILE fp char filename20 int i
  • printf("Input filename ")
  • scanf("s", filename)
  • if((fpfopen(filename,"r"))NULL)
  • printf("Cannot open file s\n\a", filename)
  • else
  • fread(i, sizeof(int), 1, fp)
  • fclose(fp)
  • system("PAUSE") return 0

26
Next File in Sequence
  • int main()
  • FILE fp char filename20 int i0, error1
  • do
  • sprintf(filename, "file1d.txt", i)
  • if((fpfopen(filename,"r"))!NULL)
    fclose(fp)
  • else error0
  • while(error1)
  • fpfopen(filename,"w")
  • printf("Opened file s\n", filename)
  • fclose(fp)
  • system("PAUSE") return 0
Write a Comment
User Comments (0)
About PowerShow.com