Currently im making a little program that can overwrite a .txt file i have included in my .cpp file (ifstream infil("biljett.txt");
I made an example. This is for a cenima. The purpose of this program is for "people" to see what movie that is avaible for them to watch (by selecting in the program).
If someone uses this program for example and selects Movie1, and enter how many tickets they want to buy. Once he/she close the program, it will overwrite the current .txt file biljett.txt.
And in the end, once all the tickets are sold out, the program is going to "reset" the biljett.txt as it was from the beginning. My problem is, i can't find anywhere about this overwrite thing, i know it's possible cause i have seen a similar program around the webb.
Can anyone help me out?
Here is the code.
NOTE: Im from Sweden, the sentences translated from the beginning
Movie1: How many tickets do you want to reserve?
What movie do you wish to see?, hit 1 for Movie1, 2 for Movie2, 3 Movie3,4 Movie4
void menu()
{
int a,b, c, d ;
char s;
s = _getch();
if (s=='1')
{
cout<<"Movie1: skriv in antal biljetter du vill köpa: ";
cin>>a;
}
else if (s=='2')
{
cout<<"Movie2: skriv in antal biljetter: ";
cin>>b;
}
else if (s=='3')
{
cout<<"Movie3: skriv in antal biljetter: ";
cin>>c;
}
else if (s=='4')
{
cout<<"Movie4: skriv in antal biljetter: ";
cin>>d;
}
}
main()
{
int n=0, menu();
char tecken;
ifstream infil("biljett.txt");
while (infil.get(tecken))
{
cout.put(tecken);
if (tecken == '\n')
n++;
}
cout<<endl;
cout<<endl;
cout<<"Vilken film vill du se?, klicka 1 för Movie1, 2 för Movie2, 3 Movie3,4 Movie4.";
cout<<endl;
cout<<endl;
Salongnummer: Salonnumber
Filmnamn: Name of the movie
Antalplatser: Total amount of seats
Antal sålda biljetter: Total amount of sold tickets (to seats)
I'm just wondering, why are you reading the file character by character and just echoing that to the screen?
Why don't you read the data into some variables?
Also, I'm wondering why you want to reset the file "biljett.txt" back to it's original state. Don't you want to keep track of the number of seats/tickets reserved?
If you look at the .txt file what ever you inputted should be in there, and when it starts again, and you change it, it should write over that. Maybe I don't understand the question.
Ex:
It's when i start my program and enter numbers in there that it is going to overwrite the .txt.
And thanks LittleQuick. Thats helped me out alot.
EDIT: Hi again, LittleQuick, i noticed that everything i write into the program overwrites the whole .txt file with what i wrote in.
Is it possible to make it overwrite a specified part of the notes, for example only some number, and possible "add (+)" the number wroten in with the number currently in the document? (like a calculator), OR is that the while loop you were talking about?.
I supose that goes into the loop aswell. I have to use a variable int x , x= x + line or something?
Is it possible to make it overwrite a specified part of the notes
That would be what's called a random-access file. I would think it would be easier to just read your data, add or subtract whatever you need it to, and then overwrite it with your new data. Rather than fooling around with record-sizes for such a small file.