当从cygwin调用时,C ++ exe将不同的文本输出写入文件

时间:2014-07-21 17:46:46

标签: c++ encoding cygwin ofstream

我需要帮助,在我看来这是一个神秘的问题,我无法看到如何解决它。

我写了一个C ++ exe。它有一个非常简单的任务,即读取包含以下行的小文本文件:

house <CLASSMISSING> missing    
roof <CLASSMAJORWEAR> major wear

对TAGS进行一些比较,并将非常简单的结果(文本)写入.csv文件。 输出看起来像:

house 11; MISSING

此输出由以下产生:

fout << argv[3] << ";" << sC_damages.get_damage(sC_damages.worst_dam_ID).c_str() << "\n";

其中argv [3]是一个标识符字符串,在这种情况下,“house 11”传递给命令行上的程序,sC_damages.get_damage在这种情况下输出一个字符串“MISSING”。 结果将按以下方式写入以追加模式打开的文件:

ofstream fout(argv[2], ios::app);

当我从CMD(Win7)或Visual Studio 2010中运行时,一切都很好。但我需要/想要从cygwin下的bash脚本运行它。

当我从cygwin32 bash命令行多次调用C ++程序时:

cygdrive/d/CCpp/program.exe "out_tmp//house11.txt" houses.csv "house 11"

然后查看输出文件houses.csv中的结果(对于每个相同的命令行调用,应该追加相同的结果)我得到以下灾难:

house 11;MISSING
house 11;MISSING
house 11;™
ï2åK¨vhouse 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11òëc‰çÁu*Ihouse 11;MISSING

所以偶尔会插入神秘的人物。它没有任何模式。 从CMD调用时相同的输出看起来很完美:

house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING
house 11;MISSING

我真的无法理解当从CMD或cygwin bash调用程序时,ofstream(我在C ++程序中使用它来写入文件houses.csv)会产生不同的输出。

请帮忙!

P.S。

代码如下:

#include <fstream>
#include <iostream>
#include "damageClass.h"

using namespace std;

int main( int argc, char **argv)
{
    char ch;
    int dam_ID = 0;
    int dam_ID_tmp = 0;
    char sc_damage[80];
    char dump[256];
    string uT_Tag;
    string source = argv[3];


    damages sC_damages;
    sC_damages.reset();

    //check correct number command line arguments
    if ( argc != 4 )
    {
        cout << "Usage: " << argv[0] << "<input concord.txt filename> <output csvfilename> <reportfilename> " << endl;
        return(1);
    }


    //input concord.txt file
    ifstream fin(argv[1]);

    //outputcsv file
    ofstream fout(argv[2], ios::app);
    if ( !fout )
    {
        cout << "Unable to open " << argv[2] << " in append mode.\n";
        return (1);
    }

    while ( !fin.eof() )
    {
        while ( fin.get(ch) )
        {

            uT_Tag = "";
            if ( ch == '<' ) //if line in concord.txt starts with <
                fin.putback( '<' );

            //read out damage tag from in between <>
            if (fin.peek() == '<')
            {
                fin.ignore(1, '<');
                fin.getline(sc_damage, 80, '>');
                fin.getline(dump, 256);
                uT_Tag = sc_damage;
                int weight = sC_damages.get_dam_weight(uT_Tag); // weight= only for debug output. Class call necessary to compute worst_dam_ID
                break;
            }
        }
    }

    cout << "writing: " << argv[3] << "; " << sC_damages.get_damage(sC_damages.worst_dam_ID) << endl;
    fout << argv[3] << ";" << sC_damages.get_damage(sC_damages.worst_dam_ID).c_str() << "\n";

    fout.close();
    fin.close();
    return 0;
}

这是类的定义:

#include "damageClass.h"

//const char *damageTags[5] = { "CLASSOK", "CLASSMINORWEAR", "CLASSMAJORWEAR", "CLASSBROKEN", "CLASSMISSING"};
//const char *damages[5] = { "NODAMAGE", "MINORWEAR", "MAJORWEAR", "BROKEN", "MISSING"};

damages::damages()
{
    this->damageClasses.clear();

    damageClass dC;
    dC.m_damageTag = "";
    dC.m_damage = "NOFINDING";
    dC.dam_weight = 0;
    this->damageClasses.push_back(dC);

    dC.m_damageTag = "CLASSOK";
    dC.m_damage = "NODAMAGE";
    dC.dam_weight = 1;
    this->damageClasses.push_back(dC);

    dC.m_damageTag = "CLASSMINORWEAR";
    dC.m_damage = "MINORWEAR";
    dC.dam_weight = 2;
    this->damageClasses.push_back(dC);

    dC.m_damageTag = "CLASSMAJORWEAR";
    dC.m_damage = "MAJORWEAR";
    dC.dam_weight = 3;
    this->damageClasses.push_back(dC);

    dC.m_damageTag = "CLASSBROKEN";
    dC.m_damage = "BROKEN";
    dC.dam_weight = 4;
    this->damageClasses.push_back(dC);

    dC.m_damageTag = "CLASSMISSING";
    dC.m_damage = "MISSING";
    dC.dam_weight = 5;
    this->damageClasses.push_back(dC);
}

damages::~damages()
{
    this->damageClasses.clear();
}

int damages::get_dam_weight(string UT_damageTag)
{
    for ( unsigned int i = 0; i < this->damageClasses.size(); i++ )
    {
        damageClass dC = this->damageClasses.at(i);
        if (dC.m_damageTag == UT_damageTag)
        {
            this->worst_dam_ID = max(dC.dam_weight, this->worst_dam_ID);
            return dC.dam_weight;
        }
    }
    return 0;
}

string damages::get_damage(int dam_weight)
{
    for ( unsigned int i = 0; i < this->damageClasses.size(); i++ )
    {
        damageClass dC = this->damageClasses.at(i);
        if (dC.dam_weight == dam_weight)
        {
            return dC.m_damage;
        }
    }
    return "ERROR: no matching damage class";
}

void damages::reset()
{
    this->worst_dam_ID = 0;
}

这是类的声明:

#include <string>
#include <vector>

using namespace std;


//class description
class damageClass 
{
public:
    //attributes
    string m_damageTag;
    string m_damage;
    int dam_weight;
};


class damages
{
public:
    damages(void);
    ~damages(void);

    //attributes
    vector<damageClass> damageClasses;
    int worst_dam_ID;

    //methods
    int get_dam_weight(string UT_damageTag);
    string get_damage(int dam_weight);
    void reset();
};

1 个答案:

答案 0 :(得分:0)

是的,这是一个困难的问题。我正在使用SafeGuard LanCrypt。显然,当在Cygwin下附加文件时,会产生随机标志。 写入未加密的共享可以解决问题。