C++ 用tinyxml解析xml文档

北极光的征兆_liekkas 2016-09-06 12:23:24
#include <iostream>
#include<conio.h>
#include "tinyxml.h"
using namespace::std;
void main()
{
cout<<"1"<<endl;

TiXmlDocument doc;
const char * xmlFile = "school.xml";
if(doc.LoadFile(xmlFile))
{
doc.Print();
}
else
{
cout<<"can not opt xml"<<endl;
}

getch();
}


school.xml文档和我的main.cpp文件在同一个路径在里面,为什么运行代码直接输出"can not opt xml"?
...全文
5517 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灸舞 2016-09-06
  • 打赏
  • 举报
回复
看你用的啥IDE了
用完整路径试试
paschen 版主 2016-09-06
  • 打赏
  • 举报
回复
建议使用完整的路径名,否则在调试程序时,当前路径一般为工程目录,直接运行时一般为EXE所在目录
孤雲独去闲 2016-09-06
  • 打赏
  • 举报
回复
和cpp文件在一起没用,得和生成的exe文件在一个路径里面。
张小飞Official 2016-09-06
  • 打赏
  • 举报
回复
所以有时候写绝对路径还是挺不错的
赵4老师 2016-09-06
  • 打赏
  • 举报
回复
会用相关函数获取当前目录和设置当前目录吗? 仅供参考:
#include <sys\stat.h>
#include <io.h>
#include <fcntl.h>
#include <share.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX_CLU_BYTES 65536
FILE *fo;
int fh;
__int64 offs,offs1;
__int64 rvi64;
int rv,wrv;
unsigned char buf[MAX_CLU_BYTES];
char ofn[_MAX_PATH];
char offstr[80];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char fullpath[_MAX_PATH];
void strcpybutcomma(char *t,char *s) {
    char c;

    while (1) {
        c = *s++;
        if (','!=c) *t++ = c;
        if (0==c) break;
    }
}
int main(int argc,char **argv) {
    if (argc<3) {
        printf("Copy File Tail.\n");
        printf("Usage:\n");
        printf("    cft filename.ext {Offset_Begin|-Offset_End|Offset_Begin-Offset_End}\n");
        printf("Copy filename.ext {OB|-OE|OB-OE} to {OB|~OE|OB-OE}-filename.ext\n");
        printf("Note: Byte at OE is NOT included.\n");
        printf("Example:\n");
        printf("    cft abc.rar 12345\n");
        printf("Copy abc.rar offset 12345-end to 12345-abc.rar\n");
        printf("    cft abc.rar -12345\n");
        printf("Copy abc.rar offset -12345-end to ~12345-abc.rar\n");
        printf("    cft abc.rar 123-12345\n");
        printf("Copy abc.rar offset 123-12345 to 123-12345-abc.rar\n");
        printf("    cft abc.rar 0xAB-0xCD\n");
        printf("Copy abc.rar offset 0xAB-0xCD to 0xAB-0xCD-abc.rar\n");
        return 1;
    }
    strcpybutcomma(offstr,argv[2]);
    rv=sscanf(offstr,"%I64i-%I64i",&offs,&offs1);
    if (rv==0) {
        printf("offset %s is not number\n",argv[2]);
        return 2;
    }
    if (NULL==_fullpath(fullpath,argv[1],_MAX_PATH)) {
        printf("Can not get fullpath of %s\n",argv[1]);
        return 10;
    }
    fh=_sopen(fullpath,_O_BINARY|_O_RDONLY|_O_RANDOM,_SH_DENYNO);
    if (fh==-1) {
        printf("_sopen %s errno=%d %s\n",fullpath,errno,strerror(errno));
        return 3;
    }
    if (rv==1) {
        offs1=_filelengthi64(fh);
        if (offs1==-1i64) {
            printf("%I64d=_filelengthi64 errno=%d %s\n",offs1,errno,strerror(errno));
            _close(fh);
            return 4;
        }
        if (offs<0i64) {
            offs=offs1+offs;
            if (offs<0i64) offs=0i64;
        }
    } else {//rv==2
        if (offs>offs1) {
            printf("%s offset_begin %I64d > %I64d offset_end error\n",argv[2],offs,offs1);
            _close(fh);
            return 5;
        }
    }
    rvi64=_lseeki64(fh,offs,SEEK_SET);
    if (rvi64!=offs) {
        printf("%I64d=_lseeki64 %I64d errno=%d %s\n",rvi64,offs,errno,strerror(errno));
        _close(fh);
        return 6;
    }
    _splitpath(fullpath,drive,dir,fname,ext);
    if (offstr[0]=='-') offstr[0]='~';
    sprintf(ofn,"%s%s%s-%s%s",drive,dir,offstr,fname,ext);
    fo=fopen(ofn,"wb");
    if (fo==NULL) {
        printf("fopen %s errno=%d %s\n",ofn,errno,strerror(errno));
        _close(fh);
        return 7;
    }
    cprintf("\n%I64d\r",offs);
    while (1) {
        rv=_read(fh,buf,(unsigned int)__min(offs1-offs,MAX_CLU_BYTES));
        if (rv==0) break;//
        if (rv<0) {
            printf("_read %s offset %I64d errno=%d %s\n",fullpath,offs,errno,strerror(errno));
            fclose(fo);
            _close(fh);
            return 8;
        }
        wrv=fwrite(buf,1,rv,fo);
        if (wrv!=rv) {
            printf("fwrite %s errno=%d %s\n",ofn,errno,strerror(errno));
            fclose(fo);
            _close(fh);
            return 9;
        } else {
            offs+=rv;
            cprintf("%I64d\r",offs);
            if (offs>=offs1) break;//
        }
    }
    fclose(fo);
    _close(fh);
    printf("Copy %s offset %s to %s OK.\n",fullpath,argv[2],ofn);
    return 0;
}
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
在逗我吗?想表达什么 英语也是一门计算机语言的说。
在逗我吗?想表达什么
赵4老师 2016-09-06
  • 打赏
  • 举报
回复
英语也是一门计算机语言的说。
  • 打赏
  • 举报
回复
引用 1 楼 yangyangsnr 的回复:
和cpp文件在一起没用,得和生成的exe文件在一个路径里面。
我试过,只有在根目录下才行 const char * xmlFile = "D:\\student.xml"; 不知道为啥
  • 打赏
  • 举报
回复
引用 5 楼 sdghchj 的回复:
编译目录跟运行目录一般不是一样的
确实是。。这次问题出在这里
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
_getcwd, _wgetcwd Get the current working directory. char *_getcwd( char *buffer, int maxlen ); wchar_t *_wgetcwd( wchar_t *buffer, int maxlen ); Routine Required Header Compatibility _getcwd <direct.h> Win 95, Win NT _wgetcwd <direct.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters. Parameters buffer Storage location for path maxlen Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd Remarks The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer). _getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash. _wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tgetcwd _getcwd _getcwd _wgetcwd Example // GETCWD.C /* This program places the name of the current directory in the * buffer array, then displays the name of the current directory * on the screen. Specifying a length of _MAX_PATH leaves room * for the longest legal path name. */ #include <direct.h> #include <stdlib.h> #include <stdio.h> void main( void ) { char buffer[_MAX_PATH]; /* Get the current working directory: */ if( _getcwd( buffer, _MAX_PATH ) == NULL ) perror( "_getcwd error" ); else printf( "%s\n", buffer ); } Output C:\code Directory Control Routines See Also _chdir, _mkdir, _rmdir _chdir, _wchdir Change the current working directory. int _chdir( const char *dirname ); int _wchdir( const wchar_t *dirname ); Routine Required Header Optional Headers Compatibility _chdir <direct.h> <errno.h> Win 95, Win NT _wchdir <direct.h> or <wchar.h> <errno.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a value of 0 if successful. A return value of –1 indicates that the specified path could not be found, in which case errno is set to ENOENT. Parameter dirname Path of new working directory Remarks The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive and if a new drive letter is specified in dirname, the default drive letter will be changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive: _chdir("c:\\temp"); When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\). _wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise. Generic-Text Routine Mapping: TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tchdir _chdir _chdir _wchdir Example /* CHGDIR.C: This program uses the _chdir function to verify * that a given directory exists. */ #include <direct.h> #include <stdio.h> #include <stdlib.h> void main( int argc, char *argv[] ) { if( _chdir( argv[1] ) ) printf( "Unable to locate the directory: %s\n", argv[1] ); else system( "dir *.wri"); } Output Volume in drive C is CDRIVE Volume Serial Number is 0E17-1702 Directory of C:\write 04/21/95 01:06p 3,200 ERRATA.WRI 04/21/95 01:06p 2,816 README.WRI 2 File(s) 6,016 bytes 71,432,116 bytes free Directory Control Routines See Also _mkdir, _rmdir, system
老师,没有注释啊
sdghchj 2016-09-06
  • 打赏
  • 举报
回复
编译目录跟运行目录一般不是一样的
赵4老师 2016-09-06
  • 打赏
  • 举报
回复
_getcwd, _wgetcwd Get the current working directory. char *_getcwd( char *buffer, int maxlen ); wchar_t *_wgetcwd( wchar_t *buffer, int maxlen ); Routine Required Header Compatibility _getcwd <direct.h> Win 95, Win NT _wgetcwd <direct.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters. Parameters buffer Storage location for path maxlen Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd Remarks The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer). _getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash. _wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tgetcwd _getcwd _getcwd _wgetcwd Example // GETCWD.C /* This program places the name of the current directory in the * buffer array, then displays the name of the current directory * on the screen. Specifying a length of _MAX_PATH leaves room * for the longest legal path name. */ #include <direct.h> #include <stdlib.h> #include <stdio.h> void main( void ) { char buffer[_MAX_PATH]; /* Get the current working directory: */ if( _getcwd( buffer, _MAX_PATH ) == NULL ) perror( "_getcwd error" ); else printf( "%s\n", buffer ); } Output C:\code Directory Control Routines See Also _chdir, _mkdir, _rmdir _chdir, _wchdir Change the current working directory. int _chdir( const char *dirname ); int _wchdir( const wchar_t *dirname ); Routine Required Header Optional Headers Compatibility _chdir <direct.h> <errno.h> Win 95, Win NT _wchdir <direct.h> or <wchar.h> <errno.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a value of 0 if successful. A return value of –1 indicates that the specified path could not be found, in which case errno is set to ENOENT. Parameter dirname Path of new working directory Remarks The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive and if a new drive letter is specified in dirname, the default drive letter will be changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive: _chdir("c:\\temp"); When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\). _wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise. Generic-Text Routine Mapping: TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tchdir _chdir _chdir _wchdir Example /* CHGDIR.C: This program uses the _chdir function to verify * that a given directory exists. */ #include <direct.h> #include <stdio.h> #include <stdlib.h> void main( int argc, char *argv[] ) { if( _chdir( argv[1] ) ) printf( "Unable to locate the directory: %s\n", argv[1] ); else system( "dir *.wri"); } Output Volume in drive C is CDRIVE Volume Serial Number is 0E17-1702 Directory of C:\write 04/21/95 01:06p 3,200 ERRATA.WRI 04/21/95 01:06p 2,816 README.WRI 2 File(s) 6,016 bytes 71,432,116 bytes free Directory Control Routines See Also _mkdir, _rmdir, system

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧