Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 File Input/Output

2 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

3 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 class

4 Files Declare the stream to be processed need to #include ifstreamins;// ins is the internal file name for input mode ofstream outs;// outs is the internal file name for output mode Need to open and close the files ins.open (“input.txt”); //”input.txt” and “output.out” are eternal file names outs.open (“output.out”); ins >> number; outs << number; ins.close(); outs.close ();

5 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.

6 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.’

7 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

8 // fileapp.cpp #include #include // necessary for file I/O #include // necessary for string object using 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; }

9 // numread.cpp #include 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; }

10 // num_end.cpp #include 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; }

11 // num_end.cpp #include 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 file while ( !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; }

12 Pseudocode Language (Ch 5) In this chapter we added specific data type declarations, some numeric functions and file I/O Declarations:Functions: Int(Variable) Declare Variable As Data TypeRandom(Variable) Sqrt(Variable) File I/O: Open “file” For Input/Output As Name Close Name Read Name, Variable Write Name, Variable EOF(Name)


Download ppt "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."

Similar presentations


Ads by Google