Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

CS-161

Programming Fundamentals

Lecture - 29 & 30
File Handling in C++
Learning Objectives
Upon completion of this material, you should
be able to:
•Learn about Reading data from files to variables
•Explore how to Reading data from files to Arrays
•Learn about Writing data from variables to files
•Discover how to Writing data from Arrays to files

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2


Files
• Data in Main Memory is “volatile”
• File: Place for “permanent” data storage
• C: drive, A: drive, Flash drive, etc.
Main Memory
progEx2Prob3.exe
File
main()
int num;
Disk
string firstname;
What Is Wrong?
#include <iostream>
#include <string.h>
using namespace std
int Main()
{
// Say Hello 4 times
for(i == 0; i < 3; i++)
{
cout >> "Hello World!" << endl;
}
return(0);
}
What Is Wrong? (Corrections)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
for(i = 0; i < 3; i++)
{
cout << "Hello World!" << endl;
}
return(0);
}
Files

• Data in main memory is “volatile”


• File: Place for “permanent” data storage
• C: drive, A: drive, flash drive, etc.

• In C++, files can be read different ways


− Stream input (or output) lets us read (or
write) a sequence of data.
− We also use streams (cin, cout) to model
keyboard input and text output.
6
Output File Streams
• In C++, the programmer can declare output
streams and link them to output files.
#include <iostream>
#include <fstream>
using namespace std;
....
int num1, num2, num3;
cout << "Enter 3 numbers for datafile: ";
cin >> num1 >> num2 >> num3;
ofstream outputStream;
outputStream.open("datafile.txt");
outputStream << num1 << " ";
outputStream << num2 << " ";
outputStream << num3 << endl;
7 outputStream.close();
Input File Streams
• In C++, the programmer can declare input
streams and link them to input files.
#include <iostream>
#include <fstream>
using namespace std;
....
int num1, num2, num3;
ifstream inputStream;
inputStream.open("datafile.txt");
inputStream >> num1 >> num2 >> num3;
inputStream.close();
8
To Read or Write from a File

• Declare the file stream object.


− I.e., give it a name.
• Associate a file name with the file stream
object by opening the file.
• Read or write to the file using << or >>
operators.
• Close the file.

9
Manipulators

• Used to format output neatly:


− Left justification (columns of names)
− Right justification (columns of numbers)
− Minimum field length (padding)
− Decimal places
• For more information, Malik pgs. 135-145

• There's also an older style of formatting that


uses "printf" and "scanf" that has been copied
into Java.
10
Example of Output Manipulation
#include <iostream>
cout << setw(12)
#include <fstream>
<< fval << " ";
#include <string> cout << left;
#include <iomanip> cout << left;
using namespace std; cout << setw(10)
int main() << name << endl;
{ }
ifstream input;
return(0);
}
int loops, ival, i;
float fval; Output
string name; 8 9.30e+000 Jon
6 1.43e+001 Bill
cout << showpoint 0 3.57e+010 Mary
<< scientific; -23 -4.55e+000 Smith
cout << setprecision(2); -3 -4.00e+003 xyz
input.open("mydata.txt");
input >> loops;
for (i=0; i < loops; i++)
{
input >> ival;
input >> fval;
input >> name;
cout << right;
cout << setw(4)
<< ival << " ";

11
Example with File Input
#include <iostream>
cout << setw(4)
#include <fstream> << ival << " ";
#include <string> cout << setw(12)
#include <iomanip> << fval << " ";
cout << left;
using namespace std; cout << setw(10)
int main() << name << endl;
{ }
ifstream input; return(0);
}
int loops, ival, i;
float fval; mydata.txt file:
string name; 5
8 9.3 Jon
cout << showpoint 6 14.335 Bill
<< scientific 0 35.67e9 Mary
<< setprecision(2); -23 -4.55 Smith
-3 -4e3 xyz
input.open("mydata.txt"); Output:
input >> loops; 8 9.30e+00 Jon
for (i=0; i < loops; i++) 6 1.43e+01 Bill
0 3.57e+10 Mary
{
-23 -4.55e+00 Smith
input >> ival; -3 -4.00e+03 xyz
input >> fval;
input >> name;
cout << right;

12
Summary
• Data in Main Memory is “volatile”
• File: Place for “permanent” data storage
• The Manipulators are used to format output
neatly

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13


References
• C++ Programming: From Problem Analysis to
Program Design, Fourth Edition by D.S. MALIK

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14

You might also like