我尝试从数据文件中读取值。但是,每列需要提取的值不同。
值列表如下所示:
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;
}
一切正常,&#34;输入&#34;但不是array_x。另外,我想和其他5个变量做同样的事情。我知道这不是最好的解决方案,但我真的不明白为什么它不起作用。
答案 0 :(得分:1)
我不知道并发症是什么,但这是一个例子:
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::getline
和std::istringstream
,这样可以更好地符合行的对齐方式。
编辑1:逐行处理
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;
}