C++ 如何在不知道大小的情况下读取文本文件并存储到数组中?
我是新来的 如何读取文本文件并将其存储到数组中,而不知道文本文件中的数据数量C++ 如何在不知道大小的情况下读取文本文件并存储到数组中?,c++,arrays,text-files,C++,Arrays,Text Files,我是新来的 如何读取文本文件并将其存储到数组中,而不知道文本文件中的数据数量 #include <iostream> #include <fstream> using namespace std; int main(){ ifstream data; int a[100]; int i=0; data.open("test.txt"); while(data>>a[i]){ cout <<
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream data;
int a[100];
int i=0;
data.open("test.txt");
while(data>>a[i]){
cout << a[i] << endl;
i++;
}
data.close();
}
#包括
#包括
使用名称空间std;
int main(){
ifstream数据;
INTA[100];
int i=0;
data.open(“test.txt”);
而(数据>>a[i]){
cout最简单的方法是只使用std::vector
如果你不愿意这样做,那么在概念上你必须这样做
计算数据数:X
分配足够的内存来存储X
数据项
类似下面的内容应该可以完成这项工作(注意:我没有测试代码)
#包括
#包括
使用名称空间std;
大小数据大小(常量字符串名称)
{
尺寸c=0;
ifstream数据;
数据。打开(名称);
而(数据>>a[i])
C++;
data.close();
返回c;
}
int main(){
string name=“test.txt”
int*a=新int[数据大小(名称)];
int i=0;
ifstream数据;
data.open(“test.txt”);
而(数据>>a[i]){
cout最简单的方法是只使用std::vector
如果你不愿意这样做,那么在概念上你必须这样做
计算数据数:X
分配足够的内存来存储X
数据项
类似下面的内容应该可以完成这项工作(注意:我没有测试代码)
#包括
#包括
使用名称空间std;
大小数据大小(常量字符串名称)
{
尺寸c=0;
ifstream数据;
数据。打开(名称);
而(数据>>a[i])
C++;
data.close();
返回c;
}
int main(){
string name=“test.txt”
int*a=新int[数据大小(名称)];
int i=0;
ifstream数据;
data.open(“test.txt”);
而(数据>>a[i]){
不能使用std::basic_string
:
#include <iostream>
#include <fstream>
#include <string>
int main(){
ifstream data;
std::string s = "";
data.open("test.txt");
while(data>>s);
data.close();
}
#包括
#包括
#包括
int main(){
ifstream数据;
std::string s=“”;
data.open(“test.txt”);
而(数据>>s);
data.close();
}
使用std::basic\u字符串
:
#include <iostream>
#include <fstream>
#include <string>
int main(){
ifstream data;
std::string s = "";
data.open("test.txt");
while(data>>s);
data.close();
}
#包括
#包括
#包括
int main(){
ifstream数据;
std::string s=“”;
data.open(“test.txt”);
而(数据>>s);
data.close();
}
当阵列太小时,您可以将其放大。使用您认为最合适的尺寸,并且只有在提供更多数据时才能放大
#include <iostream>
#include <fstream>
int main() {
std::ifstream data;
int size = 100;
int* a = new int[size];
int i = 0;
data.open("test.txt");
if (data.is_open()) // check if file is open
return -1;
while (data.peek() != EOF) {
if (i >= size) {
int old_size = size;
size *= 2; // double the size, or what ever. just make it bigger
int* new_array = new int[size]; // create new array with new size
std::copy(a, a + old_size, new_array); // copy data to new array
int* tmp1 = a; // assign old array to a tmp pointer, to not lose the memory
a = new_array; // assign the newly allocated memory to the pointer
delete[] a; a = nullptr; // delete the old memory
}
data >> a[i];
std::cout << a[i] << '\n';
i++;
}
data.close();
// after you don't need the data anymore; delete[]
delete[] a; a = nullptr;
return 0;
}
#包括
#包括
int main(){
std::ifstream数据;
int size=100;
int*a=新的int[size];
int i=0;
data.open(“test.txt”);
if(data.is_open())//检查文件是否打开
返回-1;
while(data.peek()!=EOF){
如果(i>=大小){
int old_size=大小;
大小*=2;//是原来的两倍,或者更大一点
int*new_array=new int[size];//使用新大小创建新数组
std::copy(a,a+旧的_大小,新的_数组);//将数据复制到新数组
int*tmp1=a;//将旧数组分配给tmp指针,以免丢失内存
a=new_array;//将新分配的内存分配给指针
delete[]a;a=nullptr;//删除旧内存
}
数据>>a[i];
std::cout当数组太小时,您可以放大它。使用一个您认为最合适的大小,并且只有在提供更多数据时才会放大
#include <iostream>
#include <fstream>
int main() {
std::ifstream data;
int size = 100;
int* a = new int[size];
int i = 0;
data.open("test.txt");
if (data.is_open()) // check if file is open
return -1;
while (data.peek() != EOF) {
if (i >= size) {
int old_size = size;
size *= 2; // double the size, or what ever. just make it bigger
int* new_array = new int[size]; // create new array with new size
std::copy(a, a + old_size, new_array); // copy data to new array
int* tmp1 = a; // assign old array to a tmp pointer, to not lose the memory
a = new_array; // assign the newly allocated memory to the pointer
delete[] a; a = nullptr; // delete the old memory
}
data >> a[i];
std::cout << a[i] << '\n';
i++;
}
data.close();
// after you don't need the data anymore; delete[]
delete[] a; a = nullptr;
return 0;
}
#包括
#包括
int main(){
std::ifstream数据;
int size=100;
int*a=新的int[size];
int i=0;
data.open(“test.txt”);
if(data.is_open())//检查文件是否打开
返回-1;
while(data.peek()!=EOF){
如果(i>=大小){
int old_size=大小;
大小*=2;//是原来的两倍,或者更大一点
int*new_array=new int[size];//使用新大小创建新数组
std::copy(a,a+旧的_大小,新的_数组);//将数据复制到新数组
int*tmp1=a;//将旧数组分配给tmp指针,以免丢失内存
a=new_array;//将新分配的内存分配给指针
delete[]a;a=nullptr;//删除旧内存
}
数据>>a[i];
std::cout最简单的方法是计算文件中的元素。然后分配正确的内存量。然后将数据复制到该内存中
#include <iterator>
#include <algorithm>
int main()
{
std::ifstream data("test.txt");
std::size_t count = std::distance(std::istream_iterator<int>(data),
std::istream_iterator<int>{});
data.fseek(0); // Post C++11 this works as expected in older version you
// will need to reset the flags as well.
int data = new int[count];
std::copy(std::istream_iterator<int>(data), std::istream_iterator<int>{},
data);
}
#包括
#包括
int main()
{
std::ifstream数据(“test.txt”);
std::size\u t count=std::distance(std::istream\u迭代器(数据),
std::istream_迭代器{});
data.fseek(0);//在C++11之后,这在旧版本中可以正常工作
//还需要重置标志。
整数数据=新整数[计数];
std::copy(std::istream_迭代器(数据),std::istream_迭代器{},
数据);
}
最简单的方法是对文件中的元素进行计数。然后分配正确的内存量。然后将数据复制到该内存中
#include <iterator>
#include <algorithm>
int main()
{
std::ifstream data("test.txt");
std::size_t count = std::distance(std::istream_iterator<int>(data),
std::istream_iterator<int>{});
data.fseek(0); // Post C++11 this works as expected in older version you
// will need to reset the flags as well.
int data = new int[count];
std::copy(std::istream_iterator<int>(data), std::istream_iterator<int>{},
data);
}
#包括
#包括
int main()
{
std::ifstream数据(“test.txt”);
std::size\u t count=std::distance(std::istream\u迭代器(数据),
std::istream_迭代器{});
data.fseek(0);//在C++11之后,这在旧版本中可以正常工作
//还需要重置标志。
整数数据=新整数[计数];
std::copy(std::istream_迭代器(数据),std::istream_迭代器{},
数据);
}
如果您不想使用std::vector,您可以使用容器(例如)(我真的不明白),您必须手动执行同样的操作。动态分配内存,并在需要更多空间时扩展内存。使用动态分配数组-new
,依此类推。实际上,这是我分配的一小部分,并且不允许使用,在说明中给出。只要您不预期test.txt文件的大小将超过100元素,while(i<100&&data>>a[i])
如果您不想,可以使用容器(例如)