I have code that developes a string. I intend to pass this string to ofstream for the file name. What is the right way of doing so?
1 2
// Pass string to output file stream as filename
ofstream file(comp);
or
1 2 3 4 5
// Cast string as character array for filename
char complete = static_cast<char>(comp);
// Pass char array to output file stream as filename
ofstream file(complete);
The second method is currently giving me a compile error (can't remember error as I am not at computer that I am developing the code on).
Or should I be doing this a completely different way?
//------------------------------------------------------------------------------------------//
// //
// Developer: (NOT DISCLOSED FOR POST) //
// Developed: 15 MARCH 2012 //
// Filename: clock.cpp //
// //
// Copyright 2012, (NOT DISCLOSED FOR POST) //
// //
// //
// This program is free software: you can redistribute it and/or modify it under the terms //
// of the GNU General Public License as published by the Free Software Foundation, either //
// version 3 of the License, or any later version. //
// //
// This program is distributed in the hope that it will be useful, but WITHOUT ANY //
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A //
// PARTICULAR PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License along with this //
// program. If not, see <http://www.gnu.org/licenses/>. //
// //
//------------------------------------------------------------------------------------------//
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
usingnamespace std;
int main () {
//Setup String Stream object
stringstream ss (stringstream::in | stringstream::out);
//Declare and initialize necessary system variables
int found = 0;
float fin = 0, fout = 0, n, m, p, nMax = 4096, mMax = 512;
float pMax = 128;
string first = "pll_m_n_p_";
string middle = "_";
string end = ".txt";
// Prompt user for input and output frequencies
cout << "Welcome to the CDCE706 PLL configurator./n/n";
cout << "Please enter your desired input and output frequencies in MHz." << endl << endl;
cout << "Input: ";
cin >> fin;
cout << endl << "Output: ";
cin >> fout;
cout << endl;
// Check to see if fequencies are within chip capabilities
if ((fin <= 200) && (fout <= 300)) {
// Output float values as strings
ss << fin;
string in = ss.str();
ss << fout;
string out = ss.str();
// Combine all strings for filename
string comp = first + in + middle + out + end;
cout << "Output being sent to file " << comp;
cout << endl << endl;
// Cast string as character array for filename
//char complete = static_cast<char>(comp);
// Pass string to output file stream as filename
//ofstream file(complete);
// Pass char array to outpul file stream as filename
ofstream file(comp);
// check if file is open other wise
if (file.open()) {
// Setup column headers
file << "M Value\t" << "N Value\t" << "P Value\n" << endl;
// Process all possible compinations of settings
for ( n = 0; n < nMax; n++) {
for ( m = 0; m < mMax; m++) {
for ( p = 0; p < pMax; p++) {
// Check to see if current combination meets chip criteria
if (((fin * n)/(m * p)) == fout) && ((n/m) >= 1)) {
// Tell system combination was found.
found = 1;
// Output combination to file. (Two tabs required for
// proper formatting)
file << m << "\t\t";
file << n << "\t\t";
file << p << endl << endl;
}
}
}
}
// Close the file
file.close();
}
// Terminate program
else {
cout << "No file open. Exiting." << endl;
return(1);
}
// Check if a combination was found
if (!found) {
// Inform user no combination found
cout << "Sorry. I was unable to find any matches." << endl;
cout << "Please try a different input frequency. Goodbye." << endl;
}
else {
// Other wise tell user processing complete
cout << "Done. Thank you.";
}
// Inform user of proper input and output frequency range
else {
cout << "Input/Ouput frequency to high." << endl;
cout << "Limits: In - 200MHz; Out - 300MHz." << endl;
cout << "Goodbye.";
}
// Terminate program successfully.
return(0);
}
// Cast string as character array for filename
constchar* complete = static_cast<constchar*>(comp);
// Pass char array to output file stream as filename
ofstream file(complete);
So for backwards compatability with older compilers use:
1 2 3 4 5
// Cast string as character array for filename
constchar* complete = static_cast<constchar*>(comp);
// Pass char array to output file stream as filename
ofstream file(complete.c_str());