英文:
C++ strlen function bug
问题
我遇到了一个非常奇怪的 bug,不知道为什么会发生,Visual Studio 显示这是 C++ 语言文件 "xstring" 中的一个 bug,它告诉我与 strlen() 函数有关。
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
	string args;
	string file;
	for (int i = 0; i < sizeof(argv); i++) {
		args = args + argv[i];
	}
	if (args.empty()) {
		while (true) {
			cout << "指定应用程序路径。\n";
			cout << ">>>";
			cin >> file;
		}
	}
	else {
		cout << "正在加载文件... " << argv[1] << "\n";
	}
}
我尝试找到解决方案,但没有一个解决方案有效。
英文:
I have encountered a very weird bug, i don't know why is this happening and visual studio says it is a bug in the cpp language file "xstring". it tells me it is something with the strlen() function.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
	string args;
	string file;
	for (int i = 0; i < sizeof(argv); i++) {
		args = args + argv[i];
	}
	if (args.empty()) {
		while (true) {
			cout << "Specify A Path To An App.\n";
			cout << ">>>";
			cin >> file;
		}
	}
	else {
		cout << "Loading File... " << argv[1] << "\n";
	}
}
I tried to find a solution but no solution worked.
答案1
得分: 2
sizeof(argv) 不是参数的数量,而是指针 char** 的字节数。您应该使用参数 argc 来获取参数的数量。
英文:
sizeof(argv) is not the number of arguments but the number of bytes  of a pointer char**. You should use the argument argc to retrieve the number of arguments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论