C++ 从.txt文件中提取数字并将其放入c++;
我需要在一个文件中获取未知数量的整数,并将它们存储到一个向量中,从最大值到最小值排序,然后找到最小值和最大值的调和平均值。然后我需要将平均值和文件中的所有数字输出到一个新文件中。我创建了一个适用于小于10的整数的代码,但它将数字作为字符串输入到向量中,我需要重写代码以将文件的每一行作为整数输入,但出现错误。它不会向“AverageFile”输出任何内容,加载后程序将崩溃。它给出了错误 “在抛出'std::bad_alloc'what():std::bad_alloc'实例后终止调用” 我的代码如下,我认为问题在于向量的输入或输出C++ 从.txt文件中提取数字并将其放入c++;,c++,file,vector,C++,File,Vector,我需要在一个文件中获取未知数量的整数,并将它们存储到一个向量中,从最大值到最小值排序,然后找到最小值和最大值的调和平均值。然后我需要将平均值和文件中的所有数字输出到一个新文件中。我创建了一个适用于小于10的整数的代码,但它将数字作为字符串输入到向量中,我需要重写代码以将文件的每一行作为整数输入,但出现错误。它不会向“AverageFile”输出任何内容,加载后程序将崩溃。它给出了错误 “在抛出'std::bad_alloc'what():std::bad_alloc'实例后终止调用” 我的代码如
#include <iostream>
#include <fstream>
#include <string>
#include<stdlib.h>
#include<vector>
using namespace std;
int main()
{
int line;
vector<int> score;
int i=0;
//double sum=0;
//double avg;
int temp=0;
string x;
cin>>x;
ifstream feederfile(x.c_str());
if(feederfile.is_open())
{
while(feederfile.good())
{
score.push_back(line);
i++;
}
feederfile.close();
}
else cout<<"Unable to open Source file";
//Sorting from greatest to least:
for(int i=0;i<score.size()-1;i++)
{
for(int k=i;k<score.size();k++)
{
if(score[i]<score[k])
{
temp=score[i];
score[i]=score[k];
score[k]=temp;
}
}
}
int a=score[score.size()-1];
int b=score[0];
double abh=2/(1/double (a)+1/double (b));
ofstream myfile ("AverageFile.txt");
if(myfile.is_open())
{
myfile<<"The Harmonic Mean is: "<<abh<<endl;
myfile<<"Sorted Numbers: ";
for(int i=0;i<score.size();i++)
{
if(i<score.size()-1)
{
myfile<<score[i]<<", ";
}
else{myfile<<score[i];}
}
myfile.close();
}
else cout<<"Unable to open Export file";
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
内线;
向量得分;
int i=0;
//双和=0;
//双平均值;
内部温度=0;
字符串x;
cin>>x;
ifstream-federfile(x.c_-str());
if(feederfile.is_open())
{
while(feederfile.good())
{
得分。向后推(线);
i++;
}
federfile.close();
}
否则,您可能会忘记读取文件中的.In
while(feederfile.good()){
score.push_back(line);
i++;
}
您从未从文件中读取数据,因此有一个无限循环,因为文件始终是good()
,最终您在尝试向向量中添加对象时耗尽了内存
#include <iostream>
#include <fstream>
#include <string>
#include<stdlib.h>
#include<vector>
using namespace std;
int main()
{
int line;
vector<int> score;
int i=0;
//double sum=0;
//double avg;
int temp=0;
string x;
cin>>x;
ifstream feederfile(x.c_str());
if(feederfile.is_open())
{
while(feederfile.good())
{
score.push_back(line);
i++;
}
feederfile.close();
}
else cout<<"Unable to open Source file";
//Sorting from greatest to least:
for(int i=0;i<score.size()-1;i++)
{
for(int k=i;k<score.size();k++)
{
if(score[i]<score[k])
{
temp=score[i];
score[i]=score[k];
score[k]=temp;
}
}
}
int a=score[score.size()-1];
int b=score[0];
double abh=2/(1/double (a)+1/double (b));
ofstream myfile ("AverageFile.txt");
if(myfile.is_open())
{
myfile<<"The Harmonic Mean is: "<<abh<<endl;
myfile<<"Sorted Numbers: ";
for(int i=0;i<score.size();i++)
{
if(i<score.size()-1)
{
myfile<<score[i]<<", ";
}
else{myfile<<score[i];}
}
myfile.close();
}
else cout<<"Unable to open Export file";
return 0;
}
使用feederfile.good()
。相反,您希望使读取操作成为循环条件。因此,如果我们这样做,则
while(feederfile >> line){
score.push_back(line);
i++;
}
将一直读取,直到遇到错误或文件结束。您忘记读取文件。在
while(feederfile.good()){
score.push_back(line);
i++;
}
您从未从文件中读取数据,因此有一个无限循环,因为文件始终是good()
,最终您在尝试向向量中添加对象时耗尽了内存
#include <iostream>
#include <fstream>
#include <string>
#include<stdlib.h>
#include<vector>
using namespace std;
int main()
{
int line;
vector<int> score;
int i=0;
//double sum=0;
//double avg;
int temp=0;
string x;
cin>>x;
ifstream feederfile(x.c_str());
if(feederfile.is_open())
{
while(feederfile.good())
{
score.push_back(line);
i++;
}
feederfile.close();
}
else cout<<"Unable to open Source file";
//Sorting from greatest to least:
for(int i=0;i<score.size()-1;i++)
{
for(int k=i;k<score.size();k++)
{
if(score[i]<score[k])
{
temp=score[i];
score[i]=score[k];
score[k]=temp;
}
}
}
int a=score[score.size()-1];
int b=score[0];
double abh=2/(1/double (a)+1/double (b));
ofstream myfile ("AverageFile.txt");
if(myfile.is_open())
{
myfile<<"The Harmonic Mean is: "<<abh<<endl;
myfile<<"Sorted Numbers: ";
for(int i=0;i<score.size();i++)
{
if(i<score.size()-1)
{
myfile<<score[i]<<", ";
}
else{myfile<<score[i];}
}
myfile.close();
}
else cout<<"Unable to open Export file";
return 0;
}
使用feederfile.good()
。相反,您希望使读取操作成为循环条件。因此,如果我们这样做,则
while(feederfile >> line){
score.push_back(line);
i++;
}
它将一直读取,直到遇到错误或文件结束。是否feederfile
应该是ifstream
或of stream
?抱歉,忘记删除'ofstream'?是否应该是ifstreamIsfeederfile
或者of stream
?抱歉,忘记删除'ofstream'应该是ifstream