gpt4 book ai didi

c++ - 从每列具有不同数量值的文件中读取值,C++

转载 作者:行者123 更新时间:2023-11-28 05:52:44 25 4
gpt4 key购买 nike

我尝试从数据文件中读取值。但是,我需要提取的每列值的数量不同。

值列表如下所示:

8
11
0 0 -50
1000 0 -50
2000 0 0
0 500 0
500 500 0
0 1000 -50
1000 1000 0
2000 1000 150

使用下面的代码,我可以将所有值存储在数组中,但我想单独存储 8 和 11。此外,第一列(0 到 2000)应该存储在一个数组中,第二列(0 到 1000)存储在第二个数组中,第三列(-50 到 150)存储在第三个数组中。

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
cout << "PROGRAM PIPE NETWORK" << endl;

//Read input data file
int n;
double *array;

cout << "How many data sets do you have?\nDatasets: ";
cin >> n;

ifstream infile("pipedata.dat");
array = new double[n];

for (int i = 1; i < n; ++i)
infile >> array[i];

return 0;
}

我希望你们中的一些人能帮助我,请尽量不要写一个笼统的答案。我对这个话题很陌生。问候。

编辑 1:

另一种尝试如下,但它有时会起作用,但我也会遇到数据地址或堆困难的错误。有时它可以工作,但大多数时候我的程序只是停止工作。

int main()
{
cout << "PROGRAM PIPE NETWORK" << endl;

// Read input all data files
int n; double *input;
int limit_x, x; double *array_x;
int limit_y, y; double *array_y;
int limit_q, q; double *array_q;
int n_nodes, n_tubes;

cout << "How many data sets do you have?\nDatasets: ";
cin >> n;

ifstream infile("pipedata.dat");
input = new double[n];
for (int i = 0; i<n; ++i) infile >> input[i];

// Assign input values to their variables
// Number of nodes and number of tubes
n_nodes = input[0];
n_tubes = input[1];

cout << "Input values" << endl;
for (int i = 0; i < n; ++i)
{
cout << input[i] << endl;
}
cout << "---------------------------------------" << endl;
cout << "X-Values" << endl;
// Node data x-values
x = n_nodes;
limit_x = n_nodes * 3;
array_x = new double[x];
for (int i = 0; i < limit_x; i += 3) array_x[i] = 0;
for (int i = 0; i < limit_x; i+=3) array_x[i] = input[i+2];

for (int i = 0; i < limit_x; i+=3) cout << array_x[i] << endl;
return 0;
}

“input”一切正常,但 array_x 不行。我还想对其他 5 个变量做同样的事情。我知道这不是最好的解决方案,但我真的不明白为什么它不起作用。

最佳答案

我不知道并发症是什么,但这里有一个例子:

std::vector<int> array_1;
std::vector<int> left;
std::vector<int> middle;
std::vector<int> right;
int temp;

// Read two numbers into an array
data_file >> temp;
array_1.push_back(temp);
data_file >> temp;
array_1.push_back(temp);

// Read columns of data into separate arrays
int left_value, middle_value, right_value;
while (data_file >> left >> middle >> right)
{
left.push_back(left_value);
middle.push_back(middle_value);
right.push_back(right_value);
}

以上只是众多例子中的一个。它没有优化。

另一个示例使用 std::getlinestd::istringstream,这将更好地符合行的对齐方式。

Edit1:逐行处理

std::string text_line;
std::istringstream parser;

// Read a line from the file
std::getline(data_file, text_line);

// Extract the numbers from the line:
parser.str(text_line); // Initialize the std::istringstream with the text line.
int value_1 = 0;
parser >> value_1; // Extract the first number of the first line.

// Read the second line from the file
std::getline(data_file, text_line);

// Extract the numbers from the line:
parser.str(text_line); // Initialize the std::istringstream with the text line.
int value_2 = 0;
parser >> value_2; // Extract the first number of the second line.

// After reading 2 lines, the file pointer should be pointing
// at the 3rd line.
// The data format changes at the 3rd line with 3 numbers per line.
// Let's use an array this time, one per column.
const unsigned int ARRAY_CAPACITY = 256;
int column_1[ARRAY_CAPACITY];
int column_2[ARRAY_CAPACITY];
int column_3[ARRAY_CAPACITY];
unsigned int row = 0;

// Read until the data stream fails, usually at EOF.
while (std::getline(data_file, text_line))
{

// ** Very important, check for overflow before using array**
if (row >= ARRAY_CAPACITY)
{
// Either reallocate and copy old array or ...
// Crash the program.
std::cerr << "Array capacity is too small.\n";
exit(1);
}

// Initialize the parser
parser.str(text_line);

// Extract the first value and place into array, directly.
parser >> column_1[row];

// Likewise, the next columns.
parser >> column_2[row];
parser >> column_3[row];

// Advance the column index to the next row (line)
++row;
}

关于c++ - 从每列具有不同数量值的文件中读取值,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34881328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com