-
April 28th, 2019, 10:20 AM
#1
trancate fonction
Hi,
I'm using c++ (2011) with the truncate function. I would like to cut the file fichier.txt after the byte 1000.
So here is my programm
I would like to use the truncate function.
Here is the program
Code:
using namespace std;
#include <string>
#include <fstream>
#include <stdio.h>
#include <unistd.h>
// namespace std;
// {
// include <string>
// }
int main()
{
///// creation d'un fichier random avec des a aux bits Ã* supprimer
/// marche pas
fstream lulu;
lulu.open("C:/Users/H2O/Desktop/odoo_exe/fichier.txt",ios::out);
for (int i=1;i<900;i=i+1)
{
lulu << "bonjour le monde"<<i<<"\n";
}
truncate ("C:/Users/H2O/Desktop/odoo_exe/fichier.txt",10000);
lulu.close();
return 0;
}
And here is the file
bonjour le monde 1
...../ like that
bonjour le monde478
bonjour le monde479
bonjour le monde480
bonjour le monde481
// the file should stop here but it starts again
bonjour
bonjour le monde890
bonjour le monde891
bonjour le monde892
bonjour le monde893
bonjour le monde894
bonjour le monde895
bonjour le monde896
bonjour le monde897
bonjour le monde898
bonjour le monde899
So i don't understand what is going on
thank you
Last edited by 2kaud; April 29th, 2019 at 03:19 AM.
Reason: Added code tags
-
April 28th, 2019, 04:56 PM
#2
Re: trancate fonction
Originally Posted by manoir
I'm using c++ (2011) with the truncate function.
The truncate function doesn't seem to belong to the C++ standard but I found a description here,
https://www.ibm.com/support/knowledg...bd00/trunc.htm
It says if truncate returns -1 you can check the errno variable to get an indication what's wrong.
My guess would be that you need to close the file before you call truncate. You can easily check if that's the problem by just swapping two lines like,
Code:
lulu.close(); // close file before calling truncate
truncate ("C:/Users/H2O/Desktop/odoo_exe/fichier.txt",10000);
-
April 29th, 2019, 01:36 AM
#3
Re: trancate fonction
I suggest to use the portable C++ Standard resize_file() method. It's part of the C++ Standard Library std::filesystem.
See https://en.cppreference.com/w/cpp/fi...em/resize_file
-
April 29th, 2019, 03:32 AM
#4
Re: trancate fonction
As a general rule, you should always check return value/error code from a called function for problems arising. Some C++ functions throw exceptions rather than a return value/error code. In these cases these functions should be used within the bounds of a try/catch to check for errors in the called function. The function documentation will describe how issues are reported.
Functions which take a file name as a parameter usually do not succeed if the file is already open. Open files should be first closed before these functions are used.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 29th, 2019, 04:10 AM
#5
Re: trancate fonction
Just a note: The C++ Standard resize_file() method I mentioned earlier requires C++17.
-
April 29th, 2019, 04:47 AM
#6
Re: trancate fonction
Yes i think truncate function do not belong to c++ but linux. When i close the file before truncate it i do not have that problem anymore, it truncate the file.
Thank you for your help, i was stuck with that problem.
-
April 29th, 2019, 08:02 AM
#7
Re: trancate fonction
Originally Posted by manoir
i was stuck with that problem.
Or the problem was stuck with you!
You can avoid truncation altogether by only writing exactly what you want on the file in the first place.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|