C/C++ Text File Functions

Size: px
Start display at page:

Download "C/C++ Text File Functions"

Transcription

1 1

2 2

3 3

4 C/C++ Text File Functions fopen opens a text file. fclose closes a text file. feof detects end-of-file marker in a file. fscanf reads formatted input from a file. fprintf prints formatted output to a file. fgets reads a string from a file. fputs prints a string to a file. fgetc reads a character from a file. fputc prints a character to a file. fflush empties the buffer 4

5 Review Practice 1 5

6 Review Practice 2 6

7 Standard Text Files - Program A Mode w erases, the file if it exits! 7

8 Read From Text File 8

9 9

10 10

11 11 11

12 12

13 Some Applications Require More Than 24 Hours To Read A Single File! 13 13

14 Text File Read Processing Open File Read & Write To Sought Data Repeat! Read Some Applications Require More Than 24 Hours To Read A Single File! Read Sought Data Sought Data 14 Read #1 Read #2 14

15 Some Applications Require More Than 24 Hours To Read A Single File! 15 15

16 Text File Change Processing Open Original & Destination Files Read Original Write Destination [up to point of change] Write Change Data Read Original Write Destination [remaining data] Erase Original File & Rename Destination File Read & Write Change Data Changed Data Read & Write Change #1 16

17 17

18 DA File Read Processing Open File Seek & Read Data Repeat! Can Often Be Done In Less Than 1/10 Second Hardware Dependent Sought Data Sought Data 18 Read #1 Read #2 18

19 DA Change Processing Open Original Read Original Write Destination [up to point of change] Write Change Data Read Make Change Change Data Write Change #1 19

20 20

21 21

22 Text Files The Choice Is Yours! Direct Access Files 22

23 23

24 Create Three New Direct Access Files File Ptrs Create File Close File Execute This Program? 24

25 What Happened As A Result Of Executing This Program? Three Files Were Created? Where? Find Them! 25

26 How Big Are The Files? How Do You Know? Right Mouse Click on the file and select Properties! 26

27 Dir ls 27 27

28 Dir Wild Cards * Dir *.fil ls *.fil 28

29 About fopen StudentFil = fopen("student.fil", "wb+"); Opening a file in the wb+ mode checks to see if there is a file by the name Student.Fil: If so, it erases the contents & positions the read/write pointer at the beginning of the empty file If not, it creates the file & positions the read/write pointer at the beginning of the empty file 29

30 fopen Modes Direct Access Files DAF s 30

31 fclose(studentfil); All files are buffered. About fclose The buffer size can be set by the operating system and or the programming language. To reduce wear and tear on the hard drives, most operating systems write information to a buffer when the buffer is full, it is dumped to disk. When a file is closed, the information, remaining in the buffer, is written to disk and the file allocation table is updated to reflect changes in the file. Failure to close a file can result in lost information. 31

32 32

33 About fseek fseek(fileptr, NoBytes, SEEK_SET); SEEK_SET is a constant representing the beginning of the file. SEEK_END is a constant representing the end of the file. fseek(studentfil, 4 * sizeof(student), SEEK_SET); The size of each Student record is 36 Bytes. Move the Read-Write Pointer 4 * 36 = 144 bytes from the beginning of the file referenced by the StudentFil ptr. Valid Records To Seek Are 0 N+1 33

34 fseek Examples fseek(studentfil, 0 * sizeof(student), SEEK_SET); fseek(studentfil, 0, SEEK_SET); Move the Read-Write Pointer to the beginning of the file [First Record] fseek(studentfil, (N+1) * sizeof(student), SEEK_SET); fseek(studentfil, 0 * sizeof(student), SEEK_END); fseek(studentfil, 0, SEEK_END); Move the Read-Write Pointer to the end of the file [EOF Marker] to append a new record. 34

35 35

36 About fwrite fwrite(&info, InfoSize(byes), # records, fp); fwrite(&newstudent, sizeof(student),(long)1,studentfil); Write the 1 record of information referenced by NewStudent, whose size is 36 Bytes, to the current Read-Write Location of the StudentFil. Student Students[20] fwrite(&students,sizeof(student),(long)20,studentfil); Write the 20 records of information referenced by array Students to the current Read-Write Location of the StudentFil. FWRITE can be used to write a single record or a block of records. 36

37 Suppose the Output Buffer holds only two records. Student LocalYear; fwrite Examples fseek(studentfil, 3 * sizeof(student), SEEK_SET); fwrite(&localyear,sizeof(student),(long)1,studentfil); The information is actually written to the buffer when the buffer is full or location is changed, it is written to disk. The buffer size can be set 0 to force immediate writes! 37

38 38

39 Review -Valid Writes For A File With 3 Records Replace Record 0 fseek(studentfil, 0 * sizeof(student), SEEK_SET); fwrite(&newstudent,sizeof(student),(long)1,studentfil); Replace Record 1 fseek(studentfil, 1 * sizeof(student), SEEK_SET); fwrite(&newstudent,sizeof(student),(long)1,studentfil); Replace Record 2 fseek(studentfil, 2 * sizeof(student), SEEK_SET); fwrite(&newstudent,sizeof(student),(long)1,studentfil); Append/Add New Record 3 fseek(studentfil, 3 * sizeof(student), SEEK_SET); fwrite(&newstudent,sizeof(student),(long)1,studentfil); 39

40 Change To Compile The Program! 40

41 Did It Do Anything? How Do You Know? Yes It Did Something! The File Is 4 Bytes Bigger Than It Was. Did It Do The Right Thing? How Do You Know? We Don t! How Could You Know? Read The File? 41

42 42

43 Create File Open To Read Or Write File Ptrs Open An Existing File To Read Or Write Execute This Program? 43

44 44

45 About fseek fseek(fileptr, NoBytes, SEEK_SET); SEEK_SET is a constant representing the beginning of the file. SEEK_END is a constant representing the end of the file. Can Do Have Already Done! fseek(studentfil, 4 * sizeof(student), SEEK_SET); The size of each Student record is 36 Bytes. Move the Read-Write Pointer 4 * 36 = 144 bytes from the beginning of the file referenced by the StudentFil ptr. 45

46 46

47 fread(&info, InfoSize(byes), # records, fp); fread(&newstudent,sizeof(student),(long)1,studentfil); Read the 1 36-byte record of information, pointed to by the current Read-Write Pointer of the StudentFil, into the NewStudent container. Student Students[20] About fead fread(&students,sizeof(student),(long)20,studentfil); Read twenty 36-byte records of information, pointed to by the current Read-Write Pointer of the StudentFil, into array Students. FREAD can be used to write a single record or a block of records. 47

48 Suppose the Input Buffer holds only two records. Student LocalYear; fseek(studentfil, 2 * sizeof(student), SEEK_SET); fread(&localyear,sizeof(student),(long)1,studentfil); Note that both Record 2 and Record 3 are in the Input Buffer. If the program logic were to need to read either of these records, they would be retrieved from the buffer as opposed to reading them from disk Much Faster [Nanosecond vs. Millisecond] 48

49 49

50 Valid Reads For A File With 3 Records Read Record 0 fseek(studentfil, 0 * sizeof(student), SEEK_SET); fread(&newstudent,sizeof(student),(long)1,studentfil); Read Record 1 fseek(studentfil, 1 * sizeof(student), SEEK_SET); fread(&newstudent,sizeof(student),(long)1,studentfil); Read Record 2 fseek(studentfil, 2 * sizeof(student), SEEK_SET); fread(&newstudent,sizeof(student),(long)1,studentfil); 50

51 Change To Compile The Program! 51

52 Did It Do The Right Thing? Absolutely! But Will It Work For Multiple Records? 52

53 Change Compile The Program! 53

54 Wonder What The File Looks Like With A Text Editor? 54

55 Binary Files Provide Some Additional Security In That They Are Less Readable Than Text Files 55

56 Did It Do The Right Thing? Absolutely! But Does rb+ Really Allow Reads & Writes? 56

57 Add This To The Program Compile The Program! 57

58 Will This Really Work With Class Datatypes? 58

59 59

60 Change To + + Compile The Program! 60

61 Works For One Student More Extensive Auto Testing! 61

62 One 36-Byte Record 62

63 Change To Compile The Program! 63

64 64

65 Eight 36-Byte Records 65

66 66

67 Open An Existing Direct Access File FILE * AutoFil, * StudentFil; StudentFil = fopen("student.fil", "rb+"); AutoFil = fopen("auto.fil", "rb+"); 67

68 Open A New Direct Access File & Write A Record To Record 0 And A Record To Record 1 FILE Student * StudentFil; NewStudent; StudentFil = fopen("student.fil", "rb+"); if (NewStudent.Get()) fwrite(&newstudent,sizeof(student),(long)1,studentfil); if (NewStudent.Get()) fwrite(&newstudent,sizeof(student),(long)1,studentfil); fclose(studentfil); 68

69 Open A Existing Access File Student.Fil (Has Two Records) & Add A Third Record` FILE Student * StudentFil; NewStudent; Move To Record 2 Third Record 0,1,2 StudentFil = fopen("student.fil", "rb+"); fseek(studentfil, 2 * sizeof(student), SEEK_SET); if (NewStudent.Get()) fwrite(&newstudent,sizeof(student),(long)1,studentfil); fclose(studentfil); 69

70 Open A Existing Access File Student.Fil (Has Three Records) Display All 3 Records In Reverse Order int FILE Student Counter; * StudentFil; NewStudent; StudentFil = fopen("student.fil", "rb+"); for (Counter = 2; Counter >= 0; Counter --) { } fseek(studentfil, Counter * sizeof(student), SEEK_SET); fread(&newstudent,sizeof(student),(long)1,studentfil); NewStudent.Display(); fclose(studentfil); Move To Record 2, 1, 0 Respectively 70

71 Replace Record 3 [Fourth Record] With NewStudent Student FILE NewStudent; * StudentFil; StudentFil = fopen("student.fil", "rb+"); if (NewStudent.Get() == VALID) fwrite(&newstudent,sizeof(student),(long)3,studentfil); fclose(studentfil); 71

72 72

73 Generic FileLength Function long int Filelength (FILE * FilePtr, long int NoBytesPerRecord) { long int Bytes; fseek (FilePtr, 0L, SEEK_END); Bytes = ftell (FilePtr); return (Bytes / NoBytesPerRecord); } Belongs in Utilities.cpp Update It Now! int FILE Counter; * StudentFil; Test It Now! NoRecords = FileLength(StudentFil, sizeof(student)); 73

74 Generic WriteRecord Belongs in Utilities.hpp ////////////////////////////////////////////////////////////////////////////////// Update It Now! ////////////////////////////////////////////////////////////////////////////////// // // // WriteRecord // // // // Purpose : Write the direct access record RecordNo from file *fp from // // container Info. No error checking yet! // // // // Written By : Dr. Thomas E. Hicks Environment : Windows 2000 // // Date : XX/XX/XX Compiler : Visual C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// template <class InfoType> bool WriteRecord(InfoType *Info, long int RecordNo, FILE *fp) { fseek(fp, RecordNo * sizeof(infotype), SEEK_SET); fwrite(info, (long)sizeof(infotype), 1L, fp); return (SUCCESSFUL); } 74

75 Test Generic WriteRecord Student FILE long int NewStudent; * StudentFil; Counter = 0; Test It Now! StudentFil = fopen("student.fil", "wb+"); while (NewStudent.Get() == VALID) fwrite(&newstudent,sizeof(student),counter++,studentfil); fclose(studentfil); Not A Great Solution Does Not Trap Errors 75 Never Returns UNSUCCESSFUL See Chapter 11! 75

76 Generic ReadRecord Belongs in Utilities.hpp Update It Now! ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // // // ReadRecord // // // // Purpose : Read the direct access record RecordNo from file *fp into // // container Info. No error checking yet! // // // // Written By : Dr. Thomas E. Hicks Environment : Windows 2000 // // Date : XX/XX/XX Compiler : Visual C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// template <class InfoType> bool ReadRecord(InfoType *Info, long int RecordNo, FILE *fp) { fseek(fp, RecordNo * sizeof(infotype), SEEK_SET); fread(info, (long)sizeof(infotype), 1L, fp); return (SUCCESSFUL); } 76

77 Test Generic ReadRecord File Exists From Test Of WriteRecord Student NewStudent; FILE * StudentFil; long int Counter, NoRecords; Test It Now! StudentFil = fopen("student.fil", "rb+"); NoRecords = FileLength(StudentFil, sizeof(student)); for (Counter = NoRecords - 1; Counter >=0; Counter --) if (ReadRecord(&NewStudent,Counter,StudentFil)) NewStudent.Display(); fclose(studentfil); Not A Great Solution Does Not Trap Errors 77 Never Returns UNSUCCESSFUL See Chapter 11! 77

78

79 Almost Every Application Starts With Some Data You Need To Manage

80 class Part { public: You Do A Systems Analysis & Determine What Data Is Needed You Create Your Base Class private: char Name [26]; long int No, Quantity, DeptNo; double Cost; //... };

81 Your System Analysis Will Help You Determine The Necessary Support Functions Accessor & Mutator Functions class Part { public: Part (char NewName[] = "", long int NewNo = 0, long int NewDeptNo = 0, long int NewQuantity = 0, double NewCost = 0.0); ~Part (void); void Set (char NewName[] = "", long int NewNo = 0, long int NewDeptNo = 0, long int NewQuantity = 0, double NewCost = 0.0); bool Get(void); long int Key(void); long int No_(void); double Cost_(void); long int Quantity_(void); long int DeptNo_(void); private: char long int double }; Name [26]; No, Quantity, DeptNo; Cost;

82 Your Specifications Will Almost Always Dictate Your Operator Overload Design I always do overload ostream & operator << for diagnostic testing friend ostream & operator << (ostream & OutputStream, Part P); // I have decided that Name is to be the Primary Key for this record type. bool operator == (const Part & P); bool operator > (const Part & P); bool operator >= (const Part & P); bool operator < (const Part & P); bool operator <= (const Part & P); bool operator!= (const Part & P); If the Specificationswant reports that are in order by the Part.Name I will have this collection void operator = (const Part & P); Required Only For Deep Copy // I have decided that Name is to be the Primary Character Key for this record type. bool operator == (const char Key[]); bool operator > (const char Key[]); bool operator >= (const char Key[]); bool operator < (const char Key[]); bool operator <= (const char Key[]); bool operator!= (const char Key[]); void operator = (const char Key[]); If the Specificationswant are going to search, or compare, the base class with a character string, I will have this collection // I have decided that No is to be the Primary Integer Key for this record type. bool operator == (const long int Key); bool operator > (const long int Key); bool operator >= (const long int Key); bool operator < (const long int Key); bool operator <= (const long int Key); bool operator!= (const long int Key); void operator = (const long int Key); If the Specificationswant are going to search, or compare, the base class with a long integer, I will have this collection There are ways to do generics with more than one char string or integer.

83

84 Most Systems Should Have Some Written Requirements/Specifications The user will be able to search 1,000,000 records by Part.No and display the sought record in no more than.5 seconds. The user will be able to add a new part to 999,999 others in no more that 1.0 seconds.

85 Our Requirements Almost Always Require The Following Functionality

86 Application Specifications Have To Use A File

87 To Begin, We Will Have To Determine How Long It Takes To Read/Write On The Hardware Being Used For The Project: Our Results Will Not Be 100% Accurate - But They Will Certainly Provide Us Some Degree Of Magnitude & Confidence In the beginning, we may not know all the fields or all the functions. printf("sizeof(part) = %ld\n", sizeof(part)); sizeof(part) = 48 class Part { public: private: char Name [26]; long int No, Quantity, DeptNo; double Cost; //... };

88 I Am Choosing To Create A Class BigPart 20,000 Bytes class BigPart { public: printf("sizeof(bigpart) = %ld\n", sizeof(bigpart)); sizeof(part) = private: char Name [26]; long int No, Quantity, DeptNo; double Cost; char Satellite1[19946]; }; This is all we need to determine Approximate Read/Write Times

89

90 To Calculate Average Search Time For A Computer - 1 We Can create a direct access file with 1,000,000 Records 20,000 Bytes Each the content (x) does not matter. 1,000, , , , x x x x 4 x 3 x 2 x 1 x 0 1,000,000 Direct Access File ActNo Of Valid Records

91 To Calculate Average Search Time For A Computer - 2 We can create an internal memory array whose integer data members are 1 1,000,000 randomly mixed. 1,000, , , , x x x x 1,000, , , , ,124 4 x 4 2, Direct Access File x x x , ,412 Internal Memory Array

92 To Calculate Average Search Time For A Computer - 3 Start the clock For I = 1 To 1,000,000 ReadRecord(&P, A[I], fp) 1,000, , , , ,124 Read record # 34,412 Read Record # 8 Read Record # 23,311 Read Record # 2, Read Record # Read Record # 65 Read Record # 874 Read Record # 2 4 2, , ,412 A # Seconds Average Search = # Records Stop the clock Compute The Total # Seconds

93

94

95

96 To Calculate Average Search Time For A Computer - 1 We Can create a direct access file with 1,000,000 Records 20,000 Bytes Each the content (x) does not matter. Normal file processing includes Searching, Adding, and Deleting Records. 1,000, , , ,997 x x x x Deleted records will be stored at the end of the file. Record 0 is a convenient place to store ActNo. 4 x Direct Access File x x x 1,000,000 It may be the case that file has 101 records; record 0 contains 95 because the last 5 records represent deleted records. ActNo Of Valid Records

97 Assume We Have 1,000,000 Valid Data Items Assume That For Our Hardware & Data Item Size We Can Read/Write 200 Data Items Per Second D A 1,000,000 F I L E 1

98

99 N = 1,000, R/W Per Sec - Unorderd Direct Access File 1,000,000 D A F I L E 1,000, Sequential Search Read 1,000,000 Records 1,000,000/200 = 5,000 Seconds We Probably Would Keep The ActNo In Memory And Would Not Have To Read It In Order To Search +/- 1 Will Not Matter!

100 N = 1,000, R/W Per Sec - Unorderd Direct Access File 1,000,000 D A F I L E 500,000 Sequential Search Read 1,000,000/2 = 500,000 Records 500,000/200 = 2,500 Seconds 1

101

102 N = 1,000, R/W Per Sec - Unorderd Direct Access File W D A F I L E 999, ,000, ,000 0 Append 1 Record To End Of File - 1 Write 1/200 =.005 Seconds Update Record 0 1 Write 1/200 =.005 Seconds We Probably Would Keep The ActNo In Memory And Would Not Have To Read It In Order To Search

103 N = 1,000, R/W Per Sec - Unorderd Direct Access File W 999,999 D A Append 1 Record To End Of File - 1 Write 1/200 =.005 Seconds F I L E 1 1,000, ,000 0 Update Record 0 1 Write 1/200 =.005 Seconds We Probably Would Keep The ActNo In Memory And Would Not Have To Read It In Order To Search

104

105 N = 1,000, R/W Per Sec - Unorderd Direct Access File R D A F I L E 1,000, ,997 1 W Read Last Valid Record (1,000,000) 1/200 =.005 Seconds Write Last Rec To Record 999,997 1/200 =.005 Seconds Update Record 0 =.005 Seconds 1,000, ,000 We Probably Would Keep The ActNo In Memory And Would Not Have To Read It In Order To Search

106 N = 1,000, R/W Per Sec - Unorderd Direct Access File R 1,000,000 D A F I L E W 1 Worst Search = 5,000 Seconds Remember The Read Of The Last Valid Record (1,000,000) Write Last Rec To Record 1 1/200 =.005 Seconds 999,000 Update Record 0 =.005 Seconds 1,000,000 0

107 N = 1,000, R/W Per Sec - Unorderd Direct Access File R D A F I L E 1,000, ,997 1 W Read Last Valid Record (1,000,000) 1/200 =.005 Seconds Write Last Rec To Record 999,997 1/200 =.005 Seconds Update Record 0 =.005 Seconds 1,000, ,000 We Probably Would Keep The ActNo In Memory And Would Not Have To Read It In Order To Search

108 N = 1,000, R/W Per Sec - Unorderd Direct Access File R 1,000,000 D A F I L E W 1 Average Search = 2,500 Seconds Remember The Read Of The Last Valid Record (1,000,000) Write Last Rec To Record 1 1/200 =.005 Seconds 999,000 Update Record 0 =.005 Seconds 1,000,000 0

109 Can t Satisfy These Requirements Specifications With One UnOrdered Array The user will be able to search 1,000,000 records by Part.No and display the sought record in no more than.5 seconds. The user will be able to add a new part to 999,999 others in no more that 1.0 seconds.

110

111 On Exam/Quiz, You May Calculate Log 2 (N) 111

112 , , , , , , ,536 If There Are N Nodes At This Level Of A Complete Tree, There Are N-1 Nodes Above This Level Ball Park = ~16 112

113 Computer A Search 1 Record Ordered/Sorted Direct Access Files Ordered List ~16.6 sec 113

114 Computer A Search 1 Record Ordered/Sorted Direct Access Files Ordered List ~ sec -1?? 114

115 To Compute Binary Exact? 1 Log 2 (15) = 2.8 = = = 12 Total Searches = 17 Avr Search = 17/7 =

116 To Compute Binary Exact? 1 Log 2 (15)= 3.9 = = = = 32 Total Searches = 49 Avr Search = 49/15 =

117 To Compute Binary Exact? 4.9 Log 2 (31)= = 1 = 4 = 12 = 32 = 80 Total Searches = 129 Avr Search = 129/31 =

118 118 Complete Binary Tree or Binary Search Worst Search Log 2 (N) Reads Ave Search Log 2 (N)-1 Reads 118

119 Design Thought Ready For A Sorted/ Ordered Direct Access File 119

120 Suppose We Have 7 Items In Ordered File We Delete There Will Be Times When Not All Of The Records Are Used - Record 0 Provides A Great Place To Store The Actual Number Of Valid Records - We Will Have To Keep This Number Accurate! I Will Not Include It In All Of The Sketches! 120

121

122

123 N = 1,000, R/W Per Sec - Orderd Direct Access File 1,000,000 R etc Binary Search Read 20 Records 20/200 =.1 Seconds R Mid 1 1,000,000 0

124 N = 1,000, R/W Per Sec - Orderd Direct Access File 1,000,000 R etc Binary Search Read 20-1 Records 19/200 =.095 Seconds R Mid 1 1,000,000 0

125

126 N = 1,000, R/W Per Sec - Orderd Direct Access File 999,000 R etc R Mid We Could Search For The Location INEFFICIENT! 1 999,000 0

127 N = R/W Per Sec - Orderd Direct Access File Examine The Algorithm! Search Would Do No Good! New W D A F I L E W 5 R Read Rec W W New W 56 Write To Rec 6 R Read Rec 4 Write To Rec 5 R Read Rec 3 Write To Rec 4 R Read Rec 2 Write To Rec 3 W 12/200 =.06 Seconds R Read Rec 1 Write New To 1 Write To Rec 2 Update Rec 0 600

128 N = 1,000, R/W Per Sec - Orderd Direct Access File New 50 2,000,000/200 = 10,000 Seconds Read 999,999 Records Write 999,999 Records Write New Record To 1 Update Record 0 999,000

129 N = 1,000, R/W Per Sec - Orderd Direct Access File New 50 1,000,001/200 = 5, Sec Read 500,000 Records Write 499,999 Records Write New Record To 1 Update Record 0 999,000

130

131 N = 1,000, R/W Per Sec - Orderd Direct Access File 1,000,000 D A F I L E 1 2,000,00 = 10,000.0 Seconds Read 1,000,000 Records Write 999,999 Records Update Record 0 1,000,000

132 N = 1,000, R/W Per Sec - Orderd Direct Access File 1,000,000 D A F I L E 1,000, ,000,020 = 10, Seconds Worst Case Binary Search Read 20 Records Read 1,000,000 Records Write 999,999 Records Update Record 0

133 N = 1,000, R/W Per Sec - Orderd Direct Access File 1,000,000 D A F I L E 1 1,000,000 = 5,000 Seconds Read 500,000 Records Write 499,999 Records Update Record 0 1,000,000

134 N = 1,000, R/W Per Sec - Orderd Direct Access File 1,000,000 D A F I L E 1 1,000,019 = 5, Seconds Average Case Binary Search Read 19 Records Read 500,000 Records Write 499,999 Records Update Record 0 1,000,000

135

136 N = 1,000, R/W Per Sec - Unorderd Direct Access File 1,000,000 D A F I L E 1,000, ,000,000 x 1,000,000 = 1,000,000,000,000 1,000,000,000,000/200 = 5,000,000,000 Sec 5,000,000,000/60 = 83,333,333 Minutes 83,333,333/60 = ~1,388,889 Hours 1,388,889/24 = ~57,870 Days

137 N = 1,000, R/W Per Sec - Unorderd Direct Access File 1,000,000 D A F I L E 1,000, ,000,000 x 20 = 20,000,000 20,000,000/200 = 100,000 Seconds 100,000/60 = 1,667 Minutes 1,667 /60 = ~27.78 Hours 27.78/24 = ~1.16 Days

138 Can t Satisfy These Requirements Specifictions With One Ordered Array The user will be able to search 1,000,000 records by Part.No and display the sought record in no more than.5 seconds. The user will be able to add a new part to 999,999 others in no more that 1.0 seconds.

139 We Need Better Solutions! Keep Coming To Class! 139

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

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL. Files Files enable permanent storage of information C performs all input and output, including disk files, by means of streams Stream oriented data files are divided into two categories Formatted data

More information

Lecture 9: File Processing. Quazi Rahman

Lecture 9: File Processing. Quazi Rahman 60-141 Lecture 9: File Processing Quazi Rahman 1 Outlines Files Data Hierarchy File Operations Types of File Accessing Files 2 FILES Storage of data in variables, arrays or in any other data structures,

More information

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

UNIT IV-2. The I/O library functions can be classified into two broad categories: UNIT IV-2 6.0 INTRODUCTION Reading, processing and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

CSI 402 Lecture 2 (More on Files) 2 1 / 20

CSI 402 Lecture 2 (More on Files) 2 1 / 20 CSI 402 Lecture 2 (More on Files) 2 1 / 20 Files A Quick Review Type for file variables: FILE * File operations use functions from stdio.h. Functions fopen and fclose for opening and closing files. Functions

More information

Standard C Library Functions

Standard C Library Functions Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

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

CSI 402 Lecture 2 Working with Files (Text and Binary) CSI 402 Lecture 2 Working with Files (Text and Binary) 1 / 30 AQuickReviewofStandardI/O Recall that #include allows use of printf and scanf functions Example: int i; scanf("%d", &i); printf("value

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 INPUT/OUTPUT INPUT AND OUTPUT Programs must be able to write data to files or to physical output devices such as displays or printers, and to read in data from files or

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 4 Files page 1 of 14 EM108 Software Development for Engineers Section 4 - Files 1) Introduction 2) Operations with Files 3) Opening Files 4) Input/Output Operations 5) Other Operations 6)

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e Storage of data in variables and arrays is temporary such data is lost when a program terminates. Files are used for permanent retention of data. Computers store files on secondary

More information

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS A mini Quiz 2 Consider the following struct definition struct name{ int a; float b; }; Then somewhere in main() struct name *ptr,p; ptr=&p;

More information

Week 9 Lecture 3. Binary Files. Week 9

Week 9 Lecture 3. Binary Files. Week 9 Lecture 3 Binary Files 1 Reading and Writing Binary Files 2 Binary Files It is possible to write the contents of memory directly to a file. The bits need to be interpreted on input Possible to write out

More information

C-Refresher: Session 10 Disk IO

C-Refresher: Session 10 Disk IO C-Refresher: Session 10 Disk IO Arif Butt Summer 2017 I am Thankful to my student Muhammad Zubair bcsf14m029@pucit.edu.pk for preparation of these slides in accordance with my video lectures at http://www.arifbutt.me/category/c-behind-the-curtain/

More information

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

CSci 4061 Introduction to Operating Systems. Input/Output: High-level CSci 4061 Introduction to Operating Systems Input/Output: High-level I/O Topics First, cover high-level I/O Next, talk about low-level device I/O I/O not part of the C language! High-level I/O Hide device

More information

System Software Experiment 1 Lecture 7

System Software Experiment 1 Lecture 7 System Software Experiment 1 Lecture 7 spring 2018 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungyunkwan University http://csl.skku.edu SSE3032: System Software Experiment 1, Spring 2018

More information

OOP- 6 Direct Access Files & Software Engineering Individual Assignment

OOP- 6 Direct Access Files & Software Engineering Individual Assignment OOP-6-DA-Files-SE-HW.docx CSCI 2320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax, but you must

More information

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

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060 CS 2060 Files and Streams Files are used for long-term storage of data (on a hard drive rather than in memory). Files and Streams Files are used for long-term storage of data (on a hard drive rather than

More information

Ch 11. C File Processing (review)

Ch 11. C File Processing (review) Ch 11 C File Processing (review) OBJECTIVES To create, read, write and update files. Sequential access file processing. Data Hierarchy Data Hierarchy: Bit smallest data item Value of 0 or 1 Byte 8 bits

More information

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

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Lecture 8 Data Files Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To be able to create, read, write and update

More information

C Programming Language

C Programming Language C Programming Language File Input/Output Dr. Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE Review of the Precedent Lecture Arrays and Pointers Class Objectives What is a File?

More information

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

fopen() fclose() fgetc() fputc() fread() fwrite() The ability to read data from and write data to files is the primary means of storing persistent data, data that does not disappear when your program stops running. The abstraction of files that C provides

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 13 si 14: Unix interface for working with files. Cristina Nita-Rotaru Lecture 13/Fall 2013 1 Working with Files (I/O) File system: specifies how the information is organized

More information

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

Chapter 12. Files (reference: Deitel s chap 11) chap8 Chapter 12 Files (reference: Deitel s chap 11) 20061025 chap8 Introduction of File Data files Can be created, updated, and processed by C programs Are used for permanent storage of large amounts of data

More information

C:\Temp\Templates. Download This PDF From The Web Site

C:\Temp\Templates. Download This PDF From The Web Site 11 2 2 2 3 3 3 C:\Temp\Templates Download This PDF From The Web Site 4 5 Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6 Copy/Paste Main # include "Utilities.hpp" # include

More information

ENG120. Misc. Topics

ENG120. Misc. Topics ENG120 Misc. Topics Topics Files in C Using Command-Line Arguments Typecasting Working with Multiple source files Conditional Operator 2 Files and Streams C views each file as a sequence of bytes File

More information

Chapter 11 File Processing

Chapter 11 File Processing 1 Chapter 11 File Processing Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 11 File Processing Outline 11.1 Introduction 11.2 The Data Hierarchy 11.3

More information

Introduction to file management

Introduction to file management 1 Introduction to file management Some application require input to be taken from a file and output is required to be stored in a file. The C language provides the facility of file input-output operations.

More information

Computer System and programming in C

Computer System and programming in C File Handling in C What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary storage so that the contents of files remain intact

More information

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12 BIL 104E Introduction to Scientific and Engineering Computing Lecture 12 Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents

More information

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

File (1A) Young Won Lim 11/25/16 File (1A) Copyright (c) 2010-2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

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

File I/O. Arash Rafiey. November 7, 2017 November 7, 2017 Files File is a place on disk where a group of related data is stored. Files File is a place on disk where a group of related data is stored. C provides various functions to handle files

More information

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Systems Programming. 08. Standard I/O Library. Alexander Holupirek Systems Programming 08. Standard I/O Library Alexander Holupirek Database and Information Systems Group Department of Computer & Information Science University of Konstanz Summer Term 2008 Last lecture:

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6 1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard

More information

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

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan File Processing Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 11.2 The Data Hierarchy 11.3 Files and Streams 11.4 Creating a Sequential

More information

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University Introduction to Computer and Program Design Lesson 6 File I/O James C.C. Cheng Department of Computer Science National Chiao Tung University File System in OS Microsoft Windows Filename DriveID : /DirctoryName/MainFileName.ExtensionName

More information

Computer programming

Computer programming Computer programming "He who loves practice without theory is like the sailor who boards ship without a ruder and compass and never knows where he may cast." Leonardo da Vinci T.U. Cluj-Napoca - Computer

More information

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

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Standard File Pointers

Standard File Pointers 1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf

More information

C File Processing: One-Page Summary

C File Processing: One-Page Summary Chapter 11 C File Processing C File Processing: One-Page Summary #include int main() { int a; FILE *fpin, *fpout; if ( ( fpin = fopen( "input.txt", "r" ) ) == NULL ) printf( "File could not be

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Introduction to Computer Programming Lecture 18 Binary Files

Introduction to Computer Programming Lecture 18 Binary Files Introduction to Computer Programming Lecture 18 Binary Files Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr 1 RECALL: Text File Handling

More information

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection 1 Files 1.1 File functions Opening Files : The function fopen opens a file and returns a FILE pointer. FILE *fopen( const char * filename, const char * mode ); The allowed modes for fopen are as follows

More information

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

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline. CMPE-013/L Outline File Processing File I/O Gabriel Hugh Elkaim Winter 2014 Files and Streams Open and Close Files Read and Write Sequential Files Read and Write Random Access Files Read and Write Random

More information

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

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur 8. Structures, File I/O, Recursion 18 th October IIT Kanpur C Course, Programming club, Fall 2008 1 Basic of Structures Definition: A collection of one or more different variables with the same handle

More information

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

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character 1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard

More information

UNIX System Programming

UNIX System Programming File I/O 경희대학교컴퓨터공학과 조진성 UNIX System Programming File in UNIX n Unified interface for all I/Os in UNIX ü Regular(normal) files in file system ü Special files for devices terminal, keyboard, mouse, tape,

More information

Dr. Thomas Hicks. Computer Science Department Trinity University 1

Dr. Thomas Hicks. Computer Science Department Trinity University 1 Dr. Thomas Hicks Computer Science Department Trinity University General Information About B Trees & B+ Trees There Are a Number of Ways to Implement B+ Trees; Having Coded B+ Trees Twice, I Am Going To

More information

Computer Programming Unit v

Computer Programming Unit v READING AND WRITING CHARACTERS We can read and write a character on screen using printf() and scanf() function but this is not applicable in all situations. In C programming language some function are

More information

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

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name. Unit-III Preprocessor: The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it

More information

Lecture6 File Processing

Lecture6 File Processing 1 Lecture6 File Processing Dr. Serdar ÇELEBİ 2 Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a Sequential Access File Updating Sequential Access

More information

25.2 Opening and Closing a File

25.2 Opening and Closing a File Lecture 32 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 32: Dynamically Allocated Arrays 26-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor:

More information

C Basics And Concepts Input And Output

C Basics And Concepts Input And Output C Basics And Concepts Input And Output Report Working group scientific computing Department of informatics Faculty of mathematics, informatics and natural sciences University of Hamburg Written by: Marcus

More information

Input/Output and the Operating Systems

Input/Output and the Operating Systems Input/Output and the Operating Systems Fall 2015 Jinkyu Jeong (jinkyu@skku.edu) 1 I/O Functions Formatted I/O printf( ) and scanf( ) fprintf( ) and fscanf( ) sprintf( ) and sscanf( ) int printf(const char*

More information

Program Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts)

Program Design (II): Quiz2 May 18, 2009 Part1. True/False Questions (30pts) Part2. Multiple Choice Questions (40pts) Class: No. Name: Part1. True/False Questions (30pts) 1. Function fscanf cannot be used to read data from the standard input. ANS: False. Function fscanf can be used to read from the standard input by including

More information

Data File and File Handling

Data File and File Handling Types of Disk Files Data File and File Handling Text streams are associated with text-mode files. Text-mode files consist of a sequence of lines. Each line contains zero or more characters and ends with

More information

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING C: Part 6 LANGUAGE OF THE C In part 6 of Steve Goodwins C tutorial we continue our look at file handling and keyboard input File handling Most software will at some time need to read from (or perhaps write

More information

Input / Output Functions

Input / Output Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Input / Output Functions Presentation G Read/Study: Reek Chapter 15 Gojko Babić 10-03-2018 Input and Output Functions The stdio.h contain

More information

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

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System What Is Operating System? Operating Systems, System Calls, and Buffered I/O emacs gcc Browser DVD Player Operating System CS 217 1 Abstraction of hardware Virtualization Protection and security 2 Academic

More information

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100 FINAL EXAMINATION INTRODUCTION TO ALGORITHMS AND PROGRAMMING II 03-60-141-01 U N I V E R S I T Y O F W I N D S O R S C H O O L O F C O M P U T E R S C I E N C E Winter 2014 Last Name: First Name: Student

More information

Laboratory: USING FILES I. THEORETICAL ASPECTS

Laboratory: USING FILES I. THEORETICAL ASPECTS Laboratory: USING FILES I. THEORETICAL ASPECTS 1. Introduction You are used to entering the data your program needs using the console but this is a time consuming task. Using the keyboard is difficult

More information

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax,

More information

Organization of a file

Organization of a file File Handling 1 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example: Consider keeping students records 100 students

More information

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

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch. 1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions

More information

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate! 1 2 Objectives 1. Agregates 2. Structs 3. Introduction To Classes 4. Constructor 5. Destructor 6. Function Overloading 7. Default Arguments 8. Accessors & Mutators 9. ADT Abstract Data Types 10. Operator

More information

2009 S2 COMP File Operations

2009 S2 COMP File Operations 2009 S2 COMP1921 9. File Operations Oliver Diessel odiessel@cse.unsw.edu.au Last updated: 16:00 22 Sep 2009 9. File Operations Topics to be covered: Streams Text file operations Binary file operations

More information

Lecture Data layout on disk. How to store relations (tables) in disk blocks. By Marina Barsky Winter 2016, University of Toronto

Lecture Data layout on disk. How to store relations (tables) in disk blocks. By Marina Barsky Winter 2016, University of Toronto Lecture 01.04 Data layout on disk How to store relations (tables) in disk blocks By Marina Barsky Winter 2016, University of Toronto How do we map tables to disk blocks Relation, or table: schema + collection

More information

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 1 RECOMMENDATION Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 2 3 Copy Project Folder There will be a number of times when

More information

C mini reference. 5 Binary numbers 12

C mini reference. 5 Binary numbers 12 C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char

More information

Fundamentals of Programming. Lecture 10 Hamed Rasifard

Fundamentals of Programming. Lecture 10 Hamed Rasifard Fundamentals of Programming Lecture 10 Hamed Rasifard 1 Outline File Input/Output 2 Streams and Files The C I/O system supplies a consistent interface to the programmer independent of the actual device

More information

File I/O. Preprocessor Macros

File I/O. Preprocessor Macros Computer Programming File I/O. Preprocessor Macros Marius Minea marius@cs.upt.ro 4 December 2017 Files and streams A file is a data resource on persistent storage (e.g. disk). File contents are typically

More information

UNIT- 2. Binary File

UNIT- 2. Binary File UNIT- 2 Binary Files Structure: 2.1 Classification Of Files 2.2 Files Modes 2.3 Standard Library Functions for Files 2.4 File Type Conversion 2.1 Classification Of Files A file is collection of data. A

More information

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSC209H Lecture 3. Dan Zingaro. January 21, 2015 CSC209H Lecture 3 Dan Zingaro January 21, 2015 Streams (King 22.1) Stream: source of input or destination for output We access a stream through a file pointer (FILE *) Three streams are available without

More information

2 2

2 2 1 2 2 3 3 C:\Temp\Templates 4 5 Use This Main Program 6 # include "Utilities.hpp" # include "Student.hpp" Copy/Paste Main void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A

More information

System Programming. Standard Input/Output Library (Cont d)

System Programming. Standard Input/Output Library (Cont d) Content : by Dr. B. Boufama School of Computer Science University of Windsor Instructor: Dr. A. Habed adlane@cs.uwindsor.ca http://cs.uwindsor.ca/ adlane/60-256 Content Content 1 Binary I/O 2 3 4 5 Binary

More information

File Handling. Reference:

File Handling. Reference: File Handling Reference: http://www.tutorialspoint.com/c_standard_library/ Array argument return int * getrandom( ) static int r[10]; int i; /* set the seed */ srand( (unsigned)time( NULL ) ); for ( i

More information

Introduction to C Recursion, sorting algorithms, files

Introduction to C Recursion, sorting algorithms, files Introduction to C Recursion, sorting algorithms, files Teil I. recursion TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1 RECURSION Recursion in programming is a way of solving a problem, by solving

More information

M.CS201 Programming language

M.CS201 Programming language Power Engineering School M.CS201 Programming language Lecture 16 Lecturer: Prof. Dr. T.Uranchimeg Agenda Opening a File Errors with open files Writing and Reading File Data Formatted File Input Direct

More information

IO = Input & Output 2

IO = Input & Output 2 Input & Output 1 IO = Input & Output 2 Idioms 3 Input and output in C are simple, in theory, because everything is handled by function calls, and you just have to look up the documentation of each function

More information

Programming Fundamentals

Programming Fundamentals Programming Fundamentals Day 4 1 Session Plan Searching & Sorting Sorting Selection Sort Insertion Sort Bubble Sort Searching Linear Search Binary Search File Handling Functions Copyright 2004, 2 2 Sorting

More information

Standard I/O in C, Computer System and programming in C

Standard I/O in C, Computer System and programming in C Standard I/O in C, Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library 5. Files and Directories 6. System Data Files and Information 7. Environment

More information

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions. UNIT-V Unit-5 File Streams Formatted I/O Preprocessor Directives Printf Scanf A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage

More information

COMP1917: 15 File IO

COMP1917: 15 File IO COMP1917: 15 File IO Sim Mautner s.mautner@unsw.edu.au October 9, 2016 Sim Mautner (UNSW) COMP1917: 15 File IO October 9, 2016 1 / 8 Purpose Read/write external files from within an application. Previously,

More information

Lecture 8: Structs & File I/O

Lecture 8: Structs & File I/O ....... \ \ \ / / / / \ \ \ \ / \ / \ \ \ V /,----' / ^ \ \.--..--. / ^ \ `--- ----` / ^ \. ` > < / /_\ \. ` / /_\ \ / /_\ \ `--' \ /. \ `----. / \ \ '--' '--' / \ / \ \ / \ / / \ \ (_ ) \ (_ ) / / \ \

More information

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions Topic: File input/output I. Streams II. Access methods III. C++ style Input, output, random access Stream classes: ifstream, ofstream IV. C style The FILE data type Opening files Writing to, reading text

More information

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

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing Chapter 14: File Processing Files and Streams C views each file simply as a sequential stream of bytes. It ends as if there is an end-of-file marker. The data structure FILE, defined in stdio.h, stores

More information

UNIX input and output

UNIX input and output UNIX input and output Disk files In UNIX a disk file is a finite sequence of bytes, usually stored on some nonvolatile medium. Disk files have names, which are called paths. We won t discuss file naming

More information

HIGH LEVEL FILE PROCESSING

HIGH LEVEL FILE PROCESSING HIGH LEVEL FILE PROCESSING 1. Overview The learning objectives of this lab session are: To understand the functions used for file processing at a higher level. o These functions use special structures

More information

Writing to and reading from files

Writing to and reading from files Writing to and reading from files printf() and scanf() are actually short-hand versions of more comprehensive functions, fprintf() and fscanf(). The difference is that fprintf() includes a file pointer

More information

C PROGRAMMING. Characters and Strings File Processing Exercise

C PROGRAMMING. Characters and Strings File Processing Exercise C PROGRAMMING Characters and Strings File Processing Exercise CHARACTERS AND STRINGS A single character defined using the char variable type Character constant is an int value enclosed by single quotes

More information

C: How to Program. Week /June/18

C: How to Program. Week /June/18 C: How to Program Week 17 2007/June/18 1 Chapter 11 File Processing Outline 11.1 Introduction 11.2 The Data Hierarchy 11.3 Files and Streams 11.4 Creating a Sequential Access File 11.5 Reading Data from

More information

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

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by

More information

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL. https://vu5.sfc.keio.ac.jp/sa/login.php

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL. https://vu5.sfc.keio.ac.jp/sa/login.php 1 SOFTWARE ARCHITECTURE 2. FILE SYSTEM Tatsuya Hagino hagino@sfc.keio.ac.jp lecture URL https://vu5.sfc.keio.ac.jp/sa/login.php 2 Operating System Structure Application Operating System System call processing

More information

Chapter 10: File Input / Output

Chapter 10: File Input / Output C: Chapter10 Page 1 of 6 C Tutorial.......... File input/output Chapter 10: File Input / Output OUTPUT TO A FILE Load and display the file named formout.c for your first example of writing data to a file.

More information

mywbut.com 12 File Input/Output

mywbut.com 12 File Input/Output 12 File Input/Output Data Organization File Operations Opening a File Reading from a File Trouble in Opening a File Closing the File Counting Characters, Tabs, Spaces, A File-copy Program Writing to a

More information

CSCE C. Lab 10 - File I/O. Dr. Chris Bourke

CSCE C. Lab 10 - File I/O. Dr. Chris Bourke CSCE 155 - C Lab 10 - File I/O Dr. Chris Bourke Prior to Lab Before attending this lab: 1. Read and familiarize yourself with this handout. 2. Review the following free textbook resources: http://en.wikibooks.org/wiki/c_programming/file_io

More information

STRUCTURES & FILE IO

STRUCTURES & FILE IO STRUCTURES & FILE IO Structures Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored in files Combined

More information

Call The Project Dynamic-Memory

Call The Project Dynamic-Memory 1 2 2 Call The Project Dynamic-Memory 4 4 Copy-Paste Main # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo) = %d\n", (*PtrNo)); } getchar();

More information

Pointers and File Handling

Pointers and File Handling 1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY

More information

CSE 410: Systems Programming

CSE 410: Systems Programming CSE 410: Systems Programming Input and Output Ethan Blanton Department of Computer Science and Engineering University at Buffalo I/O Kernel Services We have seen some text I/O using the C Standard Library.

More information

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file. File Structures Physical Files and Logical Files Chap 2. Fundamental File Processing Operations Things you have to learn Physical files and logical files File processing operations: create, open, close,

More information