需要帮助诊断C ++程序中的错误,该程序旨在从XML文件中提取时间戳

时间:2015-11-22 08:22:02

标签: c++ xml

在一些帮助下,我几乎完成了一个程序,它使我能够提取时间戳(例如:timestamp =" 2014-07-08T18:14:16.468Z")并且只有时间戳来自和XML文件并将它们输出到指定的输出文件。但是,我的代码中仍然存在一些错误,这些错误使我处于智慧结束,这似乎无法纠正。更有经验的C ++人会帮助我吗? 错误出现在第35,38行,& 47。

错误屏幕截图:http://i.imgur.com/jVUig4T.jpg

链接到XML文件:http://pastebin.com/DLVF0cXY

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
using namespace std;
string tempStr;
// escaped double qoute.
string findStr = "timestamp=\"";

ifstream inFile;
ofstream outFile;
outFile.open("Outputdata.txt");
inFile.open("Groupproject.xml");
if (inFile.fail()) {
    cout << "Error Opening File" << endl;
    system("pause");
    exit(1);
}

size_t found;

while (inFile) {
    getline(inFile, tempStr);
    found = tempStr.find(findStr);
    if (found != std::string::npos)
    {
        break;
    }
}

// Erases from beggining to end of timestamp="
tempStr.erase(tempStr.begin(), (found + tempStr.length()));

// Finds index of next double qoute.
found = tempStr.findStr("\"");

if (found = std::string::npos)
{
    cerr << "Could not find matching qoute:";
    exit(1);
}

// Erases from matching qoute to the end of the string.
tempStr.erase(found, tempStr.end());

cout << "timestamp found" << tempStr << "Saving to outFile" << endl;

outFile << tempStr;

inFile.close();
outFile.close();
system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:0)

您确定仔细阅读了所有正在使用的功能的参考资料吗? Your new friend

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
    string tempStr;
    string findStr = "timestamp=\"";  // escaped double quote

    ifstream inFile;
    ofstream outFile;
    outFile.open( "Outputdata.txt" );
    inFile.open( "Groupproject.xml" );
    if ( inFile.fail() )
    {
        cout << "Error Opening File" << endl;
        cin.get();
        exit( 1 );
    }

    size_t found;

    while ( inFile )
    {
        getline( inFile, tempStr );
        cout << tempStr << endl;
        found = tempStr.find( findStr );
        if ( found != string::npos )
            break;
    }
    tempStr.erase( 0, found + findStr.length() ); // erases from beggining to end of timestamp="

    found = tempStr.find( "\"" ); // finds index of next double quote

    if ( found == string::npos )
    {
        cerr << "Could not find matching quote" << endl;
        exit( 1 );
    }

    tempStr.erase( found, string::npos ); // erases from matching quote to the end of the string.

    cout << "timestamp found:" << tempStr << " Saving to outFile" << endl;

    outFile << tempStr;

    inFile.close();
    outFile.close();
    cin.get();
    return 0;
}