can anyone explain to me what it wants me to do im really confused. sorry for my lack of understanding
Processing Text Files Sequentially
File: An area in secondary storage used to hold information. It can be used as input to a program instead of using the keyboard. Can also be used to store output from a program instead of displaying it on the screen. You must know the physical name {file name) of the file on the device.
1. Include the header file fstream in the program. #include <fstream>
2. Declare the stream (file) variable.
ifstream inData; defines an input stream with name inData
ofstream outData;• defines an output stream with name outData
3. Open the File. For input file, this means the file name and location must be known. We must check to be sure it is found before processing. For output file, it simply means finding space to put the file on the specified device.
inData.open ("prog.dat"); if file is in same place as the program
inData.open ("C:\\temp\\prog.dat"); to specify different path.
Note the \\ which is required in a path name.
outData.open ("prog.dat");
if (!inData) will check for input failure on the open. Be sure to check and stop the program if file not found.
4. Read from the file as with cin. Example: inData >> variablename;
Write to file as with cout. Example: outData << variablename;
All the iomanip functions can be used on this output file.
Use a loop to continue reading until End of File is found:
while (indata) this will return false on End of File. OR
while (!infile.eof()) this will return false if a record was read, true if the end of file mark was read. Loop will continue as long as there is data. Note the !
5. When done with processing, close the file: inData.close(); outData.close();
//This program reads names from a file and writes them to another file. It uses the priming read approach. It will continue until end-of-file is found.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{ ifstream infile; ofstream outfile; string name;
infile.open ("c:\\temp\\myfile.dat");
if (!infile) //there was an error on open, file not found
{ cout << "Cannot open file, terminating program"<< endl;
exit (1); }
outfile.open ("c:\\temp\\newfile.dat);
infile >> name; //this is called a priming read, only used once
while (infile) //or while (!infile.eof())
{ outfile <<name<< endl;
infile>> name; };
infile.close();
outfile.close ();
return 0; }