我尝试从数据文件中读取值。但是,我需要提取的每列具有不同数量的值。
值列表如下所示:
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;
}
一切都可以通过“输入”正常工作,但不能与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::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;
}
关于c++ - 从文件读取值,每列具有不同数量的值,C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34881328/