Chapter7 - Chapter 7 File Input/Output Introduction All the...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 7 File Input/Output Introduction All the programs we have created so far store data temporarily. As soon as the program is closed, all its data is lost. Program data RAM volatile There is need for a permanent data storage mechanism that will preserve data even when the computer is turned off. Files Hard Disk, CD ROM non-volatile File Processing A file is a block of memory which stores non-volatile data. File Input : Transferring data from an existing file into the program. File Output : Saving program output to a new or existing file. File Types Files are divided in two categories: binary files and text (ASCII or Unicode) files . These names (binary and text) refer to the way a file stores information. We will cover text files only. To understand the differences between these two file types, we need to talk about data types. Binary Data Type Example int n; n = 45; An integer occupies 4 bytes in memory. What is the binary value of n ? Answer: 0000 0000 0000 0000 0000 0000 0010 1101 Memory Location Memory Content (binary value) byte 1 0000 0000 byte 2 0000 0000 byte 3 0000 0000 byte 4 0010 1101 2 5 2 3 2 2 2 ASCII Data Type Binary representation makes sense for numeric types but what about strings? How is a string, say East #45 stored in memory? Strings are composed of characters such as a , b , ( , ? , etc. E a s # 4 t 5 \0 ASCII Characters Computer memory can hold only numbers. Every alphanumeric character has to be stored as a number (binary) in memory. This numeric code is called the ASCII value. ASCII values are stored in what is called the ASCII Character Set . ASCII Character Set 30 40 50 60 70 80 90 100 110 120 N/A ( 2 < F P Z d n x 1 N/A ) 3 = G Q [ e o y 2 <space> * 4 > H R \ f p z 3 ! + 5 ? I S ] g q { 4 , 6 @ J T ^ h r | 5 #- 7 A K U _ i s } 6 $ . 8 B L V ` j t ~ 7 % / 9 C M W a k u N/A 8 & : D N X b l v N/A 9 ' 1 ; E O Y c m w N/A ASCII character codes 0 31 and 127 are reserved for non-visible control characters. Binary numbers greater than 127 are not part of the 7-bit ASCII Character Set. ASCII Strings Memory location Character ASCII value Memory content (binary value) byte 1 E 69 0100 0101 byte 2 a 97 0110 0001 byte 3 s 115 0111 0011 byte 4 t 116 0111 0100 byte 5 32 0010 0000 byte 6 # 35 0010 0011 byte 7 4 52 0011 0100 byte 8 5 53 0011 0101 A File I/O Example #include <iostream> #include <cstdlib> #include <fstream> using namespace std; // FILEIO.CPP int main() { ofstream out_file; ifstream in_file; int i; bool done; out_file.open( "test.dat" ); i = 1 ; do { out_file << i << ' ' << i*i << endl; } while (i++ < 5); out_file.close(); in_file.open( "test.dat" ); done = false ; do { in_file >> i; if (in_file.eof()) done = true ; else cout << i; } while (!done); in_file.close(); cout << endl; system( "PAUSE" ); return 0; } A File I/O Example #include <iostream>...
View Full Document

Page1 / 148

Chapter7 - Chapter 7 File Input/Output Introduction All the...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online