使用文件操作解析XMLFile

时间:2019-03-26 18:56:26

标签: c++

我正在尝试使用文件操作编写XML解析器,但我得到了XML文件的指针。我正在尝试使用fgetc函数逐个字符地读取字符,但是它不起作用。

std::string sFileName = "C:\\Users\\simple\\Desktop\\XMLFile.xml";
std::string sTempstring;
char cXML;
FILE *fptr;
fptr = fopen(sFileName.c_str(),"r");
fseek(fptr, 0L, SEEK_END);
long int iLength = ftell(fptr);
const int bufSize = 412;

char* tempdata = new char[iLength]();
if (fptr != NULL)
{
    int i = 0;
    while ((cXML = fgetc(fptr)) != EOF)
    {
        cout << cXML;
        i++;
    }
    sTempstring.append(tempdata);
    cout << sTempstring.c_str();

}
else
{
    cout << "File is empty";
}
getchar();
return 0;

请让我知道我在做什么错误。  我想通过使用文件操作读取xml文件来将XML信息存储在某个字符串中,所以我已经编写了上述功能

1 个答案:

答案 0 :(得分:0)

fptr = fopen(sFileName.c_str(),"r");
fseek(fptr, 0L, SEEK_END);
long int iLength = ftell(fptr);
fseek(fptr, 0L, SEEK_SET);

添加 fseek(fptr,0L,SEEK_SET)的特定位置。 http://www.cplusplus.com/reference/cstdio/fseek/
希望您的研究成功完成。