在C++中,可以将文件放入数组变量中,但是需要先读取文件内容,并将内容存储到数组中。下面是一个示例代码:
#include <iostream> #include <fstream> #include <vector> int main() { std::ifstream file("example.txt"); std::string line; std::vector<std::string> lines; if (file.is_open()) { while (std::getline(file, line)) { lines.push_back(line); } file.close(); // 将文件内容存储到数组中 for (const auto& l : lines) { std::cout << l << std::endl; } } else { std::cout << "Unable to open file" << std::endl; } return 0; } 上面的代码示例打开一个名为"example.txt"的文件并从中读取内容,然后将每一行存储到一个vector中。最后将文件内容打印出来。您可以根据需要修改代码来处理文件内容。