Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interactive I/O Input from keyboard Must prompt user User friendly

Similar presentations


Presentation on theme: "Interactive I/O Input from keyboard Must prompt user User friendly"— Presentation transcript:

1 Interactive I/O Input from keyboard Must prompt user User friendly
echo checking cosc175/files

2 Noninteractive processing
Batch processing Store values in a file File input and Output C++ can write and read from files just as to keyboard and screen standard input file - keyboard standard output file - screen or printer cosc175/files

3 Advantages to noninteractive
can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output to screen or print Can use out as input into another program means of communicating between programs cosc175/files

4 files file - named secondary memory area that holds a collection of information usually stored on an auxiliary storage device examples external file - a file that is used to communicate with people or programs. It is stored externally to the program. internal file - a file that is created but not saved (scratch file) cosc175/files

5 text files composed of characters only Differs from .doc(x) file
Size of file Can be created in notepad, Visual C cosc175/files

6 File organization naming rules(dos) - 1 to 8 character name, optionally followed by a period and up to a 3 character extension (recommended for programmers!) extension tells the type of file  .cpp – c++ program file .doc – Word file .exe – executable file .obj - Compiled programs - object files .txt - Text files(ASCII form, printable) .dat - data files file is composed of tracks, sectors FAT table cosc175/files

7 files program files - contains program instructions
data files - contains data, collection of records record - one entity i.e. customer file - each record holds data for each customer  fields - data element  customer file has name field, account field,... record key or key field cosc175/files

8 Path Filename includes path No path – default ddirectory
c:\temp\junk.txt Temp directory is usually accessible to students \ is a special character inside strings c:\\temp\\junk.txt cosc175/files

9 Buffer File i/o is slow Buffer is temporary storage area
data for input or output gets written to buffer i/o happens when buffer is full Less reading and writing transaction Happens behind the scenes cosc175/files

10 Writing Data to a File 1. Open the file as an output file.
2. Write the elements. 3. Close the file (happens automatically when you exit the program) cosc175/files

11 Example in c++ #include <fstream> //includes ifstream and ofstream //declare a handle ofstream outFile; //output only (writing) //open the file outFile.open("c:\\temp\\stuff.txt"); //outfile is associated with stuff // write to file outFile << "Hello" << endl; cosc175/files

12 Opening for output causes:
ofstream outFile;//any variable can be used here outFile.open(filename); //include path The directory is searched for "filename". If it doesn't exist, it is created. If it does exist, it will be written over The file is set to write mode. A buffer is created. A file pointer is set to the beginning of file. This is updated with each write. cosc175/files

13 Reading the Data: Open the file for input. Read the data
Close the file.  cosc175/files

14 Opening for input causes:
The directory is searched for "filename". The file is set to read mode. A buffer is created in memory. A file pointer is set to the beginning of file. cosc175/files

15 Example in c++ #include <fstream> //declare a handle ifstream inFile; //input only (reading) //open the file inFile.open("c:\\temp\\stuff.txt"); //DON"T FORGET TO CHECK IF IT WORKED!!! inFile >> name >> age; cosc175/files

16 Using Files in c++: 1. #include <fstream>
2.declare file streams ifstream inFile; //input only (reading) ofstream outFile; //output only (writing) 3. open each file for reading or writing filevar.open(filename) 4.specify names of file streams in input and output statement cosc175/files

17 Open Files inMPG.open("inmpg.dat"); outMPG.open("outmpg.dat"); 1. associates a stream variable with file 2. output file – check if it exists, overwrite, if not create sets marker to beginning of file good idea to open all files at the beginning cosc175/files

18 input and output- use var name in input and output statements in place of cin and cout inFile >> startMiles >> endMiles; outFile << "the mileage per gallon is " << mpg << endl; cosc175/files

19 Input Failure invalid data -> fail state
after fail state, any further i/o operations have no effect opening file that doesn't exist -> fail state Important to test for this!!!!!! inMPG.open("inmpg.dat"); if(!inMPG) { cout << “Can’t open inmpg.dat”; return -1; } cosc175/files


Download ppt "Interactive I/O Input from keyboard Must prompt user User friendly"

Similar presentations


Ads by Google