Question: C++: Is it possible to read in values from a text file then close it that file, and in the same function write a new

C++: Is it possible to read in values from a text file then close it that file, and in the same function write a new file where I can use those values. I am trying to avoid a vector or anything like that. My code is below and is working, but I am trying to see if I can get it a certain way because my teacher is particular. I commented and bolded the areas that I would like to change if possible.

I should add that the text file I'm reading in has city names (one word) listed with a temperature, for example "Lima 66"

#include #include //Enables use of the ifstream class #include

using namespace std;

int main() { ifstream inFS; //Input file stream-reads file string cityName; int fahrenheit; int celsius;

inFS.open("FahrenheitTemperature.txt"); //Opens file

//Read in cityName and fahrenheit with inFS >>

//Close inFS ofstream outFS; outFS.open("CelsiusTemperature.txt"); //Writes new file

while (inFS >> cityName >> fahrenheit){ //Remove inFS >> here if possible celsius = round((fahrenheit - 32.0) * (5.0 / 9)); outFS << cityName << " " << celsius << endl; }

inFS.close(); //remove if can outFS.close();

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!