将文件中的数据存储到数组中
storing data from a file into an array
我编写了以下代码来读取列中的数据从文件(area.inp(到数组。 在屏幕中显示数据时,第一个"for"循环显示正确的数字(因此代码正确读取文件中的数字(,但第二个"for"循环显示不正确的数字集。我无法解决这个问题。 我将不胜感激有关该问题的任何指导。
area.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
如注释中所述,您正在创建一个 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;
}
数组
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;
}
相关文章:
- 多维数组存储三种不同的数据类型?
- C++将原始字符数组存储在元组中
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 如何将JSON数组存储在Arduino程序中
- 如何在AVR(例如:ATMega)或Arduino微控制器上制作2D PROGMEM阵列的1D PROGMEM数组(存储
- 使用 char 数组存储数据
- 使用平面数组存储的元组容器
- 数组 存储并要求用户搜索它
- 将值从一个 char 数组存储到另一个 char 数组
- 如何使字符串数组存储在向量中
- 将构造的静态数组存储在多个文件中的内存中
- 将整数数组存储在堆上并通过指针访问它们
- c++声明一个数据类型的字节数组,该数组存储在字符串中
- 使用指针数组存储阶乘计算的所有中间结果
- 使用异构数组存储不同的子类
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- 尝试将复杂数组存储到类 c++ 中
- 在字节数组存储中嵌入 int/string