Passing a string to ofstream

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?

Below is my full code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//------------------------------------------------------------------------------------------//
//                                                                                          //
// 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>

using namespace 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);

}
Last edited on
either the first way or
1
2
3
4
5
// Cast string as character array for filename
const char* complete = static_cast<const char*>(comp);

// Pass char array to output file stream as filename
ofstream file(complete);
@viliml: No

This is how it has to be done:
ofstream file(complete.c_str());
The std::ofstream constructor which takes a std::string is not available in old C++. It is there in C++11.
http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream
So for backwards compatability with older compilers use:

1
2
3
4
5
// Cast string as character array for filename
const char* complete = static_cast<const char*>(comp);

// Pass char array to output file stream as filename
ofstream file(complete.c_str());
Topic archived. No new replies allowed.