【发布时间】:2021-02-21 23:58:13
【问题描述】:
我无法理解我在网上找到的这段代码的某些部分,它的目标是从 .txt 文件打印 ASCII 艺术。更准确地说,我无法理解第 28 行中的 while 循环,该循环是字符串函数“getFileContents”的一部分。 'TempLine' 是什么意思?
#include <iostream>
#include <fstream>
#include <string>
std::string getFileContents (std::ifstream&); //Gets file contents
int main(int argc, char *argv[])
{
std::ifstream Reader ("File1.txt"); //Open file
std::string Art = getFileContents (Reader); //Get file
std::cout << Art << std::endl; //Print it to the screen
Reader.close (); //Close file
return 0;
}
std::string getFileContents (std::ifstream& File)
{
std::string Lines = ""; //All lines
if (File) //Check if everything is good
{
while (File.good ())
{
std::string TempLine; //Temp line
std::getline (File , TempLine); //Get temp line
TempLine += "\n"; //Add newline character
Lines += TempLine; //Add newline
}
return Lines;
}
else //Return error
{
return "ERROR File does not exist.";
}
}
【问题讨论】:
-
意思好像是cmets解释的。 具体而言,您不清楚什么?附言就像很多“在网上找到”的代码一样,它有缺陷并且有几个逻辑缺陷。
标签: c++ ascii-art interpretation