Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++; 我写了一个C++程序,读取浮点的.txt文件并将其转换成数组。.txt文件的结构如下所示: 115;-83.570045;-1.082268;26.332237;0.019520;-0.976976;0.123464;2.601000;-1.224000;-0.306000;329.895233;-824.890686;28.381367;27.530001;963.750000 119;-82.757698;-1.187364;26.512978;0.026840;-0.975024;0.124928;1.530000;-3.060000;0.612000;317.688202;-812.683655;26.855488;27.530001;963.780029 121;-83.921730;-2.208382;27.361389;-0.002928;-1.015528;0.103456;4.284000;-20.348999;-7.650000;338.745361;-812.683655;16.479504;27.540001;963.760010 123;-83.580917;-4.218470;29.861431;0.145912;-1.009184;0.108824;0.918000;-0.153000;4.437000;341.797119;-803.833557;4.272464;27.530001;963.789978 ..._C++_Arrays_String_File_Parsing - Fatal编程技术网

在C++; 我写了一个C++程序,读取浮点的.txt文件并将其转换成数组。.txt文件的结构如下所示: 115;-83.570045;-1.082268;26.332237;0.019520;-0.976976;0.123464;2.601000;-1.224000;-0.306000;329.895233;-824.890686;28.381367;27.530001;963.750000 119;-82.757698;-1.187364;26.512978;0.026840;-0.975024;0.124928;1.530000;-3.060000;0.612000;317.688202;-812.683655;26.855488;27.530001;963.780029 121;-83.921730;-2.208382;27.361389;-0.002928;-1.015528;0.103456;4.284000;-20.348999;-7.650000;338.745361;-812.683655;16.479504;27.540001;963.760010 123;-83.580917;-4.218470;29.861431;0.145912;-1.009184;0.108824;0.918000;-0.153000;4.437000;341.797119;-803.833557;4.272464;27.530001;963.789978 ...

在C++; 我写了一个C++程序,读取浮点的.txt文件并将其转换成数组。.txt文件的结构如下所示: 115;-83.570045;-1.082268;26.332237;0.019520;-0.976976;0.123464;2.601000;-1.224000;-0.306000;329.895233;-824.890686;28.381367;27.530001;963.750000 119;-82.757698;-1.187364;26.512978;0.026840;-0.975024;0.124928;1.530000;-3.060000;0.612000;317.688202;-812.683655;26.855488;27.530001;963.780029 121;-83.921730;-2.208382;27.361389;-0.002928;-1.015528;0.103456;4.284000;-20.348999;-7.650000;338.745361;-812.683655;16.479504;27.540001;963.760010 123;-83.580917;-4.218470;29.861431;0.145912;-1.009184;0.108824;0.918000;-0.153000;4.437000;341.797119;-803.833557;4.272464;27.530001;963.789978 ...,c++,arrays,string,file,parsing,C++,Arrays,String,File,Parsing,该文件有2117行,每行由15个数字组成,以“;”分隔。到目前为止,我的计划是: std::ifstream input_file; input_file.open("data.txt"); float sensor_data[2117][15]; std::string line; std::string delimiter = ";"; int count = 0; while (count < 2117 && input_fi

该文件有2117行,每行由15个数字组成,以“;”分隔。到目前为止,我的计划是:

std::ifstream input_file;
input_file.open("data.txt");
float sensor_data[2117][15];
std::string line;
std::string delimiter = ";";
int count = 0;

while (count < 2117 && input_file >> line) {
    size_t pos = 0;
    std::string token;
    int _count = 0;
    while ((pos = line.find(delimiter)) != std::string::npos) {
        token = line.substr(0, pos);
        sensor_data[count][_count] = std::stof(token);
        std::cout << "Read token: " << token << std::endl;
        line.erase(0, pos + delimiter.length());
    }
    
    count += 1;
    std::cout << "New line!" << std::endl << "---------" << std::endl;
}
std::cout << "Sample line: " << std::endl;
for (int i = 0; i < 15; i++) {
    std::cout << sensor_data[0][i] << " ";
}
input_file.close();
我试着用方法来转换这些值,但这给了我完全相同的输出


我做错了什么?有可靠的方法将字符串转换为浮点吗?

您的问题是您从不增加计数


顺便说一下,不要使用以
开头的名称。我真的建议使用。用于读取文件中的行,以及解析输入本身(您可以使用任意字符作为“换行符”)。“是否有可靠的方法将字符串转换为浮点?”确实有:。正如我的想法一样,@Some you need
getline()
std::stringstream ss(line)一起使用问题是关于不是单个字符的定界符,问题中的代码对于单个字符定界符是合适的。使用
getline
比你所申请的答案要简单得多。谢谢你的建议!我最近了解到,使用一个
前缀是完全符合标准的,而
则不是ῥεῖ <代码>.
如果后跟大写字母,则标准禁止使用前缀。我没有说
\u count
是禁止的,但是我给出了关于好的风格的建议…如果后面跟大写字母。如果不是的话,我不明白为什么要避免这样的事情names@Eugene谢谢你指出,这才是真正的问题所在。我现在觉得自己有点傻了
C++
社区里是不受欢迎的。该标准禁止使用大写字母后跟的
(因为它为标准库实现保留了此类符号)<代码> < >小写是正规的,但是许多非标准C++库函数从它开始,可能会有混淆。
// Output #1:
Read token: 5555
Read token: -85.951965
Read token: 51.452652
Read token: 3.685740
Read token: -0.607560
Read token: -0.671488
Read token: -0.019032
Read token: 6.426000
Read token: 9.486000
Read token: -20.042999
Read token: -180.664185
Read token: -210.266251
Read token: 43.334991
Read token: 30.940001
// Output #2:
Sample line: 
27.53 0 0 0 0 0 0 0 0 0 0 0 0 0 0