For some reason my code is not compiling on a linux server. It gives me several ofstream, ifstream, and fstream errors. It compiles perfectly fine on a windows machine.
#include <iostream>
#include <fstream>
#include <string>
bool fileExists(const std::string);
//Bool function to test if output file already exists
bool fileExists(const std::string fileName)
{
std::ifstream infile(fileName);
return infile.good();
}
int main()
{
std::ifstream file_in;//declares the input file
std::ofstream file_out;//declares the output file
std::string filename_in, filename_out;//declares the file strings
//Name prompt for input file
std::cout<<"What is the name of the input file?\n";
std::cin>>filename_in;
//Opens the input file
file_in.open(filename_in, std::ios::in);
//Error if input file fails
if(file_in.fail())
{
//Gives error message and aborts
std::cout<<"\nThe filename "<<filename_in<<" could not be read.\n";
std::cout<<"Possible errors include:\n";
std::cout<<"A - The file-path was not found or\n";
std::cout<<"B - The file does not exist.\n\n";
std::system("pause");
exit(1);
}
std::cout<<"What is the name of the output file?\n";
std::cin>>filename_out;
//Checks to see if a file already exists
if(fileExists(filename_out))
{
//Gives error message and aborts
std::cout<<"\nThe filename "<<filename_out<<" already exists!\n";
std::cout<<"ABORTING FILE COPY!\n\n";
system("pause");
exit(1);
}
//Opens the output file for writing
file_out.open(filename_out, std::ios::out);
//Loop that reads input and writes to output file
char inp;
while(!file_in.eof())
{
//reads the input char
file_in.get(inp);
//does not read eof char on last loop
if(!file_in.eof())
{
file_out<<inp;
}
}
//closes files
file_in.close();
file_out.close();
std::cout<<"\nCopy Successful!\n";//Success message
return 0;
}