text files. text files files data saved in external storage and can be referenced by a single name...
TRANSCRIPT
TEXT FILES
Text Files• Files
• Data saved in external storage and can be referenced by a single name
• File types• Document, image, audio, video, etc.• Program files, data files• Source code, object code, executable code
• File types• Text files – can be read without special program (.txt, .dat)• Binary files – requires special programs for reading
(.exe, .jpg, .docx, etc)
File Processing• Open
• Make connection between external file and internal file object
• Process• Read into buffer• Write to buffer
• Close• Sever connection between external file and internal file object• Empty buffer
Reading from File#include <iostream>#include <fstream> // for ifstream#include <string>using namespace std;
int main() { ifstream inf; string name; int age; // Open file inf.open("myFile.txt");
// Read data and output to console while (!inf.eof()) { inf >> name; inf >> age; cout << name << " is " << age << " years old.\n"; }
return 0;}
Anne 25Ben 15Cathy 21Donald 25Eddy 29
myFile.txt