文本文件到数组的传输
Textfile to array transfer
有人能帮我做一个for循环吗?这个循环可以从文本文件中读取并输出到数组中?
for (int i = 0; i < numCreatures[x]; i++)
{
dataFile = creaturesDT[i];
}
就我的想法而言,这是错误的。
这就是你可以写的:
// Input stream for your file. Passing the file name and it gets open for you
ifstream dataFile("test.txt");
// Array replacement with a proper container
vector<string> stringlist;
// Temporary variable to read the line or word in
string mystring;
// Read continously into the temporary variable until you run out of data
while (getline(dataFile, mystring)) {
// In each iteration, push the value of the temporary variable
// to the end of the container
stringlist.push_back(mystring);
}
// At last, close the file as we do not need it anymore
dataFile.close();
我建议不要使用原始数组,而是使用适当的标准库容器,如vector
或list
和string
。它还取决于您的确切用例,是希望使用operator>>
重载还是getline。前者将读取一个单词,而后者将读取.
相关文章:
- Angelscript从C++传输数组
- 我无法将数据从数据网格列传输到 C# 中的数组以获取文本框建议收集数据
- 为什么我的整数不能传输到数组?
- 如何修复将传输到 java 的 2d 数组代码使用 JNI
- 如何在 C++ 中以特定形式将内容从 1D 数组传输到 2D 数组
- 在不使用For循环的情况下将数据从指向本地数组的指针传输到全局数组
- 将 cv::Mat 存储在字节数组中,以便将数据传输到服务器
- 检查数组中的相似元素并传输到新数组
- C++读取文件传输到2d数组
- 将 int 数组值传输到无符号向量
- 动态分配的数组通过引用传输到函数
- Python C API 将 PyObject * 传输到 c 数组中
- 从比特集读取比特值并传输到字节数组
- 如何在C++中将文本文件传输到数组2D
- 编辑:如何将数组值传输到正数组和负数组
- 正在将指针地址从二维数组传输到结构
- 转换之间的双和字节数组,为传输ZigBee API
- 通过套接字传输大型数组的最快方式
- 使用命名(fifo)管道在python和c++之间传输数组(图像)
- 文本文件到数组的传输