SlideShare a Scribd company logo
1 of 50
Computer Science: A Structured Programming Approach Using C 1
Objectives
❏ To understand the basic properties and characteristics of external files
❏ To understand the C implementation of file I/O using streams
❏ To write programs that read and write text files using the formatting functions
❏ To write programs that read and write text files using the C character I/O
functions
❏ To write programs that handle simple I/O errors
❏ To understand and implement basic data validation concepts
Chapter 7
Text Input/Output
Computer Science: A Structured Programming Approach Using C 2
7-1 Files
A file is an external collection of related data treated
as a unit. The primary purpose of a file is to keep a
record of data. Since the contents of primary memory
are lost when the computer is shut down, we need files
to store our data in a more permanent form.
File Name
File Information Table
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C 3
A file is an external
collection of related data treated as a unit.
Note
Computer Science: A Structured Programming Approach Using C 4
7-2 Streams
As we briefly discussed in Chapter 2, data is input to
and output from a stream. A stream can be associated
with a physical device, such as a terminal, or with a
file stored in auxiliary memory.
Text And Binary Streams
Stream-File Processing
System-Created Streams
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C 5
FIGURE 7-1 Streams
Computer Science: A Structured Programming Approach Using C 6
Standard stream names have
already been declared in the stdio.h header
file and cannot be declared again in our program.
Note
Computer Science: A Structured Programming Approach Using C 7
There is no need to open and close the standard streams.
It is done automatically by the operating system.
Note
Computer Science: A Structured Programming Approach Using C 8
7-3 Standard Library Input/Output
Functions
The stdio.h header file contains several different
input/output function declarations. They are grouped
into eight different categories, as shown in Figure 7-2.
The first three will be discussed in the following
sections. Those shown in shaded boxes will be
discussed in Chapters 11 and 13.
File Open and Close
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C 9
FIGURE 7-2 Categories of Standard Input/Output Functions
Computer Science: A Structured Programming Approach Using C 10
FIGURE 7-3 File Open Results
Computer Science: A Structured Programming Approach Using C 11
Table 7-1 Text File Modes
Computer Science: A Structured Programming Approach Using C 12
FIGURE 7-4 File-Opening Modes
Computer Science: A Structured Programming Approach Using C 13
PROGRAM 7-1 Testing for Open and Close Errors
Computer Science: A Structured Programming Approach Using C 14
PROGRAM 7-1 Testing for Open and Close Errors
Computer Science: A Structured Programming Approach Using C 15
7-4 Formatting Input/Output
Functions
In Chapter 2 we introduced two formatting
input/output functions, scanf and printf. These two
functions can be used only with the keyboard and
monitor. The C library defines two more general
functions, fscanf and fprintf, that can be used with
any text stream.
Stream Pointer
Format Control Strings
Input Formatting (scanf and fscanf)
Output Formatting (printf and fprintf)
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C 16
Table 7-2 Formatting Functions
Computer Science: A Structured Programming Approach Using C 17
A whitespace character in an input format string causes
leading whitespace characters in the input to be
discarded. A whitespace character in an output
format string is copied to the output stream.
Note
Computer Science: A Structured Programming Approach Using C 18
The number, order, and type of the conversion specifications
must match the number, order, and type of the parameters
in the list. Otherwise, the result will be unpredictable and
may terminate the input/output function.
Note
Computer Science: A Structured Programming Approach Using C 19
FIGURE 7-5 Conversion Specifications
Computer Science: A Structured Programming Approach Using C 20
scanf reads from stdin;
fscanf reads from a user-specified stream.
Note
Computer Science: A Structured Programming Approach Using C 21
Table 7-3 Sizes and Conversion Code for scanf Family
Computer Science: A Structured Programming Approach Using C 22
Table 7-3 Sizes and Conversion Code for scanf Family
Computer Science: A Structured Programming Approach Using C 23
FIGURE 7-6 Side Effect and Value of scanf and fscanf
Computer Science: A Structured Programming Approach Using C 24
FIGURE 7-7 Another Common Error
Computer Science: A Structured Programming Approach Using C 25
PROGRAM 7-2 Checking scanf Results
Computer Science: A Structured Programming Approach Using C 26
PROGRAM 7-2 Checking scanf Results
Computer Science: A Structured Programming Approach Using C 27
FIGURE 7-8 Missing Address Operator in scanf
Computer Science: A Structured Programming Approach Using C 28
FIGURE 7-9 Data Type Conflict
Computer Science: A Structured Programming Approach Using C 29
FIGURE 7-10 fscanf Examples
Computer Science: A Structured Programming Approach Using C 30
Discarding the return character is different
from consuming it.
Discarding can be done by whitespaces
in the control string;
consuming can only be done by reading
the return character with a %c.
Note
Computer Science: A Structured Programming Approach Using C 31
Table 7-4 Flags, Sizes, and Conversion Codes for printf Family
Computer Science: A Structured Programming Approach Using C 32
Table 7-4 Flags, Sizes, and Conversion Codes for printf Family
Computer Science: A Structured Programming Approach Using C 33
Table 7-5 Flag Formatting Options
Computer Science: A Structured Programming Approach Using C 34
FIGURE 7-11 Side Effect and Value of printf
Computer Science: A Structured Programming Approach Using C 35
PROGRAM 7-3 Read and Print Text File of Integers
Computer Science: A Structured Programming Approach Using C 36
PROGRAM 7-3 Read and Print Text File of Integers
Computer Science: A Structured Programming Approach Using C 37
PROGRAM 7-4 Copy Text File of Integers
Computer Science: A Structured Programming Approach Using C 38
PROGRAM 7-4 Copy Text File of Integers
Computer Science: A Structured Programming Approach Using C 39
PROGRAM 7-4 Copy Text File of Integers
Computer Science: A Structured Programming Approach Using C 40
PROGRAM 7-5 Append Data to File
Computer Science: A Structured Programming Approach Using C 41
PROGRAM 7-5 Append Data to File
Computer Science: A Structured Programming Approach Using C 42
PROGRAM 7-5 Append Data to File
Computer Science: A Structured Programming Approach Using C 43
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 44
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 45
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 46
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 47
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 48
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 49
PROGRAM 7-6 Student Grades
Computer Science: A Structured Programming Approach Using C 50
PROGRAM 7-6 Student Grades

More Related Content

Similar to Chap-07-1.ppt

Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chaptersIbrahim Elewah
 
Unit-2_Getting Started With ‘C’ Language (3).pptx
Unit-2_Getting Started With ‘C’ Language (3).pptxUnit-2_Getting Started With ‘C’ Language (3).pptx
Unit-2_Getting Started With ‘C’ Language (3).pptxSanketShah544615
 
Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)IIUM
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmIIUM
 
CIS110 Computer Programming Design Chapter (7)
CIS110 Computer Programming Design Chapter  (7)CIS110 Computer Programming Design Chapter  (7)
CIS110 Computer Programming Design Chapter (7)Dr. Ahmed Al Zaidy
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manualAnil Bishnoi
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesLiemLe21
 
C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse studentsAbdur Rahim
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRokonuzzaman Rony
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYRajeshkumar Reddy
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question keyArthyR3
 

Similar to Chap-07-1.ppt (20)

C programming-1.pptx
C programming-1.pptxC programming-1.pptx
C programming-1.pptx
 
Chap-02-1.ppt
Chap-02-1.pptChap-02-1.ppt
Chap-02-1.ppt
 
Computer programming all chapters
Computer programming all chaptersComputer programming all chapters
Computer programming all chapters
 
Chapter 02-02.pptx
Chapter 02-02.pptxChapter 02-02.pptx
Chapter 02-02.pptx
 
Intro
IntroIntro
Intro
 
Sample Programs.ppt
Sample Programs.pptSample Programs.ppt
Sample Programs.ppt
 
Unit-2_Getting Started With ‘C’ Language (3).pptx
Unit-2_Getting Started With ‘C’ Language (3).pptxUnit-2_Getting Started With ‘C’ Language (3).pptx
Unit-2_Getting Started With ‘C’ Language (3).pptx
 
Embedded concepts
Embedded conceptsEmbedded concepts
Embedded concepts
 
Chap 01-1 jwfiles
Chap 01-1 jwfilesChap 01-1 jwfiles
Chap 01-1 jwfiles
 
Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)Csc1100 lecture01 ch01 pt2-paradigm (1)
Csc1100 lecture01 ch01 pt2-paradigm (1)
 
Csc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigmCsc1100 lecture01 ch01 pt2-paradigm
Csc1100 lecture01 ch01 pt2-paradigm
 
CIS110 Computer Programming Design Chapter (7)
CIS110 Computer Programming Design Chapter  (7)CIS110 Computer Programming Design Chapter  (7)
CIS110 Computer Programming Design Chapter (7)
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
 
C programming languag for cse students
C programming languag for cse studentsC programming languag for cse students
C programming languag for cse students
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C_Intro.ppt
C_Intro.pptC_Intro.ppt
C_Intro.ppt
 
Session1 c1
Session1 c1Session1 c1
Session1 c1
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
 
Compiler gate question key
Compiler gate question keyCompiler gate question key
Compiler gate question key
 

Recently uploaded

Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 

Recently uploaded (20)

Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 

Chap-07-1.ppt

  • 1. Computer Science: A Structured Programming Approach Using C 1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To understand the C implementation of file I/O using streams ❏ To write programs that read and write text files using the formatting functions ❏ To write programs that read and write text files using the C character I/O functions ❏ To write programs that handle simple I/O errors ❏ To understand and implement basic data validation concepts Chapter 7 Text Input/Output
  • 2. Computer Science: A Structured Programming Approach Using C 2 7-1 Files A file is an external collection of related data treated as a unit. The primary purpose of a file is to keep a record of data. Since the contents of primary memory are lost when the computer is shut down, we need files to store our data in a more permanent form. File Name File Information Table Topics discussed in this section:
  • 3. Computer Science: A Structured Programming Approach Using C 3 A file is an external collection of related data treated as a unit. Note
  • 4. Computer Science: A Structured Programming Approach Using C 4 7-2 Streams As we briefly discussed in Chapter 2, data is input to and output from a stream. A stream can be associated with a physical device, such as a terminal, or with a file stored in auxiliary memory. Text And Binary Streams Stream-File Processing System-Created Streams Topics discussed in this section:
  • 5. Computer Science: A Structured Programming Approach Using C 5 FIGURE 7-1 Streams
  • 6. Computer Science: A Structured Programming Approach Using C 6 Standard stream names have already been declared in the stdio.h header file and cannot be declared again in our program. Note
  • 7. Computer Science: A Structured Programming Approach Using C 7 There is no need to open and close the standard streams. It is done automatically by the operating system. Note
  • 8. Computer Science: A Structured Programming Approach Using C 8 7-3 Standard Library Input/Output Functions The stdio.h header file contains several different input/output function declarations. They are grouped into eight different categories, as shown in Figure 7-2. The first three will be discussed in the following sections. Those shown in shaded boxes will be discussed in Chapters 11 and 13. File Open and Close Topics discussed in this section:
  • 9. Computer Science: A Structured Programming Approach Using C 9 FIGURE 7-2 Categories of Standard Input/Output Functions
  • 10. Computer Science: A Structured Programming Approach Using C 10 FIGURE 7-3 File Open Results
  • 11. Computer Science: A Structured Programming Approach Using C 11 Table 7-1 Text File Modes
  • 12. Computer Science: A Structured Programming Approach Using C 12 FIGURE 7-4 File-Opening Modes
  • 13. Computer Science: A Structured Programming Approach Using C 13 PROGRAM 7-1 Testing for Open and Close Errors
  • 14. Computer Science: A Structured Programming Approach Using C 14 PROGRAM 7-1 Testing for Open and Close Errors
  • 15. Computer Science: A Structured Programming Approach Using C 15 7-4 Formatting Input/Output Functions In Chapter 2 we introduced two formatting input/output functions, scanf and printf. These two functions can be used only with the keyboard and monitor. The C library defines two more general functions, fscanf and fprintf, that can be used with any text stream. Stream Pointer Format Control Strings Input Formatting (scanf and fscanf) Output Formatting (printf and fprintf) Topics discussed in this section:
  • 16. Computer Science: A Structured Programming Approach Using C 16 Table 7-2 Formatting Functions
  • 17. Computer Science: A Structured Programming Approach Using C 17 A whitespace character in an input format string causes leading whitespace characters in the input to be discarded. A whitespace character in an output format string is copied to the output stream. Note
  • 18. Computer Science: A Structured Programming Approach Using C 18 The number, order, and type of the conversion specifications must match the number, order, and type of the parameters in the list. Otherwise, the result will be unpredictable and may terminate the input/output function. Note
  • 19. Computer Science: A Structured Programming Approach Using C 19 FIGURE 7-5 Conversion Specifications
  • 20. Computer Science: A Structured Programming Approach Using C 20 scanf reads from stdin; fscanf reads from a user-specified stream. Note
  • 21. Computer Science: A Structured Programming Approach Using C 21 Table 7-3 Sizes and Conversion Code for scanf Family
  • 22. Computer Science: A Structured Programming Approach Using C 22 Table 7-3 Sizes and Conversion Code for scanf Family
  • 23. Computer Science: A Structured Programming Approach Using C 23 FIGURE 7-6 Side Effect and Value of scanf and fscanf
  • 24. Computer Science: A Structured Programming Approach Using C 24 FIGURE 7-7 Another Common Error
  • 25. Computer Science: A Structured Programming Approach Using C 25 PROGRAM 7-2 Checking scanf Results
  • 26. Computer Science: A Structured Programming Approach Using C 26 PROGRAM 7-2 Checking scanf Results
  • 27. Computer Science: A Structured Programming Approach Using C 27 FIGURE 7-8 Missing Address Operator in scanf
  • 28. Computer Science: A Structured Programming Approach Using C 28 FIGURE 7-9 Data Type Conflict
  • 29. Computer Science: A Structured Programming Approach Using C 29 FIGURE 7-10 fscanf Examples
  • 30. Computer Science: A Structured Programming Approach Using C 30 Discarding the return character is different from consuming it. Discarding can be done by whitespaces in the control string; consuming can only be done by reading the return character with a %c. Note
  • 31. Computer Science: A Structured Programming Approach Using C 31 Table 7-4 Flags, Sizes, and Conversion Codes for printf Family
  • 32. Computer Science: A Structured Programming Approach Using C 32 Table 7-4 Flags, Sizes, and Conversion Codes for printf Family
  • 33. Computer Science: A Structured Programming Approach Using C 33 Table 7-5 Flag Formatting Options
  • 34. Computer Science: A Structured Programming Approach Using C 34 FIGURE 7-11 Side Effect and Value of printf
  • 35. Computer Science: A Structured Programming Approach Using C 35 PROGRAM 7-3 Read and Print Text File of Integers
  • 36. Computer Science: A Structured Programming Approach Using C 36 PROGRAM 7-3 Read and Print Text File of Integers
  • 37. Computer Science: A Structured Programming Approach Using C 37 PROGRAM 7-4 Copy Text File of Integers
  • 38. Computer Science: A Structured Programming Approach Using C 38 PROGRAM 7-4 Copy Text File of Integers
  • 39. Computer Science: A Structured Programming Approach Using C 39 PROGRAM 7-4 Copy Text File of Integers
  • 40. Computer Science: A Structured Programming Approach Using C 40 PROGRAM 7-5 Append Data to File
  • 41. Computer Science: A Structured Programming Approach Using C 41 PROGRAM 7-5 Append Data to File
  • 42. Computer Science: A Structured Programming Approach Using C 42 PROGRAM 7-5 Append Data to File
  • 43. Computer Science: A Structured Programming Approach Using C 43 PROGRAM 7-6 Student Grades
  • 44. Computer Science: A Structured Programming Approach Using C 44 PROGRAM 7-6 Student Grades
  • 45. Computer Science: A Structured Programming Approach Using C 45 PROGRAM 7-6 Student Grades
  • 46. Computer Science: A Structured Programming Approach Using C 46 PROGRAM 7-6 Student Grades
  • 47. Computer Science: A Structured Programming Approach Using C 47 PROGRAM 7-6 Student Grades
  • 48. Computer Science: A Structured Programming Approach Using C 48 PROGRAM 7-6 Student Grades
  • 49. Computer Science: A Structured Programming Approach Using C 49 PROGRAM 7-6 Student Grades
  • 50. Computer Science: A Structured Programming Approach Using C 50 PROGRAM 7-6 Student Grades