gpt4 book ai didi

c++ - 第 929 行 xstring 断言无效的空指针

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:23 34 4
gpt4 key购买 nike

当我在 visual studio 上调试时它会运行但是当我运行 .exe 时我收到以下错误:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring929号线表达式:无效的空指针

这是我的代码。

#include "stdafx.h"
#include <io.h> //For access
#include <sys/types.h> //For stat().
#include <sys/stat.h> //For stat().
#include <iostream>
#include <string>
#include <direct.h>
#include <fstream>
#include <stdio.h>

using namespace std;

int _tmain(int argc, char* argv[]){

string* argument = new string();
string strPath = argv[1];
argument = nullptr;
argument = &strPath;

if ( access( strPath.c_str(), 0 ) == 0 ){

struct stat status;
stat( strPath.c_str(), &status );

if ( status.st_mode & S_IFDIR ){
cout << "The directory exist." << endl;
getc(stdin);
}
}
else{
mkdir( "C:\\Program Files\\EZshred\\" );
getc( stdin );
}

return 0;
}

我试过了,但我不知道还能做什么,请帮忙

最佳答案

这是一些更新的代码。由于除了 strPath.c_str() 之外,您从未真正使用过 strPath,因此没有任何理由将其包装在 string 中。当然 string 如果您要解析或拆分或添加到它或其他任何东西,它更适合一般用途,但在这种情况下,它真的没有必要。还去掉了 argument,因为它似乎没有必要。此外,您调用 access() 时有问题,第二个参数为零。 F_OK 在 Linux 系统上恰好被定义为零,但我不确定它在任何地方都是如此。此外,由于您正在使用 _tmain(),因此最好也使用 _TCHAR* argv[],尤其是在构建 Unicode 或其他宽-char 项目(尽管我在这方面远非权威,很久以前就放弃了 M$...)。如果是这样,我不知道 access()stat() 等是否为非 char 适当定义类型,但我希望它们是...否则您需要先转换字符串...

#include "stdafx.h"
#include <io.h> //For access
#include <sys/types.h> //For stat().
#include <sys/stat.h> //For stat().
#include <iostream>
#include <string>
#include <direct.h>
#include <fstream>
#include <stdio.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[]){

// make sure we have at least argv[1] and that it is not NULL
// (shouldn't happen in theory, but paranoia is good sometimes),
// and that it does not point to an empty string
if ( (argc < 2) || (argv[1] == NULL) || (*(argv[1]) == '\0') )
return -1; // probably should print an error message here

if ( !access( argv[1], F_OK ) ){

struct stat status;
stat( argv[1], &status );

if ( status.st_mode & S_IFDIR ){
cout << argv[1] << "can be accessed and is a directory." << endl;
getc(stdin);
}
}
else{
mkdir( "C:\\Program Files\\EZshred\\" );
getc( stdin );
}

return 0;
}

关于c++ - 第 929 行 xstring 断言无效的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20361867/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com