text files. text files files data saved in external storage and can be referenced by a single name...

4
TEXT FILES

Upload: florence-carpenter

Post on 02-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

TEXT FILES

Page 2: 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

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)

Page 3: 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

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

Page 4: 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

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