从字符数组C++中截断和删除字符
Truncating and removing characters from char array C++
我基本上有一个看起来像这样的txt
文件...
High Score: 50
Player Name: Sam
Number Of Kills: 5
Map
Time
我想将:
之前的所有内容或Time
Map
之后的空格存储到一个数组中,并将之后的所有内容存储在另一个数组中。对于Map
和Time
,之后什么都没有,所以我想将空格存储为 null
。
到目前为止,我已经设法将所有这些信息读取并存储到一个temp
数组中。但是,我遇到了麻烦。这是我的代码:
istream operator >> (istream &is, Player &player)
{
char **temp;
char **tempNew;
char lineInfo[200]
temp = new char*[5];
tempNew = new char*[5];
for (int i=0; i<5; i++)
{
temp[i] = new char[200];
is.getline(lineInfo, sizeof(lineInfo));
int length = strlen(lineInfo);
for (int z=0; z < length; z++)
{
if(lineInfo[z] == '= ' ){ //HOW DO I CHECK IF THERE IS NOTHING AFTER THE LAST CHAR
lineInfo [length - (z+1)] = lineInfo [length];
cout << lineInfo << endl;
strncpy(temp[i], lineInfo, sizeof(lineInfo));
}
else{
tempNew[i] = new char[200];
strncpy(tempNew[i], lineInfo, sizeof(lineInfo));
}
}
}
如果你需要的是找到':"
#include <cstring>
只是 auto occurance = strstr(string, substring);
文档在这里。
如果出现不是空 PTR,则查看出现是否位于 Get 行的行尾。如果没有,您的值就是之后的所有内容:
使用 std::string
要容易得多。
// Read high score
int high_score;
my_text_file.ignore(10000, ':');
cin >> high_score;
// Read player name
std::string player_name;
my_text_file.ignore(10000, ':');
std::getline(my_text_file, player_name);
// Remove spaces at beginning of string
std::string::size_type end_position;
end_position = player_name.find_first_not_of(" t");
if (end_position != std::string::npos)
{
player_name.erase(0, end_position - 1);
}
// Read kills
unsigned int number_of_kills = 0;
my_text_file.ignore(':');
cin >> number_of_kills;
// Read "Map" line
my_text_file.ignore(10000, 'n');
std::string map_line_text;
std::getline(my_text_file, map_line_text);
// Read "Text" line
std::string text_line;
std::getline(my_text_file, text_line);
如果你坚持使用C风格的字符串(char
数组),你将不得不使用更复杂和不太安全的功能。 查找以下函数:
fscanf, strchr, strcpy, sscanf
相关文章:
- 在C++的字符串中搜索和删除某些字符
- 删除/替换C++字符串中的多字符 (ÿû)
- 如何删除字符串中的前 2 个字符(如果它有"-")
- 如何在 getline 中删除/忽略超过特定数字的字符?
- 如何在 c++ 中删除字符串中的某些字符?
- 从双引号括起来的子字符串中删除特定字符
- 如何从字符数组(不是字符串数组)中删除符号
- C++ 从 std::string 中删除所有非数字字符
- xcode 从源代码中删除奇怪的控制字符
- C++从句子中删除给定字符的程序不起作用
- 正在从C++中的字符数组中删除字符
- 如何检查字符串中的最后一个字符是否是某个字符并将其从字符串中删除?(C++)
- C++从字符末尾删除值
- 从字符数组中删除双空格
- 循环以从字符数组中删除过多的空格 (C++)
- 如何删除新分配的字符,这也是函数返回值?
- 删除字符串向量中的字符重复
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 删除以前打印的字符
- 回文查找器:非字母数字字符删除问题