Complete lab exercise 7-1(Book name: C++ programs to accompany programming logic
ID: 667291 • Letter: C
Question
Complete lab exercise 7-1(Book name: C++ programs to accompany programming logic and desighn , 8th edition by Jo anne smith): Opening Files and Performing File Input. Then modify the program so that it takes the text read from flowers.dat and writes it to a new file named flowers2.dat. Submit the revised flowers.cpp file.
This is the flower.cpp code which need to modify
// Flowers.cpp - This program reads names of flowers and whether they are grown in shade or sun from an input
// file and prints the information to the user's screen.
// Input: flowers.dat.
// Output: Names of flowers and the words sun or shade.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare variables here
// Open input file
// Write while loop that reads records from file.
fin >> flowerName;
// Print flower name
fin.close();
return 0;
} // End of main function
This is the flowers.dat file
Astilbe
Shade
Marigold
Sun
Begonia
Sun
Primrose
Shade
Cosmos
Sun
Dahlia
Sun
Geranium
Sun
Foxglove
Shade
Trillium
Shade
Pansy
Sun
Petunia
Sun
Daisy
Sun
Aster
Sun
Explanation / Answer
# include <fstream>
# include <iostream>
# include<string>
using namespace std;
int main()
{
char flowerName[100];
int i,n;
ifstream fin;
ofstream fout;
fin.open("flowers.dat");
cout<<"Reading from the file"<<endl;
while(!flowers.dat.eof())
{
fin >> flowerName;
cout << flowerName << endl;
}
fin.close();
n=sizeof (flowerName);
fout.open("flowers2.dat");
for (i=0; i<n; i++)
{
cout <<"writing to the file" <<endl;
fout << flowerName << endl;
}
fout.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.