如何将函数的返回结果添加到也在用 C++ 编写的文件中?
How to add the returned result of a function to a file being written too in C++?
你好,我目前正在编写一个程序来读取文本文件中的数据,执行计算,然后将结果输出到输出文本文件
这是完整的程序
主文件
int main() {
readFile(inputFile1, inputFile2, inputFile3, lines);
robotComplexity(lines);
robotVariability(lines);
writeFile(outputFile, lines, robotComplexity(lines), robotVariability(lines));
getch();
return 0;
}
void Customer() {
std::string customerName;
std::string projectName;
std::string partNumber;;
}
void Parts() {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
}
void Builder() {
std:string name;
int ability;
int variability;
}
实施文件
std::vector<std::string> lines;
std::string inputFile1 = "Builders.txt";
std::string inputFile2 = "Parts.txt";
std::string inputFile3 = "Customers.txt";
std::string outputFile = "output.txt";
std::string input;
void readFile(std::string const& inputFile1, std::string const& inputFile2, std::string const& inputFile3,
std::vector<std::string>& lines) //function to read Builders, Customers and Parts text file
{
std::ifstream file1(inputFile1);
std::ifstream file2(inputFile2);
std::ifstream file3(inputFile3);
std::string line;
while(std::getline(file1, line))
{
lines.push_back(line);
}
while(std::getline(file2, line))
{
lines.push_back(line);
}
while(std::getline(file3, line))
{
lines.push_back(line);
}
}
double robotComplexity(std::vector<std::string>& lines) { //takes in the out.txt file
std::string output = lines[0]; //so far only reads the first line of the output file.
std::string input = output.substr(18,1);
double complexity = 20 * atoi(input.c_str());
if(complexity > 100) {
complexity = 100;
}
cout << "The Robot Complexity is: " << complexity << endl;
return complexity;
}
double robotVariability(std::vector<std::string>& lines) {
std::string output = lines[0]; //so far only reads the first line of the output file.
std::string input = output.substr(15,2);
double variability;
variability = 5 + atoi(input.c_str());
cout << "The Robot Variability is: " << variability << endl;
return variability;
}
void writeFile(std::string const& outputFile,
std::vector<std::string> const& lines, double unitComplexity, double unitVariability) //writes to a file output.txt the end calculations.
{
std::ofstream file(outputFile);
for(std::string const& line: lines)
{
file << line << std::endl;
}
for(double x: unitComplexity) {
file << x << std::endl;
}
}
我也想提请注意的代码是实现文件中的最后3个函数,分别是robotCompelxity、robotVariability和writeFile。前 2 个函数 pefrom 计算基于读取的文件和 writeFile,到这个时间点,包含主文件中看到的每个机器人的详细信息(IE、构建器、客户和零件)这个工作和详细信息输出成功到文件。
我的问题在于 robotVariability 和 robotComplexity 函数。正如在 writeFile 函数参数中看到的,in 接收一个作为输出文件的字符串 const,一个作为机器人详细信息的向量列表,以及两个双精度数,作为 robotVariability 和 robotComplexity 函数的结果。我试图创建一个增强的 for 循环以将此类结果添加到输出文件中,如最后这行代码所示
for(double x: unitComplexity) {
file << x << std::endl;
}
但是,这会导致读取 "this range-based 'for' statement requires a suitable "begin" function and none was found" 表示的错误。我不确定如何解决这个问题。
重申一下,我只是尝试将 robotComplexity 和 robotVariability 都是双精度类型的结果添加到 writeFile 函数中正在写入的文件中。谢谢你。
更改您的代码以输出没有像这样的范围 for 循环的双打
void writeFile(std::string const& outputFile,
std::vector<std::string> const& lines, double unitComplexity, double unitVariability) //writes to a file output.txt the end calculations.
{
std::ofstream file(outputFile);
for(std::string const& line: lines)
{
file << line << std::endl;
}
file << unitComplexity << std::endl;
file << unitVariability << std::endl;
}
并且该函数会将双打写入文本文件。范围 for 循环仅适用于向量和数组等对象,而不适用于单值变量。
你好,我目前正在编写一个程序来读取文本文件中的数据,执行计算,然后将结果输出到输出文本文件
这是完整的程序
主文件
int main() {
readFile(inputFile1, inputFile2, inputFile3, lines);
robotComplexity(lines);
robotVariability(lines);
writeFile(outputFile, lines, robotComplexity(lines), robotVariability(lines));
getch();
return 0;
}
void Customer() {
std::string customerName;
std::string projectName;
std::string partNumber;;
}
void Parts() {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
}
void Builder() {
std:string name;
int ability;
int variability;
}
实施文件
std::vector<std::string> lines;
std::string inputFile1 = "Builders.txt";
std::string inputFile2 = "Parts.txt";
std::string inputFile3 = "Customers.txt";
std::string outputFile = "output.txt";
std::string input;
void readFile(std::string const& inputFile1, std::string const& inputFile2, std::string const& inputFile3,
std::vector<std::string>& lines) //function to read Builders, Customers and Parts text file
{
std::ifstream file1(inputFile1);
std::ifstream file2(inputFile2);
std::ifstream file3(inputFile3);
std::string line;
while(std::getline(file1, line))
{
lines.push_back(line);
}
while(std::getline(file2, line))
{
lines.push_back(line);
}
while(std::getline(file3, line))
{
lines.push_back(line);
}
}
double robotComplexity(std::vector<std::string>& lines) { //takes in the out.txt file
std::string output = lines[0]; //so far only reads the first line of the output file.
std::string input = output.substr(18,1);
double complexity = 20 * atoi(input.c_str());
if(complexity > 100) {
complexity = 100;
}
cout << "The Robot Complexity is: " << complexity << endl;
return complexity;
}
double robotVariability(std::vector<std::string>& lines) {
std::string output = lines[0]; //so far only reads the first line of the output file.
std::string input = output.substr(15,2);
double variability;
variability = 5 + atoi(input.c_str());
cout << "The Robot Variability is: " << variability << endl;
return variability;
}
void writeFile(std::string const& outputFile,
std::vector<std::string> const& lines, double unitComplexity, double unitVariability) //writes to a file output.txt the end calculations.
{
std::ofstream file(outputFile);
for(std::string const& line: lines)
{
file << line << std::endl;
}
for(double x: unitComplexity) {
file << x << std::endl;
}
}
我也想提请注意的代码是实现文件中的最后3个函数,分别是robotCompelxity、robotVariability和writeFile。前 2 个函数 pefrom 计算基于读取的文件和 writeFile,到这个时间点,包含主文件中看到的每个机器人的详细信息(IE、构建器、客户和零件)这个工作和详细信息输出成功到文件。
我的问题在于 robotVariability 和 robotComplexity 函数。正如在 writeFile 函数参数中看到的,in 接收一个作为输出文件的字符串 const,一个作为机器人详细信息的向量列表,以及两个双精度数,作为 robotVariability 和 robotComplexity 函数的结果。我试图创建一个增强的 for 循环以将此类结果添加到输出文件中,如最后这行代码所示
for(double x: unitComplexity) {
file << x << std::endl;
}
但是,这会导致读取 "this range-based 'for' statement requires a suitable "begin" function and none was found" 表示的错误。我不确定如何解决这个问题。
重申一下,我只是尝试将 robotComplexity 和 robotVariability 都是双精度类型的结果添加到 writeFile 函数中正在写入的文件中。谢谢你。
更改您的代码以输出没有像这样的范围 for 循环的双打
void writeFile(std::string const& outputFile,
std::vector<std::string> const& lines, double unitComplexity, double unitVariability) //writes to a file output.txt the end calculations.
{
std::ofstream file(outputFile);
for(std::string const& line: lines)
{
file << line << std::endl;
}
file << unitComplexity << std::endl;
file << unitVariability << std::endl;
}
并且该函数会将双打写入文本文件。范围 for 循环仅适用于向量和数组等对象,而不适用于单值变量。