【发布时间】:2020-07-20 10:27:45
【问题描述】:
我试图在 getPath() 函数中从用户那里获取文件路径,并将路径作为字符串返回。我遇到了麻烦,因为编译器说我需要使用 const char 而我不知道该怎么做。我将如何使用 const chars 以及它们是什么。另外我如何像在主函数中一样将它们打印到控制台。
#include <iostream>
#include <stdio.h>
#include <string.h>
char getPath() {
char path[64];
std::cout << "Input File Name For Debugging:";
gets(path);
std::cout << "Debugging: ";
puts(path);
return path[64];
}
int main(){
char path[64];
int pathlen = strlen(reinterpret_cast<const char *>(path));
//suppost to print the char array
for(int i; i < pathlen; i++){
std::cout << path[i];
}
return 0;
}
【问题讨论】:
-
旁白:
getPath没有正确设计,也没有被调用。您的代码中的许多内容都未初始化。 -
你在哪里学习这些?您的代码是带有
std::cout的C,而不是C++。此外,gets是邪恶的。它在 C++11 中已弃用并在 C++14 中删除。您正在学习“C with classes”,这不是正确的 C++。 -
我知道这些函数是不受保护的,很容易溢出。我正在使用它来学习二进制利用。我从 cplusplus.com 获得了大部分内容,如果有更好的网站,请告诉我。
-
我并不是要冒犯你,但在尝试学习二进制利用之前,你应该真正掌握编程。您知道
gets的问题,所以除非您尝试编写易受攻击的程序,否则不要使用它。从网站上学习基本的 C++ 通常不会有好的结果,所以你应该得到一个 good book 来代替。
标签: c++ string printing char const-char