190 likes | 202 Views
Learn about interactive and noninteractive file processing in C++. Input from keyboard and file, prompt user, user-friendly echo checking. Benefits of noninteractive processing, batch processing, storing values in a file, and more.
E N D
Interactive I/O • Input from keyboard • Must prompt user • User friendly • echo checking cosc175/files
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
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
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
text files • composed of characters only • Differs from .doc(x) file • Size of file • Can be created in notepad, Visual C cosc175/files
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
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
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
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
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
Example in c++ #include <fstream> //includes ifstream and ofstream //declare a handle ofstreamoutFile; //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
Opening for output causes: • ofstreamoutFile;//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
Reading the Data: • Open the file for input. • Read the data • Close the file. cosc175/files
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
Example in c++ #include <fstream> //declare a handle ifstreaminFile; //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
Using Files in c++: 1. #include <fstream> 2.declare file streamsifstream inFile; //input only (reading)ofstream outFile; //output only (writing) 3. open each file for reading or writingfilevar.open(filename) 4.specify names of file streams in input and output statement cosc175/files
Open Files • inMPG.open("inmpg.dat");outMPG.open("outmpg.dat");1. associates a stream variable with file2. 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
input and output- • use var name in input and output statements in place of cin and coutinFile >> startMiles >> endMiles; outFile << "the mileage per gallon is " << mpg << endl; cosc175/files
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