Electrical Engineering - Chapter 4: Basic Input and Output

Series of bytes in sequence Flow from device to device Objects are regions of storage in memory Object used determines device stream comes from or goes to cin and cout are identifiers for objects defined by iostream (screen and keyboard)

ppt16 trang | Chia sẻ: thuongdt324 | Lượt xem: 427 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Electrical Engineering - Chapter 4: Basic Input and Output, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 4 – Basic Input and OutputReading Data Keyboard inputcin object>> called extraction operatorSyntax: cin >> variable;Example:Lesson 4.1cin >> income >> expense;Reads to values typed at keyboard (separated by whitespace) to memory locations names, income, and expensePromptMessage sent from program to screenInforms user what data to enterPrecedes cin statement in programLesson 4.1int age;cout > age;Input and Output StreamsSeries of bytes in sequenceFlow from device to deviceObjects are regions of storage in memoryObject used determines device stream comes from or goes tocin and cout are identifiers for objects defined by iostream (screen and keyboard)Lesson 4.1Writing Output to a FileSimilar to writing to screenUse object connected to output fileNeed the fstream header #include Open file for writingDeclare object of ofstream classLesson 4.2Opening FilesGeneral form ofstream object_name (“file_name”);Choose object_name like variable nameobject_name is object of class ofstreamFilename is where output will be storedCan use full pathname “C:\\salary.out”Lesson 4.2Double quotes surround file_nameTwo backslashes needed – escape sequence for oneWriting to FilesGeneral form object_name Declare object of ifstream ifstream object_name(“file_name”);Use ifstream object to read file like cinLesson 4.3Reading From a FileUse ifstream object to read file like cin used for keyboard ifstream infile(“C:\\money.dat”); infile >> salary1 >> salary2;C++ looks for whitespace between numbersNewline character treated as whitespaceAdditional reading continues sequentiallyLesson 4.3Reading From a FileLesson 4.335 122 3.5 6.18 34money.datinfile >> s1 >> s2 >> s3;s1s2s335122s43.5infile >> s4;Note: The number of data entries and their data types of variables should read should match the number and order within the file!Reading Character DataCan use cin cin >> c1 >> c2 >> c3;Reads next three characters typed at keyboardWhitespace is NOT needed for separating character dataif used, whitespace is ignoredLesson 4.4Examples:Lesson 4.4cin >> c1 >> c2 >> c3;cin >> c4 >> c5;cin >> c6;cin >> c7 >> c8;af d32(enter)hj(tab)w (enter)kKeyboard Inputc1c2c3c4c5c6c7c8afd32hjwchar c1,c2,c3,c4,c5,c6,c7,c8;Note: All whitespace characters are ignored!Input BufferRegion of memory used for temporary storage of information being transferred between devicesAccessed sequentiallyPosition indicator keeps track of point to which information has been readLesson 4.4Input BufferLesson 4.4Keyboard entry: af d32(enter) a f d 3 2 (enter) h j (tab) w (enter)hj(tab)w (enter)cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6; afd32hSummaryRead input from keyboardWrite output to fileRead data from fileRead and work with character dataWork with the input bufferLearned how to:
Tài liệu liên quan