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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started