我必须把一段代码从Python翻译成C++,但我找不到翻译这部分的方法。
with open(files.square, "r") as f: # files.square is a txt file
lines = f.readlines()
with open(files.bs, "a") as f: # files.bs is a txt file
for i, line in enumerate(lines):
if line == "LINE\n": txt_line(i, line, lines)
if line == "ARC\n": txt_arc(i, line, lines)
#The functions txt_line and txt_arc doesn't need to be translated
只有文件的打开(用open...),读取(f.readlines...)和循环(for i, line in enumerate(lines))需要翻译。
基本上,我想翻译所有的代码,但我不知道如何翻译。
试试这样的方法。
void txt_line(int, std::string, std::vector<std::string>){}
void txt_arch(int, std::string, std::vector<std::string>){}
int main()
{
std::ifstream firstFile("../square.txt");
std::vector<std::string> contentOfFirstFile;
std::string aLine;
while(std::getline(firstFile, aLine))
{
contentOfFirstFile.push_back(aLine);
}
std::ifstream secondFile("../bs.txt");
aLine.clear();
int lineCounter = 0;
while(std::getline(secondFile, aLine))
{
if(aLine == "LINE")
{
txt_line(lineCounter, aLine, contentOfFirstFile);
}
else if(aLine == "ARC")
{
txt_arch(lineCounter, aLine, contentOfFirstFile);
}
++lineCounter;
}
std::cin.get();
return 0;
}