File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be...

12
File Input/Output

Transcript of File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be...

Page 1: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

File Input/Output

Page 2: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

External Files

• Batch– Requires use of data files (save to disk)– Batch can be run during off peak use– allows things to be complete at start of day

• Interactive– Real time systems– Ok for smaller programs– Programs that complete quickly

Page 3: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

Files

• Naming– .cpp .dat .out .in .txt

• How to attach files to the stream– stream object– external file name– internal name– open– close

• Additional functions as part of <fstream> class

Page 4: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

Files

•Declare the stream to be processed need to #include <fstream>ifstreamins; // ins is the internal file name for input modeofstream outs; // outs is the internal file name for output mode

•Need to open and close the filesins.open (“input.txt”); //”input.txt” and “output.out” are eternal file namesouts.open (“output.out”);ins >> number;outs << number;ins.close();outs.close ();

Page 5: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

Creating a Sequential File

• Open the file. Specify: – External name: the full name of the file on disk.– Internal name: the name by which the file will be

known in the program.– File mode: the purpose of the file: input or output.

• Write data to the file, or read data from, the file.• Close the file.

– Saves the file and puts an end-of-file marker after the last record if the file was created in the program.

– Closes the file if the file is an input file.

Page 6: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

Example

• If we issue the command ‘Open ‘GRADES” for Output As NewFile’– GRADES is the external name– NewFile is the internal name– The mode is output

• If we issue the command ‘Close NewFile’– Puts the EOF marker.– Closes the file.– Saves the file with the external name ‘GRADES.’

Page 7: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

Reading a File

• To open: ‘Open “GRADES “ for Input As GradeFile’• Read the internal filename and the fields/variables

– Read GradeFile, Name, Score• Read records within a loop.

While Not EOF(GradeFile)

Read GradeFile, Name,Score

….

End While

Close GradeFile

Page 8: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

// fileapp.cpp#include <iostream>#include <fstream> // necessary for file I/O#include <string> // necessary for string objectusing namespace std;

int main(){

string user_name;int age; ofstream outfile; // Declare file stream named outfile.

cout << "Enter your name: "; // Get name from user.getline(cin, user_name);cout << "Enter your age: "; // Get age from user.cin >> age;

outfile.open("NAME_AGE.TXT",ios::out); // Open file for output.

if (outfile) // If no error occurs while opening file/ write the data to the file.{ outfile << user_name << endl; // Write the name to the file.

outfile << age << endl; // Write the age to the file.outfile.close(); // Close the output file.

}else // If error occurred, display message.{

cout << "An error occurred while opening the file.\n";}return 0;

}

Page 9: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

// numread.cpp#include <fstream> #include <iostream>#include <iomanip>using namespace std;

int main(){ float x, sum;

int count;ifstream infile; // Declare file stream named infile.infile.open("FLOATS.TXT",ios::in); // Open file for input.

sum = 0.0; count = 0; if (infile) // If no error occurred while opening file input the data from the file{

cout.setf(ios::fixed); cout << "The numbers in the data file are as follows:\n"<< setprecision(1);

do // Read numbers until 0.0 is encountered.{

infile >> x; // Get number from file.cout << x << endl; // Print number to screen.sum = sum + x; // Add number to sum.count++; // Increment count of how many numbers read.

} while(x != 0.0);

// Output sum and average.cout << "The sum of the numbers is " << sum << endl;cout << "The average of the numbers (excluding zero) is " << sum / (count - 1) << endl;

}else // If error occurred, display message.{

cout << "An error occurred while opening the file.\n";}infile.close(); // Close the output file.return 0;

}

Page 10: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

// num_end.cpp#include <fstream> #include <iostream>#include <iomanip>using namespace std;int main(){ double x, sum;

int count;ifstream infile; // Declare file stream named infile.infile.open("NUMS.TXT",ios::in); // Open file for input.sum = 0.0; count = 0; if (infile) // If no error occurred while opening file input the data from the file{. cout.setf(ios::fixed); cout << "The numbers in the data file are as follows:\n"<< setprecision(1);

do // Read numbers until the end of file is encountered.{infile >> x; // Get number from file.if(!infile.fail()) // If the end of file is not reached...{ cout << x << endl; // Print number to screen. sum = sum + x; // Add number to sum. count++; // Increment count of how many numbers read.}} while(!infile.fail());// Output sum and average.cout << "The sum of the numbers is " << sum << endl;cout << "The average of the numbers is " << sum / count << endl;}else // If error occurred, display message.{cout << "An error occurred while opening the file.\n";}infile.close(); // Close the output file.return 0;

}

Page 11: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

// num_end.cpp#include <fstream> #include <iostream>#include <iomanip>using namespace std;int main(){ double x, sum;

int count;ifstream infile; // Declare file stream named infile.infile.open("NUMS.TXT",ios::in); // Open file for input.sum = 0.0; count = 0; if (infile) // If no error occurred while opening file input the data from the file{. cout.setf(ios::fixed);

cout << "The numbers in the data file are as follows:\n"<< setprecision(1);

infile >> x; // Get number from filewhile ( !infile.fail() ){ cout << x << endl; // Print number to screen. sum = sum + x; // Add number to sum. count++; // Increment count of how many numbers read. infile >> x; // Get number from file.}// Output sum and average.cout << "The sum of the numbers is " << sum << endl;cout << "The average of the numbers is " << sum / count << endl;

}else // If error occurred, display message.{

cout << "An error occurred while opening the file.\n";}infile.close(); // Close the output file.return 0;

}

Page 12: File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.

Pseudocode Language (Ch 5)

In this chapter we added specific data type declarations, some numeric functionsand file I/O

Declarations: Functions:Int(Variable)

Declare Variable As Data Type Random(Variable)Sqrt(Variable)

File I/O:

Open “file” For Input/Output As NameClose Name

Read Name, VariableWrite Name, Variable

EOF(Name)