如何在数独中打开文件?

3

我正在尝试编写数独程序。数独程序可以正常运行我的输入文件,但我想做一些改变,通过编译器输入文件。出现了错误,如“没有匹配的成员函数可调用 'open'”。这只是我程序的一部分,因为我认为我的问题在于I/O文件。任何帮助都将不胜感激!谢谢!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

int main()
{
char filename;
ifstream myfile;
//int row,column;
int choice;
cout << "Enter the desired sudoku 4 for (4x4) or 9 for (9x9) : \n";
cin >> choice;

if(choice == 9) {

    for(int row = 0; row < 9; row++) // iterating the loop to assign initial dummy values
    {
        for(int column = 0; column < 9; column++)
        {
            sudoku[row][column] = 0; // assigining zeros
        }
    }
    cout << "Enter the filename:" << endl;
    cin >> filename;
    myfile.open(filename); // opening the file mentioned
    cout << "The values in the file are :" << endl;
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            for(int row = 0; row < 9; row++) // iterating the loope to get the values form the file
            {
                for(int column = 0; column < 9; column++)
                {
                    myfile >> sudoku[row][column]; // assigning the values to the grid
                    cout << sudoku[row][column] << endl; // printing the grid
                }
            }
        }
    }
    myfile.close(); // closing the file
    solvesudoku(0,0);//We start solving the sudoku.
}

else if(choice == 4) {

    for(int row = 0; row < 4; row++) // iterating the loop to assign initial dummy values
    {
        for(int column = 0; column < 4; column++)
        {
            sudoku1[row][column] = 0; // assigining zeros
        }
    }
    cout << "Enter the filename:" << endl;
    cin >> filename;
    myfile.open(filename); // opening the file mentioned
    cout << "The values in the file are :" << endl;
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            for(int row = 0; row < 4; row++) // iterating the loope to get the values form the file
            {
                for(int column = 0; column < 4; column++)
                {
                    myfile >> sudoku1[row][column]; // assigning the values to the grid
                    cout << sudoku1[row][column] << endl; // printing the grid
                }
            }
        }
    }
    myfile.close(); // closing the file
    solsudoku(0,0);//We start solving the sudoku.
}
else {
    cout << "Invalid Choice..!!!";
 }
return 0;
}

你是 #include<fstream> 吗? - Assimilater
@Assimilater:是的,我有。 - pansoh
为什么在循环条件中使用 iostream.eof() 被认为是错误的?这是什么错误? - kfsone
@Peter:不,实际上,C++11是相关的。请不要删除声明OP使用的语言的标签。另一方面,我并不认为 [tag:io] 有用。 - Lightness Races in Orbit
1个回答

2

您的filename变量的类型为char。这是一个可以存储一个“字符”的单个整数值。

编译器在说没有任何一个fstream构造函数接受char类型的文件名时是正确的。

您可能意思是char[SOME_BUFFER_SIZE],或者最好使用std::string

请注意,如果您使用std::string并移至C++03编译器,则出于历史原因,将其传递给fstream时必须附加c_str()


我猜我的错误在于 "myfile.open(filename)";我不确定该如何修复它。我正在使用c++11。 - pansoh
1
@pansoh “修复”它的方式是将其转换为char []std::string,就像这个答案所说的那样。您使用的类型只能是一个字符。换句话说,您的文件名可以是“a”或“b”,但不能是“hello.txt”,因为那不止一个字符。 - Assimilater
@Assimilater:我明白了,但我还有一个问题:为什么我选择“4x4”,输入“9x9”会有输出呢?如果我选择“9x9”,输入“4x4”将会显示无限。 - pansoh
2
@pansoh,以这种方式更改您的问题是不公平的,这样做会影响回答者。单个问题不应该成为堆栈溢出上的一对一辅导课程,而是旨在回答特定问题。 - Assimilater
1
@pansoh 如果您发布一个单独的问题,我会为您的问题提供答案。 - Assimilater
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接