字符串相互比较和追加的问题
Problems with comparing and appending strings to each other c++
我正在编写一些代码,它应该做的是返回文件的路径,而不包含文件名本身。在这里
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main() {
char binaryPath[MAX_PATH]; //I think my problems stem from the fact that
//isnt truly a string
GetModuleFileName(NULL, binaryPath, MAX_PATH); //Get filename
string i = (string) binaryPath;
string fileDir = "";
int tempSlashes = 0;
for (int x = 0; x < strlen(binaryPath); x++) {
if (i[x] == '') { //In eclipse CDT this gives warning 1 (below)
tempSlashes++;
}
}
for (int y = 0; y < tempSlashes; y++) {
fileDir.append(binaryPath[y]); //Line gives warning 2
}
cout << fileDir;
return 0;
}
不管怎样,小小的警告注释
Error 1:
Multiple markers at this line
- comparison with string literal results in unspecified behaviour [-
Waddress]
- ISO C++ forbids comparison between pointer and integer [-
fpermissive]
Error 2:
Multiple markers at this line
- Invalid arguments ' Candidates are: std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const
std::basic_string<char,std::char_traits<char>,std::allocator<char>> &) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const
std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, unsigned int, unsigned int) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const
char *, unsigned int) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const char *) std::basic_string<char,std::char_traits<char>,std::allocator<char>> &
append(unsigned int, char) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(#10000, #10000) '
- invalid conversion from 'char' to 'const char*' [-fpermissive]
程序本身拒绝编译,并运行较早的。exe
我不确定我在这里做错了什么,帮助(以及解释)将是非常好的。
谢谢!
我建议向后迭代并寻找''。例如:
int len = strlen(binaryPath);
char outPath[MAX_PATH];
strcpy(outPath, binaryPath);
for (int x = len - 1; x >= 0; --x) {
outPath[x] = 0;
if (binaryPath[x] == '') {
break;
}
}
首先,在现代Win32 c++代码中,您可能希望使用Unicode代替ANSI/MBCS(因此,wchar_t
代替char
, std::wstring
代替std::string
)。
对于您的特定问题,您可能希望使用现有的Win32 API: PathRemoveFileSpec().
下面是可编译代码示例:
#define UNICODE
#define _UNICODE
#include <iostream>
#include <string>
#include <Windows.h>
#include <Shlwapi.h>
using namespace std;
#pragma comment(lib, "Shlwapi.lib")
int main()
{
wchar_t binaryPath[MAX_PATH];
GetModuleFileName(nullptr, binaryPath, MAX_PATH);
PathRemoveFileSpec(binaryPath);
// You can assign to a std::wstring as well...
wstring fileDir = binaryPath;
wcout << fileDir << endl;
}
下面是一个示例测试:
C:TempCppTests>cl /EHsc /W4 /nologo /MTd test.cpp test.cpp C:TempCppTests>test.exe C:TempCppTests
作为一种替代方法,一旦您在原始C缓冲区中读取了带有文件名的完整路径:
wchar_t binaryPathBuffer[MAX_PATH];
GetModuleFileName(nullptr, binaryPathBuffer, MAX_PATH);
您可以将其复制到std::wstring
中,然后使用其 rfind()
方法找到最后一个''
的位置,然后使用std::wstring
的 substr
方法提取适当的子字符串:
wstring binaryPath = binaryPathBuffer;
size_t last = binaryPath.rfind(L'');
wstring fileDir = binaryPath.substr(0, last);
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 追加/打印/插入数组c++函数问题
- 字符串相互比较和追加的问题
- 向链表追加节点的问题