Clearing text output file

Dec 10, 2011 at 3:48pm
I have a program that reads and stores information, however, when you store information, it just overwrites what it had before, so some of the previous information remain. Is there any possibility to clear (erase everything) from a text file?
Dec 10, 2011 at 5:50pm
closed account (3hM2Nwbp)
I think you want the ios::trunc flag. Open the file with that flag and it'll automatically be cleared.

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
// May contain syntax errors, I didn't compile it.
#include <fstream>
int main()
{
  std::fstream stream("my_file", ios::in);
  if(stream) // if it exists
  {
    stream.close();
    stream.open("my_file", ios::out | ios::trunc);
    if(stream)
    {
        // Truncated.
        stream.close();
    }
    else
    {
      // Couldn't Truncate.
    }
  }
  else
  {
    // File didn't exist.
  }
  return 0;
}
Last edited on Dec 10, 2011 at 5:56pm
Dec 10, 2011 at 6:52pm
Hmmm, this is my new attempt, however it is still not working.

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
32
33
34
35
36
37
38
39
40
41
42
void hotel::removeCustomer(string nameReg)
{
	fstream readData ; // Reads data from file
	readData.open ("information.dat");	
	fstream printData; // Prints data to file
	printData.open ("information.dat", ios::trunc);
	int accountNum;
	
	if(readData.fail() == true){ // This will check if the file exists or not
	cout << "Error: cannot find file!" << endl;
	system("PAUSE");}

	readData >> i; // Reads the first number
	i = i - 1;
	numStore.push(i); // Stores number i in stack
	getline(readData,line); // Skip to next line

	for (int j = 0; j < i+1; j++) // Stores data of each line into stacks
	{
		readData >> nameOfCustomer >> account;
		nameStore.push(nameOfCustomer);
		accountStore.push(account);
		getline(readData,line);
	}
	
	printData << numStore.top() << "\n"; // Print number i into file

	for (int k = 0; k < i+1; k++)
	{
		if(nameStore.top() != nameReg) // If the name at top of stack does not equal input
		{
			printData << nameStore.top() << " " << accountStore.top() << "\n"; // Print data into file
			nameStore.pop(); // Pop the top
			accountStore.pop(); // Pop the top
		}
		else // If top == input
		{
			nameStore.pop(); // Pop the top
			accountStore.pop(); // Pop the top
		}
	}
}


1
2
3
4
5
6
7
int main()
{
	hotel registerD;
	string removeName;
	cin >> removeName;
	registerD.removeCustomer(removeName);
}


Contents in file (information.dat):
5
John 1728
James 6621
Jerry 7121
Jessica 8881
Jean 2712


When I input: Jerry

It should be like this (information.dat):
4
John 1728
James 6621
Jessica 8881
Jean 2712


However, that is not happening.

Does anyone know what's wrong?
Last edited on Dec 10, 2011 at 6:57pm
Dec 10, 2011 at 8:44pm
i think its because you open the file again with the object
1
2
 fstream printData; 
	printData.open ("information.dat", ios::trunc);

so it erases everything in the file before you can read it.
see if this code works for you
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
void hotel::removeCustomer(string nameReg)
{
	fstream readData ; // Reads data from file
	if( !readData.open( "information.dat" , ios::in ) )
        {
             cout<<"couldn't open the file\n";
             exit(1);
        }
        //found and open file correctly
        int i;
	readData >> i; // Reads the first number
	numStore.push(--i); // Stores number i in stack
        //i think you dont need to change to next line 
        //because there is nothing  else to read from the 1st line
	//getline(readData,line); // Skip to next line

	while( readData )//when it reaach EOF it will continue after the while statement
        {
		readData >> nameOfCustomer >> account;
		nameStore.push(nameOfCustomer);
		accountStore.push(account);
                //you might need the next line inorder to skip the end of line character
                //readData.get();
	}
	//and now that you have read all the file you can close it and open it again
       // this time with ios::trunc mode

      readData.close();
      if( !readData.open( "information.dat" , ios::out | ios::trunc ) )
        {
             cout<<"couldn't open the file\n";
             exit(1);
        }
      //so now you have an empty file and everything saved in the stack
      
      


	for (int k = 0; k < i+1; k++)
	{
		if(nameStore.top() != nameReg) // If the name at top of stack does not equal input
		{
			readData << nameStore.top() << " " << accountStore.top() << "\n"; // Print data into file
			nameStore.pop(); // Pop the top
			accountStore.pop(); // Pop the top
		}
		else // If top == input
		{
			nameStore.pop(); // Pop the top
			accountStore.pop(); // Pop the top
		}
	}
}

i didnt compile it but the main problem i think was that you were trying to read from an empty file.
also by using a stask to save your data, your new file will have the oposite order in names
you can use a FIFO queue instead.
Last edited on Dec 10, 2011 at 10:58pm
Dec 10, 2011 at 11:09pm
The code does not work,

both of these lines contain an error:
1
2
if( !readData.open( "information.dat" , ios::in ) )
if( !readData.open( "information.dat" , ios::out | ios::trunc ) )


error:
Error error C2171: '!' : illegal on operands of type 'void'
Error error C2451: conditional expression of type 'void' is illegal
Dec 11, 2011 at 2:47am
Okay, I removed the file checking and changed the while loop back to for loop because it was not working right. Now everything works perfectly fine after those changes. I can see where I made my mistakes before in my first attempt. I did not close the files and whatnot.
Topic archived. No new replies allowed.