CFile的Write将一个数组的指定例如a[3]~a[10]连续写入文件中
时间: 2023-12-22 15:06:44 浏览: 38
你可以使用CFile的Write函数将数组中指定范围的元素连续写入文件中。下面是一个示例代码:
```c++
#include <iostream>
#include <fstream>
int main() {
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int startIndex = 3;
int endIndex = 10;
std::ofstream file("output.txt", std::ios::binary);
if (file.is_open()) {
file.write(reinterpret_cast<const char*>(&a[startIndex]), (endIndex - startIndex + 1) * sizeof(int));
file.close();
std::cout << "Array elements written to file." << std::endl;
} else {
std::cout << "Unable to open file." << std::endl;
}
return 0;
}
```
上述代码创建了一个名为`output.txt`的文件,并将数组`a`中从索引3到索引10的元素连续写入文件中。`startIndex`和`endIndex`分别指定了要写入的数组范围。请注意,我们使用了`std::ofstream`来打开文件,并使用`write`函数将数据写入文件。最后,我们关闭文件并输出一个消息来表示操作成功。