C++ fstream无法覆盖我的文本文件的内容
我曾试图编写一个程序,使用fstream从文本文件中读取和写入整数,但在执行该程序并从中退出后,该文件根本不会更改,并且内容保持不变C++ fstream无法覆盖我的文本文件的内容,c++,arrays,sorting,fstream,C++,Arrays,Sorting,Fstream,我曾试图编写一个程序,使用fstream从文本文件中读取和写入整数,但在执行该程序并从中退出后,该文件根本不会更改,并且内容保持不变 下面是我的文件应该做什么的简短描述: 此应用程序读取文件,修改其内容,然后将修改写回同一文件。 该文件包括3行整数。第一行表示第三行的整数数。第二行指示第三行上的哪个整数被选中为活动整数。 文件中的第三行列出了最大值为10的所有整数。 屏幕上不断显示的菜单。在菜单的右下方,程序显示文件第三行中的所有整数 该程序还显示当前选定的活动整数。 用户可以通过按菜单中的一个
下面是我的文件应该做什么的简短描述: 此应用程序读取文件,修改其内容,然后将修改写回同一文件。 该文件包括3行整数。第一行表示第三行的整数数。第二行指示第三行上的哪个整数被选中为活动整数。 文件中的第三行列出了最大值为10的所有整数。 屏幕上不断显示的菜单。在菜单的右下方,程序显示文件第三行中的所有整数 该程序还显示当前选定的活动整数。 用户可以通过按菜单中的一个扩展键来选择菜单项 按“插入”将在所选整数之前插入一个整数,并使新插入的整数处于活动状态。整数由用户键入。如果列表已满,则无法插入。 按“删除”删除活动整数。 按“排序”将列表按升序排序。排序后的活动整数与排序前的活动整数相同。 按“选择”选择列表中的下一个整数。如果选择了最后一个整数,此选项将选择列表中的第一项。 按“向右移动”将所选整数向右移动一个位置。如果选择了最后一个整数,则无法向右移动。 按“向左移动”将所选整数向左移动一个位置。如果选择第一个整数,则无法向左移动。 按“退出”结束应用程序。 程序将文件内容保存到数组中,修改数组,然后将数组内容写回文件。 [我的节目:]
//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;
}
使用fstream数据类型时,指定文件访问的类型至关重要。在您的代码中有许多方法可以实现这一点,但对我来说最简单的方法似乎是:
fstream f_stream;
f_stream.open("base.txt", ios::in | ios:out)
下面是我的文件应该做什么的简短描述:。。。真的吗?你可以打开一个fstream进行输入或输出,而不是同时打开两者。
fstream f_stream;
f_stream.open("base.txt", ios::in | ios:out)