ifstream& ist, ofstream& ost in a function



i want to be able to read the file that the user inputted and format the text then output the formatted text into the output file how do i pass the input and output files to the function, read the input file and so i can reformat it





#include <iostream>
#include <fstream>
#include <string>
#include<cstdlib>




using namespace std;

int reformat(int length, ifstream& ist, ofstream& ost) {



char c;

return length;

}



int main(int argc, char* argv[])

{


int length=70;
cout << "Please enter input txt file name to be formatted: ";
string iname;
cin >> iname;
ifstream ist(iname.c_str()); // ist is an input stream for the file named name
if (!ist) exit(EXIT_FAILURE);


cout << "Please enter name of txt output file to for the new formatted version: ";
string oname;
cin >>oname;
ofstream ost(oname.c_str()); // ost is an output stream for a file named name
if (!ost) exit(EXIT_FAILURE);


cout<<reformat(length);
reformat needs to be called reformat(length, ist, ost);
At that point your program should at least pop 70 on the screen.

You don't need to convert the string into a c-string when you open the files either.
^ That didn't work i need reformat as an int and when i just put ist and ost i get a compiler error saying they haven't been declared



----update that did work, but ill post again if im stuck LowestOne thanks for the help.
Last edited on
How would i read the file inside of the function
same as when it isn't. :)
Topic archived. No new replies allowed.