如何在多维数组中组织文本文件中的数据并从中乘以列
How do you organize data from a text file in multidimensional arrays and multiply columns from it?
抱歉,我对 c++ 有点陌生,但我需要将 txt 文件中的数据组织成数组(或向量,如果这样更容易(,它需要有 12 列和 10000 行。我需要能够将这些列相乘,但我无法将数据放入行中。数据按选项卡解析,并且已采用 12x10000 格式。如何仅使用 c++ 执行此操作?
我已经尝试过上网查找,除了阅读文本之外,我什么都没有。我还有 225 行代码,这是我尝试这样做的所有尝试。它基本上归结为这些线。我有一个解析器,但它除了按选项卡划分数据外什么都不做,而不是识别它。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
float array[12][10000]; // creates array to hold names
long loop=0; //short for loop for input
float line; //this will contain the data read from the file
ifstream myfile ("data.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
array[loop] = line;
cout << array[loop] << endl; //and output it
loop++;
}
myfile.close(); //closing the file
}
else cout << "Unable to open file"; //if the file is not open output
system("PAUSE");
return 0;
}
我希望结果是组织成数组或向量的数据(我不知道如何使用向量(,我可以在其中乘以列,但它只是伴随着我无法正确将代码放入列中的错误。
这里有一个简单的解决方案,适用于制表符或空格的分隔符。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
constexpr size_t rows_len = 10000;
constexpr size_t cols_len = 12;
int main ()
{
float array[rows_len][cols_len]{}; // value initialization to ensure unfilled cells at 0
ifstream myfile("data.txt");
if (!myfile.is_open()) {
cout << "Unable to open file" << endl;
return 1;
}
string line;
for (size_t row = 0; row < rows_len && myfile; row++) {
getline(myfile, line);
const char* s = line.c_str();
for (size_t col = 0; col < cols_len; col++) {
char* p = nullptr;
array[row][col] = strtof(s, &p);
s = p;
}
}
// use array ...
return 0;
}
strtof()
的第二个参数允许知道下一个单元格的开头在哪里。如果单元格不是数字,则所有剩余的array
行都设置为 0。
如果您确定将遵循输入格式,您可以简单地让 iostream 模块解码浮点值:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
float array[12][10000]; // creates array to hold names
long loop=0; //short for loop for input
float line; //this will contain the data read from the file
ifstream myfile ("data.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
for(int line=0; line <1000; line++) //read 1000 lines
{
for (int col=0; col<12; col++) // 12 values per line
{
myfile >> arr[col][line];
if (!myfile)
{
cout << "Read error line " << line << " col " << col << "n";
myfile.close();
return 1;
}
}
}
myfile.close(); //closing the file
}
else cout << "Unable to open file"; //if the file is not open output
system("PAUSE");
return 0;
}
请注意,此代码不使用皱眉的 while (! myfile.eof(( (,而是在读取后立即进行测试
相关文章:
- C++数据文件、数组和计算赋值
- 数据文件的第二行未正确读取
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- 将数据文件读入结构 C++
- 引入流时C++数据文件未正确读取?
- 如何在数据文件中查找和显示内容
- 如何处理错误"E1696命令行错误:无法在Visual Studio 2017中打开元数据文件"mscorlib.dll"?
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 将数据文件读入对象数组时出现问题
- C :数据文件有错误:预期的无限制ID
- C 和 C++:带有错误"Expected unqualified-id"的数据文件
- 如何在C 中读取一系列数据文件
- 从数据文件中读取每2个字节,并在向量中进行比较
- 写入单个大数据文件或多个小文件:哪个更快?
- 在读取数据文件时,如何找到用户字符串输入的字谜?
- 保护外部数据文件免受未经授权的修改
- C 读取数据文件
- C 如何将数据文件读取到结构或向量以及返回结构或向量
- 具有挑战性的数据文件格式,需要将其读取为包含类对象的数组的VAR
- C 中的Google单元测试:如何编写持久数据文件