Note that there are some explanatory texts on larger screens.

plurals
  1. POofstream error using << operator
    primarykey
    data
    text
    <p>I got error when I use ofstream outputting an double array to a txt file. Here is the code:</p> <pre><code>static void OutpuResults(std::string fileName, double * yCal, int nPtCal) { std::string value; double * yCal_local = yCal; std::ofstream outfile; outfile.open(fileName.c_str()); for (int i = 0; i &lt; nPtCal; i++) { outfile&lt;&lt;yCal_local[i]&lt;&lt;std::endl; } delete[] yCal_local; outfile.close(); } </code></pre> <p>Error occurs at <code>outfile&lt;&lt;yCal_local[i]&lt;&lt;std::endl;</code> where i = 0, and <code>yCal_local[i]</code> is 0.000000 as a double. Makes no sesne to me.</p> <p>Here is the definition of fileName:</p> <pre><code>std::string fileName = "d:\\inter.txt"; </code></pre> <p>and here is the definition of yCal:</p> <pre><code>int nPtCal = 256; double * yCal = new double[nPtCal]; </code></pre> <p>I am wondering where I did wrong? Any suggestion is appreciate it.</p> <p>Edit: It does not have a problem when compling it, but error occurs when it is running. Here is the error message:</p> <pre><code>Unhandled exception at 0x75f5812f in ppppp.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0028f754.. </code></pre> <p>And here is the code showing up when the program returns from error:</p> <pre><code>void __cdecl _unlock ( int locknum ) { /* * leave the critical section. */ LeaveCriticalSection( _locktable[locknum].lock ); } </code></pre> <p>Edit:</p> <p>Here is all the code:</p> <pre><code>#include "pppp.h" #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;vector&gt; static void show_usage(std::string name) { std::cerr &lt;&lt; "To use ZoomCal:\n " &lt;&lt; "ZoomCal.exe inputfile -options\n" &lt;&lt; "options:\n" &lt;&lt; " -l: linear\n" &lt;&lt; " -c: cubic"&lt;&lt; std::endl; } static void LoadData(std::string fileName, double* y) { std::string value; double * y_local = y; char *cstr = new char[fileName.length() + 1]; std::strcpy(cstr, fileName.c_str()); std::ifstream infile; infile.open(cstr); if (!infile.is_open()) { std::cerr&lt;&lt;"File loading failed. Double check if file exists in the same folder as ZoomCal.exe"&lt;&lt;std::endl; exit(1); } int count = 0; int i = 0; int col = 10; while ( infile.good() ) { getline ( infile, value, ',' ); // read a string until next comma: http://www.cplusplus.com/reference/string/getline/ if (count % col == 0) { if (count != 0) { *y_local = std::atof(value.c_str()); y_local++; } } count++; } //delete [] cstr; //delete y_local; infile.close(); } static void OutpuResults(std::string fileName, double * yCal, int nPtCal) { std::string value; //double * yCal_local = yCal; std::ofstream outfile; outfile.open(fileName.c_str()); for (int i = 0; i &lt; nPtCal; i++) { outfile&lt;&lt;yCal[i]&lt;&lt;std::endl; } // delete[] yCal_local; outfile.close(); } double * LinInterp(double * y, int nPt, int nPtCal) { double * yCal = new double[nPtCal]; double * tmp = new double[nPtCal-5]; int interval = 10; double * pPiece = new double[interval]; double f2, f1, m, b; int x2, x1; std::memset(yCal, 0, sizeof(double)*nPtCal); tmp += 6; for (int i = 0; i &lt; (nPt - 1); i ++) { *pPiece = *y; f2 = *(y + 1); f1 = *y; x2 = (i + 1) * interval; x1 = i * interval; m = (f2 - f1)/(x2 - x1); b = f2 - m * x2; for (int k = 1; k &lt; interval; k++) { pPiece[k] = m * ((i * interval) + k) + b; } std::memcpy(tmp + (i * interval), pPiece, sizeof(double)*interval); y++; } std::memcpy(yCal + 6, tmp, sizeof(double)*250); return yCal; } double * CubInterp(double * y, int nPt, int nPtCal) { double * yCal = new double[nPtCal]; std::memset(yCal, 0, sizeof(double)*nPtCal); return yCal; } int main(int argc, char* argv[]) { if (argc &lt; 3) { show_usage(argv[0]); return 1; } std::vector &lt;std::string&gt; sources; std::string destination; for (int i = 1; i &lt; argc; ++i) { std::string arg = argv[i]; if ((arg == "-h") || (arg == "--help")) { show_usage(argv[0]); return 0; } else if ((arg == "-d") || (arg == "--destination")) { if (i + 1 &lt; argc) { // Make sure we aren't at the end of argv! destination = argv[i++]; // Increment 'i' so we don't get the argument as the next argv[i]. } else { // Uh-oh, there was no argument to the destination option. std::cerr &lt;&lt; "--destination option requires one argument." &lt;&lt; std::endl; return 1; } } else { sources.push_back(argv[i]); } } int nPt = 26; double * y = new double[nPt]; LoadData(sources[0], y); int nPtCal = 256; double * yCal = new double[nPtCal]; yCal = LinInterp(y, nPt, nPtCal); std::string fileName = "D:\\inter.txt"; OutpuResults(fileName, yCal, nPtCal); getchar(); return 1; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload