Solutions to Chapter 7

Similar documents
File IO and command line input CSE 2451

Organization of a file

File I/O. Arash Rafiey. November 7, 2017

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Standard File Pointers

C programming basics T3-1 -

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

C Basics And Concepts Input And Output

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Standard C Library Functions

File (1A) Young Won Lim 11/25/16

CSI 402 Lecture 2 Working with Files (Text and Binary)

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

File I/O Lesson Outline

C mini reference. 5 Binary numbers 12

File I/O Lesson Outline

Introduction to file management

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

CS240: Programming in C

PROGRAMMAZIONE I A.A. 2017/2018

System Software Experiment 1 Lecture 7

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Input / Output Functions

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Lecture 9: File Processing. Quazi Rahman

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

fopen() fclose() fgetc() fputc() fread() fwrite()

Basic and Practice in Programming Lab 10

2009 S2 COMP File Operations

CE Lecture 11

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

ENG120. Misc. Topics

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

C PROGRAMMING. Characters and Strings File Processing Exercise

M.CS201 Programming language

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Fundamentals of Programming & Procedural Programming

EM108 Software Development for Engineers

Input/Output: Advanced Concepts

Goals of this Lecture

CS1003: Intro to CS, Summer 2008

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Ch 11. C File Processing (review)

Programming & Data Structure

Lab # 4. Files & Queues in C

Advanced C Programming Topics

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

The Design of C: A Rational Reconstruction (cont.)

Programming Fundamentals

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

8. Characters, Strings and Files

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Control Structure: Loop

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Chapter 14 - Advanced C Topics

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

A Crash Course in C. Steven Reeves

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

EE458 - Embedded Systems Lecture 4 Embedded Devel.

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

WARM UP LESSONS BARE BASICS

Chapter 10. File Processing 248 FILE PROCESSING

Preview from Notesale.co.uk Page 2 of 79

Computer programming

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

C introduction: part 1

Chapter 12. Files (reference: Deitel s chap 11) chap8

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

Input/Output and the Operating Systems

Computer Programming: Skills & Concepts (CP) Files in C

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

DATA STRUCTURES USING C

C File Processing: One-Page Summary

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

SAE1A Programming in C. Unit : I - V

Pointers and File Handling

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Lecture6 File Processing

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

Chapter 11 File Processing

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Naked C Lecture 6. File Operations and System Calls

LSN 3 C Concepts for OS Programming

Transcription:

Solutions to Chapter 7 Review Questions 1. a. True 3. b. False 5. a. True 7. c. stdfile 9. a. Opens the file for reading and sets the file marker at the beginning. 11. a. Conversion code 13. a. scanf implicitly casts data to match the conversion code and its corresponding address parameter 15. e. Terminates the scanf function and leaves the invalid character in the input stream. Exercises 17. i1 = 14 i2 = 67 f1 = 67.9 c1 = '.' c2 = 2 19. 000123, -00234, -00007-234, %, ", \t, 123 A 10 a H 0xf Problems 21. See Program 7-1. Program 7-1 Solution to Problem 21 /* ==================== appendfile ==================== This function appends one file to the other. Pre Files exist Post File 2 appended to file 1. Returns 0 if successful 1 if file 1 cannot be opened 2 if file 2 cannot be opened int appendfile (const char* file1, const char* file2) char c; FILE* sp2; if (!(sp1 = fopen (file1, "a"))) printf ("\nerror opening %s for appending.\n", file1); } // if 31

32 Solutions to Chapter 7 Program 7-1 Solution to Problem 21 (continued) if (!(sp2 = fopen (file2, "r"))) printf ("\nerror opening %s for reading.\n", file2); } // if while ((c = fgetc (sp2))!= EOF) fputc (c, sp1); } // appendfile 23. See Program 7-2. Program 7-2 Solution to Problem 23 /* ==================== formatline ==================== This function reformats a file to 60 characters per line. Pre File exists Post File reformatted 60 characters per line int formatline (const char* filename) char c; char* tempfile = "TEMP.DAT"; int count = 0; FILE* sp; FILE* sptemp; if (!(sp = fopen (filename, "r"))) printf ("\nerror opening %s for reading.\n", filename); if (!(sptemp = fopen (tempfile, "w"))) printf ("\nerror opening %s for writing.\n", tempfile); while ((c = fgetc (sp))!= EOF) if (count == 60) fputc ('\n', sptemp); count = 0; } // if count if (c!= '\n') fputc (c, sptemp); count++; } // if not \n fputc ('\n', sptemp);

Problems 33 Program 7-2 Solution to Problem 23 (continued) fclose (sp); fclose (sptemp); if (!(sp = fopen (filename, "w"))) printf ("\nerror opening %s for writing.\n", filename); if (!(sptemp = fopen (tempfile, "r"))) printf ("\nerror opening %s for reading.\n", tempfile); while ((c = fgetc (sptemp))!= EOF) fputc (c, sp); fclose (sp); fclose (sptemp); } // formatline 25. See Program 7-3. Program 7-3 Solution to Problem 25 /* ==================== dellastline =================== This function deletes the last line of any file. Pre File exists Post Last line of file deleted. int dellastline (const char* filename) char c; char* tempfile = "TEMP.DAT"; int line_count = 1; int total_lines = 1; FILE* sp; FILE* sptemp; if (!(sp = fopen (filename, "r"))) printf ("\nerror opening %s.\n", filename); if (!(sptemp = fopen (tempfile, "w"))) printf ("\nerror opening %s.\n", tempfile); // count the number of lines and build temp file while ((c = fgetc (sp))!= EOF) if (c == '\n') total_lines++; fputc(c, sptemp); fclose (sp);

34 Solutions to Chapter 7 Program 7-3 Solution to Problem 25 (continued) fclose (sptemp); if (!(sp = fopen (filename, "w"))) printf ("\nerror opening %s for writing.\n", filename); if (!(sptemp = fopen (tempfile, "r"))) printf ("\nerror opening %s for reading.\n", tempfile); // Write all but last line to the original file. while ((c = fgetc (sptemp))!= EOF && line_count < total_lines - 1) fputc (c, sp); if (c == '\n') line_count++; fclose (sp); fclose (sptemp); } // dellastline 27. See Program 7-4. Program 7-4 Solution to Problem 27 /* This program prints itself. char c; FILE* sp; if (!(sp = fopen ("P07-27.c", "r"))) printf ("\nerror opening P07-27.c.\n"); while ((c = fgetc(sp))!= EOF) printf ("%c", c); fclose (sp); printf ("End of Program\n\n");

Problems 35 29. See Program 7-5. Program 7-5 Solution to Problem 29 /* This program copies only the line started with a specified character. #define FILE_1 "FILE1.DAT" #define FILE_2 "FILE2.DAT" char target; char cur; char pre = '\n'; int writeln = 0; FILE* sp2; printf ("Please enter the target character: "); scanf ("%c", &target); if (!(sp1 = fopen (FILE_1, "r"))) printf ("\nerror opening %s.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "w"))) printf ("\nerror opening %s.\n", FILE_2); while ((cur = fgetc(sp1))!= EOF) if (!writeln && pre == '\n' && cur == target) writeln = 1; if (writeln) fputc (cur, sp2); if (writeln && cur == '\n') writeln = 0; pre = cur; printf ("\nend of Program\n"); 31. See Program 7-6. Program 7-6 Solution to Problem 31 /* ==================== handleerror =================== This function reads 3 pieces of numeric data, and checks if the input is correct or not. Pre nothing

36 Solutions to Chapter 7 Program 7-6 Solution to Problem 31 (continued) Post Valid input printed #define FLUSH while (getchar()!= '\n') void handleerror (void) int num1; int num2; int num3; printf ("\nplease enter 3 integers: "); while (scanf ("%d %d %d", &num1, &num2, &num3) < 3) FLUSH; printf ("\ninvalid input. Please re-enter: "); printf ("\nthe 3 input data is %d, %d, %d\n", num1, num2, num3); return; } // handleerror 33. See Program 7-7. Program 7-7 Solution to Problem 33 /* This program deletes the sixth line in a file. #define FILE_1 "FILE1.DAT" #define FILE_2 "TEMP.DAT" char ch; int line_cnt = 0; FILE* sp2; if (!(sp1 = fopen (FILE_1, "r"))) printf ("\nerror opening %s.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "w"))) printf ("\nerror opening %s.\n", FILE_2); while ((ch = fgetc(sp1))!= EOF) if (ch == '\n') line_cnt++; if (line_cnt!= 5) fputc (ch, sp2);

Problems 37 Program 7-7 Solution to Problem 33 (continued) if (!(sp1 = fopen (FILE_1, "w"))) printf ("\nerror opening %s for writing.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "r"))) printf ("\nerror opening %s for reading.\n", FILE_2); while ((ch = fgetc (sp2))!= EOF) fputc (ch, sp1); printf ("\nend of Program\n"); 35. See Program 7-8. Program 7-8 Solution to Problem 35 /* This program duplicates the fourth line in a file. #define FILE_1 "FILE1.DAT" #define FILE_2 "TEMP1.DAT" #define FILE_3 "TEMP2.DAT" char c; int line_cnt = 0; FILE* sp2; FILE* sp3; if (!(sp1 = fopen (FILE_1, "r"))) printf ("\nerror opening %s.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "w"))) printf ("\nerror opening %s.\n", FILE_2);

38 Solutions to Chapter 7 Program 7-8 Solution to Problem 35 (continued) if (!(sp3 = fopen (FILE_3, "w"))) printf ("\nerror opening %s for writing.\n", FILE_3); return (3); while ((c = fgetc(sp1))!= EOF) if (c == '\n') line_cnt++; if (line_cnt == 3) fputc (c, sp3); if (line_cnt == 4 && c == '\n') fclose (sp3); if (!(sp3 = fopen (FILE_3, "r"))) printf ("\nerror 4 opening %s.\n", FILE_3); return (4); } // if while ((c = fgetc(sp3))!= EOF) fputc (c, sp2); c = '\n'; fclose(sp3); } // if line_cnt fputc (c, sp2); if (!(sp1 = fopen (FILE_1, "w"))) printf ("\nerror opening %s for writing.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "r"))) printf ("\nerror opening %s for reading.\n", FILE_2); while ((c = fgetc (sp2))!= EOF) fputc (c, sp1); printf ("\nend of Program\n"); 37. See Program 7-9. Program 7-9 Solution to Problem 37 /* This program copies a file, inserting two space characters at the beginning of each line.

Problems 39 Program 7-9 Solution to Problem 37 (continued) #define FILE_1 "FILE1.DAT" #define FILE_2 "FILE2.DAT" char cur; char pre = '\n'; FILE* sp2; if (!(sp1 = fopen (FILE_1, "r"))) printf ("\nerror opening %s.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "w"))) printf ("\nerror opening %s.\n", FILE_2); while ((cur = fgetc(sp1))!= EOF) if (pre == '\n') fputc (' ', sp2); fputc (' ', sp2); } // if fputc (cur, sp2); pre = cur; printf ("\nend of Program\n"); 39. See Program 7-10. Program 7-10 Solution to Problem 39 /* This program writes the odd numbers between 300 and 500 to a text file. #define FILE_1 "FILE1.DAT" int number; if (!(sp1 = fopen (FILE_1, "w")))

40 Solutions to Chapter 7 Program 7-10 Solution to Problem 39 (continued) printf ("\nerror opening %s.\n", FILE_1); for (number = 301; number < 500; number += 2) fprintf (sp1, "%d\n", number); printf ("\nend of Program\n"); 41. See Program 7-11. Program 7-11 Solution to Problem 41 /* This program reads a file of scores and creates a new file of all of the scores over 90. #define FILE_1 "FILE1.DAT" #define FILE_2 "FILE2.DAT" int score; int count = 0; FILE* sp2; if (!(sp1 = fopen (FILE_1, "r"))) printf ("\nerror opening %s.\n", FILE_1); if (!(sp2 = fopen (FILE_2, "w"))) printf ("\nerror opening %s.\n", FILE_2); while ((fscanf(sp1, "%d", &score))!= EOF) if (score >= 90) fprintf (sp2, "%d\n", score); count++; } // if printf ("\nthere were %d scores over 90.\n", count); printf ("\nend of Program\n");

Problems 41

42 Solutions to Chapter 7