将文件中的数据存储到数组中
Posted
技术标签:
【中文标题】将文件中的数据存储到数组中【英文标题】:storing data from a file into an array 【发布时间】:2017-04-07 12:41:19 【问题描述】:我编写了以下代码以将列中的数据从文件 (area.inp) 读取到数组。在屏幕上显示数据时,第一个“for”循环显示正确的数字(因此代码正确地从文件中读取数字),但第二个“for”循环显示不正确的数字集。我无法解决这个问题。如有任何关于该问题的指导,我将不胜感激。
面积.inp
001.000 003.000
002.000 004.000
006.000 005.000
004.000 002.000
002.000 001.000
代码
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char **argv)
int j=0;
double X[j];
double Y[j];
static double *p;
double *q;
p=X;
q=Y;
//**** Counting the number of lines
ifstream myfile("area.inp");
myfile.unsetf(ios_base::skipws);
int points = count(
istream_iterator<char>(myfile),
istream_iterator<char>(),
'\n');
cout << "Number of data in file: " << points << "\n";
//**** Open data file and result
cout <<"file is open"<< endl;
ifstream infile;
infile.open("area.inp", ios::in);
cout <<"Reading data from file"<< endl;
for (j=0; j<points; j++)
cout << std::setprecision(3) << std::fixed;;
infile >> X[j] >> Y[j];
cout << "Value of X["<<j<<"]: " << X[j] << endl;
cout << "Value of Y["<<j<<"]: " << Y[j] << endl;
cout <<"Showing numbers stored in array"<< endl;
for (j=0; j<points; j++)
cout << "Value of X["<<j<<"]: " << X[j] << endl;
cout << "Value of Y["<<j<<"]: " << Y[j] << endl;
infile.close();
return 0;
Number of data in file: 5
file is open
Reading data from file
Value of X[0]: 1.000
Value of Y[0]: 3.000
Value of X[1]: 2.000
Value of Y[1]: 4.000
Value of X[2]: 6.000
Value of Y[2]: 5.000
Value of X[3]: 4.000
Value of Y[3]: 2.000
Value of X[4]: 2.000
Value of Y[4]: 2.000
Showing numbers stored in array
Value of X[0]: 5.000
Value of Y[0]: 3.000
Value of X[1]: 2.000
Value of Y[1]: 4.000
Value of X[2]: 4.000
Value of Y[2]: 5.000
Value of X[3]: 4.000
Value of Y[3]: 2.000
Value of X[4]: 2.000
Value of Y[4]: 2.000
------------------
(program exited with code: 0)
Press return to continue
【问题讨论】:
看看int j=0; double X[j];
。 X
可以存储多少元素?
除了 double X[j];
不是标准 C++ 而 j
不是常量表达式。
【参考方案1】:
如 cmets 中所述,您正在创建一个长度为 0 的数组。要按照你的方式做事,你必须用new
分配数组在你已经计算了文件中的点数:
double *X = new double[points];
double *Y = new double[points];
然后在程序结束时,使用delete
释放内存。
我建议您避免使用数组,而是使用std::vector
。在示例中,我使用std::pair
来存储积分,但您可以更改它。
// Using a vector of pairs
std::vector<std::pair<double, double>> data;
ifstream infile;
infile.open("area.inp");
// You should check here to make sure the file opened ok
// Read the file. No need to know size ahead of time
std::pair<double, double> tmp;
while (infile >> tmp.first >> tmp.second)
data.push_back(tmp);
// Print the results
std::cout << "Number of pairs: " << data.size() << std::endl;
for (auto p : data)
std::cout << p.first << ", " << p.second << std::endl;
【讨论】:
【参考方案2】:您的数组 X[] 和 Y[] 的大小为零。 好奇为什么没有段错误
尝试如下改变,
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char **argv)
int j=0;
static double *p;
double *q;
//**** Counting the number of lines
ifstream myfile("area.inp");
myfile.unsetf(ios_base::skipws);
const int points = count(
istream_iterator<char>(myfile),
istream_iterator<char>(),
'\n');
double X[points];
double Y[points];
p=X;
q=Y;
cout << "Number of data in file: " << points << "\n";
//**** Open data file and result
cout <<"file is open"<< endl;
ifstream infile;
infile.open("area.inp", ios::in);
cout <<"Reading data from file"<< endl;
for (j=0; j<points; j++)
cout << std::setprecision(3) << std::fixed;;
infile >> X[j] >> Y[j];
cout << "Value of X["<<j<<"]: " << X[j] << endl;
cout << "Value of Y["<<j<<"]: " << Y[j] << endl;
cout <<"Showing numbers stored in array"<< endl;
for (j=0; j<points; j++)
cout << "Value of X["<<j<<"]: " << X[j] << endl;
cout << "Value of Y["<<j<<"]: " << Y[j] << endl;
infile.close();
return 0;
【讨论】:
以上是关于将文件中的数据存储到数组中的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据库中的数据提取到 javascript 文件中并将其存储在数组中?