我正在尝试将数据从文件读取到程序的列表中,但无法正常工作,data.txt如下所示:
Pizza Donut Macaroni and cheese
我希望这些由新行分隔的项目都是列表中的一员,我已经提供了如何在python中解决这一问题的代码,以及如何尝试使用下面的类在C中解决它。
蟒蛇
foodFile = open("data/food.txt", "r") data = foodFile.read() data_into_list = data.split("\n") print(data_into_list) foodFile.close()
C类
std::ifstream myfile ("test.txt"); if (myfile.is_open()){ for(line; getline(myfile, line);) { std::string line; threatList.push_back(line); } myfile.close();
谢谢你的帮助!