2016-09-12 5 views
0

별도의 함수에서 연 파일에 대한 닫기 함수를 사용하여 파일을 닫는 방법을 알아내는 데 문제가 있습니다. 바로 지금 내 프로그램은 주로 create, open, close의 3 가지 기능으로 구성됩니다. 만들기 및 열기가 잘 작동하지만 파일을 열고 옵션 메뉴로 돌아 가면 사용자 입력없이 파일을 닫을 수 있습니다. close 함수가 어떤 파일이 열려 있는지 감지하고 닫으려고합니다. 내 오픈시 한 번에 열 수있는 텍스트 파일이 하나만 있어야합니다 (코드를 작성할 수는 없지만 닫기 기능과 동일한 것으로 가정합니다). 아래에 모든 코드가 있습니다. 아직 구현해야하는 다른 함수가 있지만 처음 세 개만 걱정됩니다. 어떤 도움을 주셔서 감사합니다!여러 함수 사이에 파일이 있습니다. C++

#include <iostream> 
#include <fstream> 
#include <stdio.h> 

using namespace std; 

void createDB() { 
    ofstream db; 
    string fileName; 

    cout << "Enter the name of the database you want to create: \n"; 
    getline (cin, fileName); 

    string fullFile = fileName + ".txt"; 

    std::ifstream fin(fullFile); 

    if(fin.good()){ // means filename already exists 
     cout << "\nCould not create database because database name " << fullFile << " is already taken\n"; 
    } 
    else{ // creates file 
     cout << "\nYour database " << fullFile << " was created successfully\n"; 
     db.open(fullFile); 
    } 

    db.close(); 
} 

void openDB() { 
    // need to add check to see if one is already open 
    string fileName; 

    cout << "Enter the name of the database you want to open: \n"; 
    getline (cin, fileName); 

    string fullFile = fileName + ".txt"; 

    std::ifstream db(fullFile); 

    if(db.good()){ // means file exists 
     cout << "\nThe database " << fullFile << " has been opened successfully\n"; 
     db.open(fullFile); 
    } 

    else{ // there is no file named that to open 
     cout << "\nThere is no database named " << fullFile << " to open\n"; 
    } 
} 


void closeDB() { 

    cout << "The database _______ has been closed successfully"; 
} 

void display() { 
    cout << "Enter the ID of the employee you want to display: \n"; 
} 

void update() { 

} 

void report() { 

} 

void add() { 

} 

void del() { 

} 

int menu() { 
    cout << "Enter the number of the operation you wish to perform (1-9)\n" 
    << "1. Create new database\n" 
    << "2. Open database\n" 
    << "3. Close database\n" 
    << "4. Display record\n" 
    << "5. Update record\n" 
    << "6. Create report\n" 
    << "7. Add a record\n" 
    << "8. Delete a record\n" 
    << "9. Quit\n"; 

    int sel = 0; 
    (std::cin >> sel).ignore(); 

    switch (sel) { 
     case 1: createDB(); 
      menu(); // after creating file go back to list of options 
      break; 

     case 2: openDB(); 
      menu(); 
      break; 

     case 3: closeDB(); 
      menu(); 
      break; 

     case 4: display(); 
      break; 

     case 5: update(); 
      break; 

     case 6: report(); 
      break; 

     case 7: add(); 
      break; 

     case 8: del(); 
      break; 

     case 9: return 0; 
      break; 

     default: cout << "Please try again and enter a valid number\n\n"; 
      menu(); 
      break; 
    } 
    return true; // to avoid error saying control may reach end of non-void function 
} 


int main() { 

    menu(); 

return 0; 
} 
+0

OS에만 구애받지 않고 쉬운 방법으로 원하는 것을 할 수는 없으며 열어 본 파일을 추적하는 것이 더 낫습니다. 이제는 파일 범위가있는 변수로 충분할 것이라고 생각합니다. –

+0

@ KenY-N은 문자열이고 openDB()의 파일 이름이 무엇이든간에 전역 변수를 갖는 것을 좋아합니까? 그 전역 변수를 닫고 그 파일을 닫는가? 매개 변수를 통해 그렇게 할 수 있습니까? – softballgirl12

+0

전역 변수를 사용하는 대신 매개 변수를 사용하여 수행 할 수도 있습니다. 전역은 더 간단하지만 매개 변수는보다 유연합니다. 개인적으로 말하면, 나는 데이터베이스 핸들 (파일 핸들)을 이와 같은 간단한 프로그램을위한 전역으로 만들 것이다. –

답변

0

열린 파일에서 파일 설명자를 받아야합니다. stile : FILE * myfile; myfile = fopen (myfile, "r"); 당신은 계속 읽을 수 있습니다 : http://www.cprogramming.com/tutorial/cfileio.html (저는 C 언어로 이해하기 쉽고 C++에서도 작동한다고 생각합니다). C++ stile http://www.cprogramming.com/tutorial/lesson10.html 그는 아주 잘 설명합니다.

기억해야 할 점 : 1) 포인터를 만들어야합니다 (데이터를 잃어 버리지 않는 유일한 방법). 2) 설명자를 보내거나 받아야합니다.

관련 문제