2014-12-13 38 views
0

我正在嘗試編寫一個程序,該程序使用fstream從文本文件讀取和寫入整數,但在程序執行並退出後,文件不會在所有,內容保持不變。fstream無法覆蓋我的文本文件的內容

這裏是什麼我的文件是應該做一個簡短的描述:

  1. 此應用程序讀取一個文件,修改其內容,並寫入修改回同一個文件。
  2. 該文件包含3行整數。第一行表示第三行的整數數量。第二行指示已選擇第三行上的哪個整數(活動)。
  3. 文件中的第三行列出了所有整數(最多10個)。
  4. 一個不斷顯示在屏幕上的菜單。在菜單下方,程序顯示文件第三行中的所有整數
  5. 該程序還顯示當前選中哪個整數(活動)。
  6. 用戶可以通過按下菜單中的某個擴展鍵來選擇一個菜單項
  7. 按下「插入」將在所選整數之前插入一個整數,並使新插入的整數處於活動狀態。整數由用戶鍵入。如果列表已滿,則無法插入。
  8. 按下「刪除」刪除活動整數。
  9. 按「排序」按升序對列表進行排序。排序後的活動整數與排序前的活動整數相同。
  10. 按下「選擇」選擇列表中的下一個整數。如果選擇了最後一個整數,則此選項將選擇列表中的第一個項目。
  11. 按下「右移」將所選整數向右移動一個位置。如果選擇了最後一個整數,則無法向右移動。
  12. 按下「向左移動」將所選整數向左移動一個位置。如果選擇第一個整數,則向左移動將不可能。
  13. 按「Exit」結束應用程序。
  14. 程序將文件的內容保存到數組中,修改數組,並將數組的內容寫回到文件中。

[我的程序:]

//Description: Grade averages 
#include<iostream> 
#include<cstdlib> 
#include<fstream>//allows file streams 
#include<conio.h> 
#include<dos.h> 
#include <iomanip> 
#include <string> 
#include <windows.h> 
using namespace std; 
void menu();//declaration of the menu funtion 
int main() 
{ 
    int num = 0; 
    int next = 0; 
    fstream f_stream;//declares the stream that allows the program to read/write from the file 
    f_stream.open("base.txt");//opens the file 
    if (f_stream.fail())//checks if the file is present 
    { 
     cout << "base.txt is missing. program cannot run :(" << endl;//displays an error if the file can't be found 
     exit(1); 
    } 
    cout << "input stream successfully opened" << endl;//confirms that the file is available 
    int ch =0; 
    int values[12];//declares the array that will store the file's integers 
    do 
    { 
     menu();//displays a menu for inputs 
     while (!f_stream.eof())//runs until the end of the file is reached 
     { 
      f_stream >> next;//grabs values from the file 
      values[num] = next;//assigns a file input to an expression in the array 
      num++;//this keeps tally of the number of items in the file 
     } 
     cout << endl; 
     cout << "Here is the list of numbers on the third line: " << endl << endl; 
     for (int t = 2; t < num; t++)//this displays the values on the third line 
     { 
      cout << values[t] << endl; 
     } 
     cout << endl; 
     int num_of_integers = num - 2;//this is the number of files in the 3rd line. the first and second line numbers are subtracted 
     int active = values[1];//the integer from the second line displays the number that is active 
     int activenumber = values[active];//grabs the active number 
     cout << "Number of integers: " << values[0] << endl; 
     cout << "Current active integer: " << activenumber << endl; 

     ch=getch();//grabs keyboard input 
     if (ch == 0||ch == 224)// 
      ch=getch();//only allows special keys to be used 
     int newactivenumber; 
     int lastorder = num - 1;//this is the last element in the array 
     if (ch == 82)//if the "insert" key is pressed 
     { 
      if (num == 12)//if the maximum number of integers is present in the file 
      { 
       cout << "Cannot insert an integer before " << activenumber << endl << "Please delete an integer before inserting" << endl; 
      } 
      else if (values[active] == values[3])//if the active integer is the very first number on the second line 
      { 
       cout << "Please enter an integer to insert at the end of the number line" << endl; 
       cin >> values[num-1]; 
      } 
      else//inserts a number before the active integer 
      { 
       cout << "Please enter an integer to insert before " << activenumber << endl; 
       int previous = active - 1; 
       cin >> values[previous]; 
      } 
     } 
     else if (ch == 83)//if the "delete" key is pressed 
     { 
      cout << values[active] << " was deleted." << endl; 
      int temp, temp1; 
      for (int p = active; p<(lastorder);p++)//shifts all values in the array to the left 
      { 
       temp1 = p + 1; 
       values[p] = values[temp1]; 
      } 
      int new_integers = num_of_integers - 1; 
      values[0] = new_integers;//since a value was deleted, the new amount of integers is recorded 
      num--; 
     } 
     else if (ch == 80)//if the "arrow down" key is pressed 
     { 
      if (values[1] == lastorder)//if the last number on the list is the active number, then the first number on the list will become the new active number 
      { 
       newactivenumber = 2; 
       values[1] = newactivenumber; 
      } 
      else//the next number of the list will become the new active number 
      { 
       newactivenumber = values[1] + 1; 
       values[1] = newactivenumber; 
      } 
      cout << values[newactivenumber] << " is now selected" << endl; 

     } 
     else if (ch == 77)//if "arrow right" is pressed, the active number is shifter to the right 
     { 
      if (values[active] == values[num-1])//disallows shifting if the active number is the last number on the list 
      { 
       cout << "I'm sorry, Dave. I'm afraid I can't do that." << endl; 

      } 
      else 
      { 
       int temp; 
       temp = values[active]; 
       int rightno = active + 1; 
       values[active] = values[rightno]; 
       values[rightno] = temp; 
       cout << "The active number has been moved 1 position right in the list" << endl; 

      } 
     } 
     else if (ch == 60)//IF the F2 key is pressed, the array numbers are sorted from smallest to greatest 
     { 
      int i; 
      int temporary = values[active]; 
      for(i=2;i<num;i++) 
      { 
       for(int j=2;j<num-i-1;j++) 
       { 
        if(values[j]>values[j+1]) 
        { 
         int temp = values[j]; 
         values[j] = values[j+1]; 
         values[j+1] = temp; 
        } 
       } 
      } 
      //displaying result 
      cout<<"Sorted list"<<endl; 
      for(i=2;i<num;i++) 
      { 
       cout<<values[i] << endl; 
      } 
      for(i=2;i<num;i++)//this loop ensures that the active number remain the same, even after the sorting 
      { 
       if (values[i] == activenumber) 
       { 
        values[1] = i; 
       } 
      } 

     } 
     else if (ch == 75)//if "arrow left" is pressed, it shifts active number to the left 
     { 
      if (values[active] == values[2])// 
      { 
       cout << "I'm sorry, Dave. I,'m afraid I can't do that." << endl; 

      } 
      else 
      { 
       int temp; 
       temp = values[active]; 
       int leftno = active - 1; 
       values[active] = values[leftno]; 
       values[leftno] = temp; 
       cout << "The active number has been moved 1 position left in the list" << endl; 

      } 
     } 
     else if (ch == 59)//terminates program if F1 is pressed 
     { 
      f_stream.close(); 
      cout << "streams successfully closed" << endl; 
      break; 
     } 
     else//if an invalid key is pressed 
     { 
      cout << "Try again" << endl; 

     } 
     for (int out = 0; out < num; out++)//exports array values to the file 
     { 
      if (out == 0 || out == 1) 
      { 
       f_stream << values[out] << '\n'; 
      } 
      else if (values[out]==0) 
      { 
       f_stream << ""; 
      } 
      else if (out != num-1) 
      { 
       f_stream << values[out] << " "; 
      } 
      else 
      { 
       f_stream << values[out]; 
      } 
     } 
     system("PAUSE"); 
     system("CLS"); 
    } 
    while (ch != 59); 
    f_stream.close(); 
    cout << "streams successfully closed" << endl; 
    return 0; 
} 
void menu()//self-explanatory 
{ 
    cout<<"Menu:" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "1.Insert" << "Press the \"Insert\" key" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "2.Delete" << "Press the \"Delete\" key" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "3.Sort" << "Press the \"F2\" key:" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "4.Select" << "Press the \"Down Arrow\" key" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "5.Move Right" << "Press the \"Right Arrow\" key" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "6.Move Left" << "Press the \"Left Arrow\" key" << endl; 
    cout << setiosflags(ios::left) << setw(14)<< "7.Exit"<< "Press the \"F1\" key" << endl; 
} 

[Base.txt]

10 
9 
-23 5 23 56 0 -32 3 9 11 66 
+0

「這是什麼我的文件是應該做一個簡短的描述:」 ......真的嗎? – quantdev 2014-12-13 03:00:27

+1

您可以打開輸入或輸出的fstream,而不是兩者。 – stark 2014-12-13 03:16:13

回答

0

當使用fstream的數據類型是至關重要指定的文件訪問類型。有多種方法去了解它在你的代碼,但最簡單的對我來說似乎是:

fstream f_stream; 
f_stream.open("base.txt", ios::in | ios:out)