2016-08-20 3 views
3

Sudoku 용 프로그램을 작성하려고합니다. 스도쿠는 제 입력 파일을 위해 잘 실행됩니다. 하지만 컴파일러에서 파일을 입력하는 몇 가지 변경을하고 싶습니다. 'open'을 호출 할 때 일치하는 멤버 함수가없는 것과 같은 에러를 잡는다. 내 문제는 I/O 파일이라고 생각하기 때문에 이것은 내 프로그램의 일부일뿐입니다. 어떤 도움을 주셔서 감사합니다! 감사합니다!Sudoku에서 파일을 여는 방법은 무엇입니까?

#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; 
} 
+0

당신은'#include '입니까? – Assimilater

+0

@Assimilater : 예, 있습니다. – pansoh

+0

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong 그리고 오류가 무엇입니까? – kfsone

답변

2

귀하의 filename 변수는 char을 입력있다. 이것은 하나의 문자를 저장할 수있는 하나의 정수 값입니다. "character".

fstream 생성자가 char 형식의 파일 이름을 사용하지 않는다고 말하면 컴파일러가 올바른 것입니다.

아마도 char[SOME_BUFFER_SIZE]이거나 이상적으로는 std::string입니다. 당신이 std::string을 사용하면 C++ 03 컴파일러로 이동하는 경우, 당신은 당신이 역사적인 이유를 들어, fstream에 전달할 때 c_str()을 추가해야한다는

참고.

+0

내 오류가 "myfile.open (filename)"에있는 것 같습니다. 나는 그것을 고치는 법을 모른다. 나는 C++ 11을 사용하고있다. – pansoh

+1

@pansoh "fix it"하는 방법은이 답변에서 말하는 것처럼'char []'또는'std :: string'으로 변환합니다. 사용한 문자 유형은 * 한 문자 * 일 수 있습니다. 즉, 파일 이름은 "a"또는 "b"일 수 있지만 "hello.txt"가 아닌 것은 한 문자 이상이기 때문입니다. – Assimilater

+0

@Assimilater : 한 가지 더 질문이 있습니다. 왜 "4x4"를 선택하고 "9x9"입력으로 출력이 나옵니까? "9x9"를 선택하고 "4x4"입력을하면 무한으로 표시됩니다. – pansoh

관련 문제