无法输出到文本文件,文件为空。

3
我尝试将一堆变量输出到一个以制表符分隔的文本文件中,但是一旦程序完成后,生成的文件完全为空。
string outFileName = "/Users/hbll-diteam/Desktop/" + identifier + ".csv";
        ofstream out(outFileName.c_str());
        out.open(outFileName);
        if(out.is_open())
        {
            //cout << ";eruigjnsldfijuglsidufblg yay";
            out << coCDM_LVL << '\t' << coCDM_LVLname << '\t' << creator << '\t' << contributors << '\t' << coTitle << '\t' << altTitle << '\t' << description << '\t' << dateOriginal << '\t' << dateSpan << '\t' << edition << '\t' << publisher << '\t' << physicalDescription << '\t' << scale << '\t' << extentField << '\t' << medium << '\t' << dimensions << '\t' << arrangement << '\t' << degree << '\t' << contributing << '\t' << names << '\t' << topics << '\t' << geoPlaceNames << '\t' << genre << '\t' << occupations << '\t' << functions << '\t' << subject << '\t' << langIN << '\t' << audience << '\t' << condition << '\t' << generalNotes << '\t' << collection << '\t' << linkToFindingAid << '\t' << source << '\t' << SIRSI << '\t' << callNumber << '\t' << coFullText << '\t' << COPYRIGHT << '\t' << rightsManagement << '\t' << rightsHolder << '\t' << contactAddress << '\t' << contactPhone << '\t' << contactEmail << '\t' << ACCESS_LEVEL << '\t' << PUBLISHER_DIGITAL << '\t' << refreshDate << '\t' << fileType << '\t' << format << '\t' << digitizationSpecs << '\t' << dateDigital << '\t' << height << '\t' << width << '\t' << checksum << '\t' << CHECKSUM << '\t' << colorSpace << '\t' << systemRequirements << '\t' << username << '\t' << metaEntryDate << '\t' << metaEntryTool << '\t' << identifier << '\t' << fileSize << '\t' << mediaType << '\t' << metaUser << '\t' << LIS_TAG << '\t' << identifier << endl;

            for(int i = 0; i < filenameList.size(); i++)
            {
                out << pgCDM_LVL << '\t' << pgCDM_LVLname << '\t' << '\t' << '\t' << "Page " << i + 1 << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t' << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t' << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t'  << '\t' << COPYRIGHT << '\t' << rightsManagement << '\t' << rightsHolder << '\t' << contactAddress << '\t' << contactPhone << '\t' << contactEmail << '\t' << ACCESS_LEVEL << '\t' << '\t' << '\t' << '\t' << format << '\t' << digitizationSpecs << '\t' << dateDigital << '\t' << '\t' << '\t' << '\t' << CHECKSUM << '\t' << '\t' << '\t' << '\t' << '\t' << '\t' << filenameList[i] << '\t' << '\t' << '\t' << '\t' << '\t' << filenameList[i] << endl;
            }
            out.close();
        }

当你在调试器中运行它(或添加打印语句)时,它是否告诉你文件是否已打开? - John3136
是的,文件已经打开(通过注释掉的cout语句进行了检查)。 - penguin
1个回答

6
您正在两次打开该文件,第二次失败了,但您并没有检查失败情况。
ofstream out(outFileName.c_str());

打开文件

out.open(outFileName);

尝试打开文件并失败,将流置于错误状态。 is_open 可能不检查错误状态。


运行完美!谢谢。 - penguin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接