2016-09-12 10 views
0

別の関数で開いたファイルのクローズ関数を使用してファイルをクローズする方法がわかりません。今私のプログラムは主に3つの関数、create、open、closeで構成されています。作成とオープンは正常に動作していますが、ファイルを開いてオプションのメニューに戻ると、ユーザー入力がなくてもファイルを閉じることができるようになっています。私はclose関数がどのファイルが開いているのかを検出し、それを閉じるようにします。開いているときに一度に開くことができるテキストファイルは1つだけにする必要があります(これはコード化できませんでしたが、クローズ機能と同じものと想定しています)。以下は私のコードですが、まだ実装する必要がある他の機能がありますが、私は最初の3つについて今心配しています。助けてくれてありがとう!複数の関数間のファイルの入出力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に固有のやり方を簡単に行う方法はありません。たとえあったとしても、開いていたファイルを追跡するほうがはるかに優れています。ファイルスコープを持つ変数であれば十分ですが、私はお勧めします。文字列で、openDBからそのどんなファイル名に設定されたグローバル変数を持つようKenY-N @ –

+0

は()ですか?そのグローバル変数を閉じてそのファイルを閉じますか?私はパラメータを使ってそれを行うだろうか? – softballgirl12

+0

また、グローバルではなくパラメータを使用して実行することもできます。グローバルの方が簡単ですが、パラメータはより柔軟です。個人的に言えば、データベースハンドル(ファイルハンドル)をこのような単純なプログラム用のグローバルにします。 KenY-N @ –

答えて

0

オープンファイルからファイル記述子を受け取る必要があります。 stile: FILE * myfile; myfile = fopen(myfile、 "r"); あなたは読書を続けることができます:http://www.cprogramming.com/tutorial/cfileio.html(私はそれが理解しやすいと思いますし、C++でも動作します)。 C++ stile http://www.cprogramming.com/tutorial/lesson10.html 彼はそれをとてもうまく説明します。

しかし、あなたは覚えている必要があります。 1)あなたは、ポインタ(データは文句を言わない失われ、この彼の唯一の方法を作成する必要があります)。 2)ディスクリプタを送受信する必要があります

関連する問題