Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++ 将文件中的0.0读入双Arr?_C++_Arrays_Double_Ifstream_Zero - Fatal编程技术网

C++ 将文件中的0.0读入双Arr?

C++ 将文件中的0.0读入双Arr?,c++,arrays,double,ifstream,zero,C++,Arrays,Double,Ifstream,Zero,我正试图从一个文件中读取一组0.0,如下所示: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 使用以下代码转换为双arr: double arr[9] = { 0.0,0.0,

我正试图从一个文件中读取一组
0.0
,如下所示:

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
使用以下代码转换为双arr:

double arr[9] = { 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 };
int tilechoice = 0, random;

ifstream inchoice("D:\\Code\\C++\\Tic Tac Toe AI\\Choices.txt");
inchoice >> fixed >> setprecision(1);
for (int i = 0; i < 11839; i++)
{
    if (i == *b)
    {
        for (int j = 0; j < 9; j++)
        {
            inchoice >> arr[j];
        }
        break;
    }
    else
        inchoice.ignore(36);

如果3.0的位置不正确?

我通过将
inchoice.ignore(36)
更改为
inchoice.ignore(38)
我猜这两个额外的空格是必要的?

您的代码可以简化,这样就不需要那种笨拙的
inchoice.ignore

ifstream inchoice("D:\\Code\\C++\\Tic Tac Toe AI\\Choices.txt");
std::string line; 

for (int i = 0; std::getline(inchoice, line); ++i)
{
    if (i == *b)
    {
        std::stringstream ss(line);
        for (int j = 0; j < 9; j++)
        {
            inchoice >> arr[j];
        }
    }
}
ifstream inchoice(“D:\\code\\C++\\Tic Tac Toe AI\\Choices.txt”);
std::字符串行;
for(inti=0;std::getline(inchoice,line);+i)
{
如果(i==*b)
{
std::stringstream ss(线路);
对于(int j=0;j<9;j++)
{
inchoice>>arr[j];
}
}
}

我觉得输入很好。您到底是如何打印输出的?您使用哪种编译器?在最新的g++上按预期工作,我猜您使用的是
cout
输出,但没有
setprecision
。似乎您没有。例如,什么是
b
——如
i==*b
?我们不知道。因为这样的事情,很难回答你的问题。你能再加一个吗?另外,请包括您的打印代码。@acraig5075是的,cout没有设置精确,您是对的,只需进行一点重新构造即可逐行读取(请参见我的答案),
istream::ignore
根本不需要。
ifstream inchoice("D:\\Code\\C++\\Tic Tac Toe AI\\Choices.txt");
std::string line; 

for (int i = 0; std::getline(inchoice, line); ++i)
{
    if (i == *b)
    {
        std::stringstream ss(line);
        for (int j = 0; j < 9; j++)
        {
            inchoice >> arr[j];
        }
    }
}