如何在数独中打开文件

How to open a file in Sudoku?

本文关键字:文件      更新时间:2023-10-16

我正在尝试为数独编写一个程序。数独对于我的输入文件运行良好。但是我想进行一些更改,在编译器中输入文件。它捕获错误,例如没有匹配的成员函数来调用"打开"。这只是我程序的一部分,因为我认为我的问题是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;
}

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

编译器说没有fstream构造函数采用类型 char 的文件名时,编译器是正确的。

你可能的意思是char[SOME_BUFFER_SIZE],或者理想情况下std::string

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