How to overwrite a .txt file with a .cpp document dev-C++?

Hi!

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

############################################################################
//Start

#include <iostream>
#include <conio.h>
#include <windows.h>
#include<fstream>



using namespace std;


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;


menu();

cout<<endl;
infil.close();
system("PAUSE");
}

//Stop
#############################################################################

This is what the textfile looks like:

Salongnummer: Salonnumber
Filmnamn: Name of the movie
Antalplatser: Total amount of seats
Antal sålda biljetter: Total amount of sold tickets (to seats)

Salongsnummer Filmnamn Antalplatser Antal sålda biljetter
1 Movie1 300 255
2 Movie2 200 15
3 Movie3 200 200
4 Movie4 300 0



If you need me to provide more information to help me out, just tell me.

Regards Johan

Last edited on
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?
It should overwrite automatically.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
    string line;
    string stuff;
cout << "Write something...\n";
cin >> stuff;
  ofstream myfile1;
  myfile1.open ("example.txt");
  myfile1 << stuff;
  myfile1.close();

  ifstream myfile2 ("example.txt");
  if (myfile2.is_open())
  {
    while ( myfile2.good() )
    {
      getline (myfile2,line);
      cout << line << endl;
    }
    myfile2.close();
  }
  else cout << "Unable to open file"; 
main();

  return 0;
}
My mistake, I see. You need to put a counting loop in there. While ticketssold <= ticketshave do program, ticketssold > ticketshave break.
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?

Last edited on
*bump* ok?
johanbest said:
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.
Topic archived. No new replies allowed.