C++ 从指定范围内的文件中读取数组
C++ Read array from file in specified range
我有一个包含如下数组的文件:
5
23
232
44
53
43
所以行包含许多元素。这意味着需要读取的元素数量。并创建一个数组。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream mystream("file.txt");
int ArraySize;
int* array;
ArraySize = ......... //read first line
for(int i = 2; i < ArraySize; i++){
...............//add elements to the array
}
您可以像对待std::cin
一样对待std::ifstream
...
#include <fstream>
#include <iostream>
int main() {
std::ifstream fs("/tmp/file.txt");
int arr_size;
fs >> arr_size; // gets first number in file.
int* arr = new int[arr_size]; // could also use std::vector
// collect next arr_size values in file.
for (int i = 0; i < arr_size; ++i) {
fs >> arr[i];
// std::cout << arr[i] << ' ';
}
delete [] arr;
return 0;
}
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量