如何使用ifstream

How to use ifstream

本文关键字:ifstream 何使用      更新时间:2023-10-16

我的程序有一个问题,它基本上是一个简单的数学程序。它做了一些向量乘法和向量矩阵乘法,它确实工作得很好,直到我试图从文本中读取数据。我可以编译程序,但当我尝试执行它时,我得到错误:"Dataname.exe不工作"。这是我的代码。不要考虑cins和counts

#include<iostream>
#include<cmath>
#include<vector>
#include"header.hpp"
#include<fstream>
using namespace std;
int main()
{
  ifstream einlesen ("Zahlen.dat"); //function "einlesen" opens the file "Zahlen.dat".
  if (einlesen) // Message if the file has been opend
    cout<<"Daten wurden eingelesen"<<endl;
  else {
    cout<<"Konnte Daten nicht einlesen"<<endl;
    return 99;
  }
  double a;
  int n{0};
  while ( einlesen >>a>>a>>a>>a>>a>>a>>a) n++;//Loop copys datas on a
  einlesen.clear();//stops the loop after the last data is copied.
  einlesen.seekg(0, ios_base::beg);//??
  vector<double> vecein(n), vecein1(n),Matrixein(n);
  for (a = 0;a<n;a++);//copys datas to a vector further calculations
  {
    einlesen>>vecein.at(a);
    einlesen>>vecein1.at(a);
    einlesen>>Matrixein.at(a);
  }
  double Matrix1[9];    //defining my Matrix and the coordinates of my vectors.
  double x,y,z,x_1,x_2,x_3;
  vector<double> vec(3);
  vector<double> vec1(3);
  // Old code where I read the data with `cin`:
  /*cout<<"Geben Sie die x Koordinaten ein:"<<endl;
  cin>>vec[0];
  cin>>vec1[0];
  cout<<"Geben Sie die y Koordinaten ein:"<<endl;
  cin>>vec[1];
  cin>>vec1[1];
  cout<<"Geben Sie die z Koordinaten ein:"<<endl;
  cin>>vec[2];
  cin>>vec1[2];
  cout<<"Geben Sie eine Matrix ein"<<endl;
  cin>>Matrix1[0]>>Matrix1[1]>>Matrix1[2]>>Matrix1[3]>>Matrix1[4]>>Matrix1[5]>>Matrix1[6]>>Matrix1[7]>>Matrix1[8];
  cout<<"Vektor1:<"<<vec[0]<<","<<vec[1]<<","<<vec[2]<<">"<<endl;
  cout<<"Vektor2:<"<<vec1[0]<<","<<vec1[1]<<","<<vec1[2]<<">"<<endl;
  vector<double> Addition(3);
  Addition = Vektoraddition(vec,vec1);
  cout<<"Addition:"<<"<"<<Addition[0]<<","<<Addition[1]<<","<<Addition[2]<<">"<<endl;
  double Skalarprodukt;
  Skalarprodukt = Skalarpr(vec,vec1);
  cout<<"Skalarprodukt:"<<Skalarprodukt<<endl;
  vector<double> kreuzprodukt(3);
  kreuzprodukt = Kreuzprodukt (vec,vec1);
  cout<<"Kreuzprodukt:"<<"<"<<kreuzprodukt[0]<<","<<kreuzprodukt[1]<<","<<kreuzprodukt[2]<<">"<<endl;
  vector<double> MatrixVektor(3);
  MatrixVektor = Matrix_vektor (Matrix1, vec);
  cout<<"Matrix*Vektor:"<<"<"<<MatrixVektor[0]<<","<<MatrixVektor[1]<<","<<MatrixVektor[2]<<">"<<endl;*/
  ofstream ausgabe ("Ausgabe.dat");//write the data on Ausgabe.dat
  for (int i = 0; i < a; i++)
  {
    ausgabe << "(" << vecein[i] << "," << vecein1[i] << "," << Matrixein[i]<<")" << endl;
  }
  return 0;
}

是用德语写的,所以我不担心变量名。我的问题是,我真的不明白我在while循环中做什么。dat是一个类似于1 2 3 4 5 6 7…而Ausgabe.dat为空文件。实际上,它确实写在ausgabe。dat中因为它告诉我内容已经更改但当我重新加载文件时它仍然是空的

我已经试过你的建议,但它仍然不起作用。我没有改变读取数据的方式,因为我想先看看它是否有效。这是我的错误信息:抛出'std::out_of_range'实例后终止调用what(): vector::_M_range_check: __n(等于10)>= this->size(等于10)

这个应用程序请求运行时以一种不寻常的方式终止它。请与应用程序支持团队联系以获取更多信息。

我的代码现在看起来是这样的:

int main()
{

ifstream einlesen ("Zahlen.dat"); 
if (einlesen)

cout<<"Daten wurden eingelesen"<<endl;

else
{
cout<<"Konnte Daten nicht einlesen"<<endl;
return 99;
}

double a;
int n{0};
while ( einlesen >>a>>a>>a>>a>>a>>a>>a) n++;
{
einlesen.clear();
einlesen.seekg(0, ios_base::beg);
vector<double> vecein(n), vecein1(n),Matrixein(n);
int b;// new variable of type int.
for (b = 0;b<n;b++);
{
einlesen>>vecein.at(b);
einlesen>>vecein1.at(b);
einlesen>>Matrixein.at(b);
}
ofstream ausgabe ("Ausgabe.dat");
for (int i = 0; i < b; i++)
{
    ausgabe << "(" << vecein[i] << "," << vecein1[i] << "," << Matrixein[i]       <<")" << endl;
}
}
return 0;

所以我基本上只改变了循环的变量。是的,当我用cin获取数据时,程序确实工作了。我真的不知道为什么程序不工作!

在第一个代码示例中有几件事情应该做得不同。程序首先打开文件,在变量n中计算文件中7个数字块的个数。然后重置文件指针,创建三个大小为n的向量,并从文件中将第一个n数字读入向量。但是:您不必事先知道vector中的条目数,因此不需要读取文件两次。只需使用vector::push_back将数字相加。

第二个问题:在读取数字的循环中,a被用作循环变量,这是一个double。双精度值的递增可能导致一次性错误,从而导致崩溃。

我也不确定逻辑是否正确:似乎您希望文件有几个(n)行数字,每个行7个数字,但您随后读取第一个n数字。假设你有10行,然后你读取第一行和第二行的三个数字到你的向量中,你也可能想要读取所有数字或每行的第一个(甚至是3d结构)。

第二个代码样本呢,你从cin读取的地方?这个有用吗?它可能会有所帮助,如果你解释你想做什么,似乎有在文件中的向量,你想采取的矢量积(kreuzproduckt) ?它们在文件中的布局是怎样的,是行还是列?

编辑:崩溃的原因似乎是这一行:

for (a = 0;a<n;a++);
{
  einlesen>>vecein.at(a);
  ...

注意循环后的;。这导致循环在没有任何语句的情况下首先运行,然后在循环条件不再满足时运行,即a已经等于n