【问题标题】:ofstream error using << operator使用 << 运算符的 ofstream 错误
【发布时间】:2013-03-14 13:38:01
【问题描述】:

当我使用 ofstream 将双精度数组输出到 txt 文件时出现错误。代码如下:

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 < nPtCal; i++)
    {
        outfile<<yCal_local[i]<<std::endl;
    }

    delete[] yCal_local;
    outfile.close();    
}

错误发生在outfile&lt;&lt;yCal_local[i]&lt;&lt;std::endl;,其中 i = 0,yCal_local[i] 是 0.000000 作为双精度数。对我来说毫无意义。

这里是文件名的定义:

std::string fileName = "d:\\inter.txt";

这里是yCal的定义:

int nPtCal = 256;
double * yCal = new double[nPtCal];

我想知道我哪里做错了?任何建议都非常感谢。

编辑: 编译时没有问题,但运行时出错。 这是错误消息:

Unhandled exception at 0x75f5812f in ppppp.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0028f754..

这是程序从错误返回时显示的代码:

void __cdecl _unlock (
        int locknum
        )
{
        /*
         * leave the critical section.
         */
        LeaveCriticalSection( _locktable[locknum].lock );
}

编辑:

所有代码如下:

#include "pppp.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>


static void show_usage(std::string name)
{
    std::cerr << "To use ZoomCal:\n " << "ZoomCal.exe inputfile -options\n"
              << "options:\n"
              << " -l: linear\n"
              << " -c: cubic"<< 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<<"File loading failed. Double check if file exists in the same folder as ZoomCal.exe"<<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 < nPtCal; i++)
    {
        outfile<<yCal[i]<<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 < (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 < 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 < 3) 
    {
         show_usage(argv[0]);   
         return 1;
    }

    std::vector <std::string> sources;
    std::string destination;

    for (int i = 1; i < 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 < 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 << "--destination option requires one argument." << 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;     
}

【问题讨论】:

  • 是编译器错误吗?你能告诉我们错误吗?
  • 对不起,让我再编辑一下。
  • 你有更多的问题:首先你说你想写入一个文本文件,但是你以二进制模式打开文件。最严重的问题可能是函数末尾的delete,特别是如果您想在调用后使用传递给函数的数组。
  • 错误是什么?此外,创建 yCal_local 并没有按照您的想法进行。当您调用 delete [] yCal_local 时,实际上是在释放 yCal 指向的内存,因为 yCal 和 yCal_local 具有相同的值。如果你在 OutpuResults 返回后尝试访问内存,你应该得到一个错误。
  • @JoachimPileborg:那么如果我想每次都覆盖,应该使用什么模式呢?

标签: c++ file-io ofstream


【解决方案1】:

好吧,我终于知道我做错了:

tmp += 6;

我的程序是将一个包含 26 个元素的数组插入到 256 个元素中。但是插值数组的前 6 个元素必须为 0;所以我创建了一个长度为 256-6 = 250 的新数组;

double nPtCal = 256;
double * tmp = new double[nPtCal-5];

但不知何故,我也将这个 tmp 指针移动了 6。我想如果我创建一个长度为 256 的数组是正确的;因此,发生错误也就不足为奇了。因为当运算填充 256-6 = 250 个元素时,如果指针已经指向总共 250 个元素中的第 6 个元素,那么最终会超出范围,因为我基本上会将指针移动 250 次以处理所有 250 个元素在新数组中。 这就是我不断出错的原因。我以为是因为一些文件IO,但现在我认为不是。 感谢你的帮助。非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多