Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Truncation (Why does the text.txt not stay empty?)
Truncation [message #46981] Sun, 09 October 2016 20:20 Go to next message
Freemen is currently offline  Freemen
Messages: 4
Registered: October 2016
Location: Canada
Junior Member
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
char filename[] = "text.txt";
std::fstream file;

file.open(filename, ios::out | ios::trunc);
file << "This text will now be inside of " << filename << std::endl;
file.close();

while(true) {
  file.open(filename, std::ofstream::out | std::ofstream::trunc);
  file.close();
  //do stuff with truncated file
}

file.open(filename, ios::in);
std::string line;
std::getline(file, line);

std::cout << line << std::endl;
}
text.txt emptied //I can double click and see everythings gone so I type something and save, and open it again and the text.txt has something in it now? even when the wile loop is on?
Re: Truncation [message #46983 is a reply to message #46981] Sun, 09 October 2016 22:46 Go to previous message
Klugier is currently offline  Klugier
Messages: 1076
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello,

As I understand truncate flag in C++ standard library - it loads files and clear it. For example this line:
file.open(filename, ios::out | ios::trunc); // If not exists create - if exists clear.
file.close(); // Here we have got always empty file - even if file is not empty!


In the Upp framework you will achieve exactly the same with SaveFile(filePat, data) function. It will clear old file and put new content. So, equivalent to the upper code is:
SaveFile("text.txt", "");


Sincerely,
Klugier


U++ - one framework to rule them all.

[Updated on: Sun, 09 October 2016 22:47]

Report message to a moderator

Previous Topic: Refrences
Next Topic: Using a label as a link to a folder
Goto Forum:
  


Current Time: Tue Apr 16 23:34:39 CEST 2024

Total time taken to generate the page: 0.02152 seconds