如何使用ifstream
How to use ifstream
我的程序有一个问题,它基本上是一个简单的数学程序。它做了一些向量乘法和向量矩阵乘法,它确实工作得很好,直到我试图从文本中读取数据。我可以编译程序,但当我尝试执行它时,我得到错误:"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
。
- 如何使用 ifstream 检查文件中变量的类型?
- 尝试使用 Ifstream 打开.txt文件时C++问题
- 使用多个ifstream作为ifstream的向量
- Visual Studio 2017中使用ifstream时,Release和Debug的输出不同
- 使用ifstream时,被调用文件中的数据不会打印
- 我如何使用ifstream使用以下类似fread()的语句?
- C++ : 如何在使用 getline() 和 ifstream 对象从文件中读取一行时跳过第一个空格?
- 使用 c++ ifstream 读取文本文件问题
- 是什么让使用 ifstream 时检测到堆栈粉碎?
- 尝试使用 ifstream 读取数据后显示不正确的数字
- 如何使用 istream_iterator 从 ifstream 中读取带有空格的字符串?
- 关于使用ifstream在C 中确定文件是否存在的问题
- std::ifstream,使用已删除功能
- ifstream不使用C 中的数组检索数据
- std::getline 用于 ifstream,使用参数字符串或 char *
- C++错误 "'"令牌之前的预期主表达式(在 ifstream 上使用 getline?
- 将 GetLine 与 ifstream 一起使用 - 没有与参数列表匹配的"getline"实例
- Ifstream如何使用c++从特定行开始读行
- 如何使用C++的ifstream来使用数据长度而不是新行从文件中读取
- 如何从argv中获取文件名并将其与ifstream一起使用/将其更改为ofstream的txt