gpt4 book ai didi

c++ - 获取字符串的最后 9 个字符

转载 作者:行者123 更新时间:2023-11-30 05:23:17 24 4
gpt4 key购买 nike

你好,需要帮助获取字符串的最后 N 个字符:有这样一行:

PutPixel(x + N, y + N, 0, 0, 0); // N = 1 - 1000

并希望获得最后 9 个符号,女巫是“0, 0, 0”;” ,我有 50k 行,每行都有不同的数字,我想找到每行 0,0,0);最后把它从数组中删除,首先我必须找到它们我试过 line[N].substr(0,-9); 但那没有用,我也试过 line[N].find("0, 0, 0); "); 但这只是给我玩一个很长的数字谎言 "4294967295"

完整代码:

const string file = "putpixel.txt";
const int maxlines = 60000;
int main()
{
string lines[maxlines];
fileRead(file,lines);
deleteBlack(lines);
cout << lines[0].find("0, 0, 0);");
return 0;
}

void fileRead(const string fn,string line[]){
int index = 0;
ifstream fin(fn.c_str());
while(!fin.eof()){
getline(fin,line[index],';');
index++;
}
fin.close();
}

最佳答案

enter image description here

此代码从文件中加载文本并搜索

"0, 0, 0" 并使用 "" 替换它

searchresult = stringFile.find("0, 0, 0")

substr( searchresult + search_string.length() , stringFile.length() )

它将一直这样做直到 searchresult = -1。如果是,它将更改的结果写回文件。

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;


int main(){

int searchresult=1;
ifstream inFile;

// load file into string
inFile.open("putpixel.txt");
stringstream strStream;
strStream << inFile.rdbuf();
string stringFile = strStream.str();
inFile.close();

cout<<"\nBefore replace \n";
cout<<""<<stringFile<<" \n";


// initialize search string and replace string
string search_string = " 0, 0, 0";
string replace_string = " ";


// while searchresult is still greater than zero then keep searching
while ( searchresult > 0 ){

searchresult = stringFile.find(search_string);

// if searchresult is greater than zero then keep doing this
if(searchresult >= 0){
string tmpstring = stringFile.substr(0,searchresult);
tmpstring += replace_string;
tmpstring += stringFile.substr(searchresult+search_string.length(), stringFile.length());
stringFile = tmpstring;
}

}
// update file after removing
ofstream outFile("putpixel.txt");
outFile << stringFile;
outFile.close();

cout<<"\nAfter replace \n";
cout<<""<<stringFile<<" \n";

cout<<"\nPress ANY key to close.\n\n";
cin.ignore(); cin.get();
return 0;
}

putpixel.txt 之前的输出:

PutPixel(x + N, y + N, 0, 0, 0);
PutPixel(x + N, y + N, 0, 0, 0);
PutPixel(x + N, y + N, 0, 0, 0);
PutPixel(x + N, y + N, 0, 0, 0);

putpixel.txt 的输出:

PutPixel(x + N, y + N, );
PutPixel(x + N, y + N, );
PutPixel(x + N, y + N, );
PutPixel(x + N, y + N, );

关于c++ - 获取字符串的最后 9 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39272414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com