hello. im in an intro to C++ class and i need some help on my current assignment. im having problem with my exclusionary statement for the if/else loop controlling file output. the program executes but if i give a false output file path, no exclusion statement is printed. i do not want the program to create its own output file. im also open to any other suggestions. i am very new at this and appreciate all help. thanks - garrett
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ofstream out;
ifstream in;
string ReadFile;
string OutFile;
cout<<"Welcome to Rose State Software Engineering lab."<<endl;
cout<<"We will calculate average carbon foot print and corresponding fine."<<endl;
cout<<"Please be ready to provide the data file name for carbon FP for US cities."<<endl;
cout<<"Please enter the full path of input data file name: ";
getline(cin,ReadFile);
cout<<"Please provide full path to output file: ";
getline(cin,OutFile);
if(!out) //this doesnt work!!!! i need correct use of logical operator i think... '!!out' and 'if(!/!!out)' do not work either..
//but i need this next cout statement to come before any factual info is printed to console..
{
cout<<"Failed to open input file. Data will only be written to console."<<endl;
}
else
{
cout<<fixed<<fine<<endl;
out<<fixed<<fine<<endl;
FineTotal = FineTotal + fine;
}
in>>name1;
}
cout<<"****************************************************************************"<<endl;
cout<<"Total number of cities in file = "<<NameCounter<<endl;
cout<<"Total fines collected = ($)"<<FineTotal<<endl;
out<<"****************************************************************************"<<endl;
out<<"Total number of cities in file = "<<NameCounter<<endl;
out<<"Total fines collected = ($)"<<FineTotal<<endl;
}
else
{
cout<<"Error opening input file. File either does not exist or has no read permission."<<endl;
}
}
cout<<"Thank you for using Garret C***** C++ software"<<endl;
out<<"Thank you for using Garret C***** C++ software"<<endl;
cout<<"****************************************************************************"<<endl;
out<<"****************************************************************************"<<endl;
system("pause");
in.close();
out.close();
return 0;
ofstreams are defined to create new files when you open files which do not exist yet. Sorry, nothing you can do about that. What you CAN do is create an ifstream with the filename as the operator to check whether the file already exists, like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int ret = 0;
string outFile = "outfile.txt";
ifstream testStream;
testStream.open(outFile.c_str());
testStream.close();
if(testStream.fail())
{
cout << "File does not exist!",
ret = 1;
} else {
//proceed with whatever you wanted to do
}
return ret;
EDIT: For the second thing, see stream manipulators. You can find them all the way down here: http://www.cplusplus.com/reference/iostream/ (see "Manipulators"- it says iostream, but they work with all other streams aswell)