|
|
|
File I/O Example - Slopes of Lines Between Pairs of Points
1. Create a data file and write data to it.
% cat points_write.cpp
// file i/o example
// create and write to a data file
#include<iostream>
#include <fstream>
using namespace std;
#include<string>
int main()
{
double x1, y1, x2, y2;
char more;
ofstream out_file;
string f1;
cout << "Enter the file name: ";
cin >> f1;
out_file.open( f1.c_str() ); // open file for
output
// c_str() converts string object
// to a C-style character array
if (out_file.fail()) // check for successful open
{
cout << "File failed to open"
<<
endl;
}
else
{
do
{
cout << "Enter x
and y coordinated for point 1: ";
cin >> x1
>>
y1;
cout << "Enter x
and y coordinated for point 2: ";
cin >> x2
>>
y2;
out_file << x1
<<
" " << y1 << " " << x2 << " "<< y2
<<
" ";
cout << "More
data?
y or n: ";
cin >> more;
} while(more == 'y');
out_file.close();
} //else
return 0;
}
% g++ points_write.cpp
% a.out
Enter the file name: points_dat
Enter x and y coordinated for point 1: 2 2
Enter x and y coordinated for point 2: 8 4
More data? y or n: y
Enter x and y coordinated for point 1: 4 10
Enter x and y coordinated for point 2: -2 -3
More data? y or n: y
Enter x and y coordinated for point 1: 27 16
Enter x and y coordinated for point 2: 19 28
More data? y or n: n
% cat points_dat
2 2 8 4 4 10 -2 -3 27 16 19 28 %
2. Read the data file and calculate the slopes of the lines.
% cat points_read.cpp
// read the points file
#include<iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include <string>
int main()
{
double x1, y1, x2, y2, slope;
ifstream in_file;
string f1;
cout << "Enter the data file name: ";
cin >> f1;
in_file.open( f1.c_str() ); // open file for
input
// c_str() converts string object
// to a C-style character array
if (in_file.fail()) // check for successful open
{
cout << "File failed to open"
<<
endl;
}
else
{
in_file >> x1 >> y1 >>
x2 >> y2;
while ( !in_file.eof() )
{
// calculate slope
slope = (y2 - y1) /
(x2
-x1);
// use stream
manipulators
cout <<
setiosflags(ios::fixed
| ios::showpoint)
<< setprecision(2);
cout << "Slope
between
points ("
<< x1 << ", " << y1 << ") and (" <<
x2
<< ", "
<< y2 << ") is " << slope << endl;
in_file >> x1
>>
y1 >> x2 >> y2;
}
in_file.close();
} // else
return 0;
}
% g++ points_read.cpp
% a.out
Enter the data file name: ponts_dat
File failed to open
% a.out
Enter the data file name: points_dat
Slope between points (2.00, 2.00) and (8.00, 4.00) is 0.33
Slope between points (4.00, 10.00) and (-2.00, -3.00) is 2.17
Slope between points (27.00, 16.00) and (19.00, 28.00) is -1.50
3. Alternate method to read the file.
% cat points_read2.cpp
// read the points file
#include<iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include <string>
int main()
{
double x1, y1, x2, y2, slope;
ifstream in_file;
string f1;
cout << "Enter the data file name: ";
cin >> f1;
in_file.open( f1.c_str() ); // open file for
input
// c_str() converts string object
// to a C-style character array
if (in_file.fail()) // check for successful open
{
cout << "File failed to open"
<< endl;
}
else
{
while (in_file
>> x1 >> y1 >> x2 >> y2)
{
// calculate slope
slope = (y2 - y1) / (x2 -x1);
// use stream manipulators
cout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2);
cout << "Slope between points ("
<< x1 << ", " << y1 << ") and (" <<
x2 << ", "
<< y2 << ") is " << slope << endl;
}
in_file.close();
} // else
return 0;
}
% g++ points_read2.cpp
% a.out
Enter the data file name: points_dat
Slope between points (2.00, 2.00) and (8.00, 4.00) is 0.33
Slope between points (4.00, 10.00) and (-2.00, -3.00) is 2.17
Slope between points (27.00, 16.00) and (19.00, 28.00) is -1.50
|
|