在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
Read a file line by line in c++ (Translating code from Python to C++)
我必须将代码从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 == "LINEn": txt_line(i, line, lines)
if line == "ARCn": txt_arc(i, line, lines)
#The functions txt_line and txt_arc doesn't need to be translated
只需要翻译文件的打开(带有 open...(、读取(f.readlines...(和循环(对于 i,枚举(行(中的行(。
基本上,我想翻译所有代码,但我不知道如何翻译它。
尝试这样的事情:
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;
}
相关文章:
- 如何将真正的字符串从python c-api转换为python脚本
- 如何在C++中将 Python 字符串转换为其转义版本?
- 使用转换器提升 Python 问题 - 静态链接
- 如何将 Python 双端转换为C++容器?
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 将代码从 Python 转换为 C++ 时出错
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 将数学函数从 Python 转换为 c++
- 从Python转换为C++
- 如何将OpenCV图像数据从Python转换为C++?
- 将代码从 Python 转换为 C++
- 将矩阵乘法从Python转换为C++
- swig C++->Python 2.X 转换和方法的参数
- 从Python转换器用于内置类型
- 将 Python 转换为 C++ - lambda 中的索引
- 将异常处理从 python 转换为 C++
- 如何将Python转换为c++
- 从输入中添加连续整数(从 Python 转换为 C++)
- 将 Python 转换为 C++:列表和元组
- 指针的Boost.Python转换器不起作用