如何将文本文件中的数据组织成多维数组并从中多列? 对不起,我对C++有点陌生,但是我需要把数据从TXT文件组织成数组(或者如果向量更简单),它需要有12列和10000行。我需要能够将这些列相乘,但我无法将数据放入行中。数据由选项卡解析,并且已经是12x10000格式。我怎么才能只用C++?
我已经试着上网了,除了阅读课文,我什么也没带。我还有225行代码,这些都是我尝试过的。从本质上讲,它就是这样的。我有一个解析器,但它除了用制表符分割数据之外什么都不做,不能识别数据如何将文本文件中的数据组织成多维数组并从中多列? 对不起,我对C++有点陌生,但是我需要把数据从TXT文件组织成数组(或者如果向量更简单),它需要有12列和10000行。我需要能够将这些列相乘,但我无法将数据放入行中。数据由选项卡解析,并且已经是12x10000格式。我怎么才能只用C++?,c++,arrays,vector,notepad,C++,Arrays,Vector,Notepad,我已经试着上网了,除了阅读课文,我什么也没带。我还有225行代码,这些都是我尝试过的。从本质上讲,它就是这样的。我有一个解析器,但它除了用制表符分割数据之外什么都不做,不能识别数据 #include <iostream> #include <fstream> #include <string> using namespace std; int main () { float array[12][10000]; // creates array to h
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
浮点数组[12][10000];//创建用于保存名称的数组
long loop=0;//输入循环的缩写
float line;//这将包含从文件读取的数据
ifstream myfile(“data.txt”);//打开文件。
if(myfile.is_open())//如果文件已打开
{
while(!myfile.eof())//未到达文件末尾时
{
getline(myfile,line);//从文件中获取一行
数组[循环]=行;
cout这里有一个简单的解决方案,适用于制表符或空格分隔符
#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;
}
#包括
#包括
#包括
使用名称空间std;
constexpr size\u t rows\u len=10000;
constexpr size\u t cols\u len=12;
int main()
{
浮点数组[rows\u len][cols\u len]{};//值初始化以确保0处未填充的单元格
ifstream myfile(“data.txt”);
如果(!myfile.is_open()){
cout如果您确信输入格式将得到遵守,您可以简单地让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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
浮点数组[12][10000];//创建用于保存名称的数组
long loop=0;//输入循环的缩写
float line;//这将包含从文件读取的数据
ifstream myfile(“data.txt”);//打开文件。
if(myfile.is_open())//如果文件已打开
{
对于(int line=0;line arr[col][line];
如果(!myfile)
{
Cux<代码>((.MyFr.Efof))<代码>多维C++中的数组不太像你在这里尝试使用它们,检查:-用2个维度需要循环两个变量,比如链接中的示例。