dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: trancate fonction

  1. #1
    Join Date
    Apr 2019
    Posts
    2

    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

  2. #2
    Join Date
    Feb 2017
    Posts
    432

    Re: trancate fonction

    Quote Originally Posted by manoir View Post
    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);

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,148

    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
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,690

    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++17 Compiler: Microsoft VS2019 (16.1.3)

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,148

    Re: trancate fonction

    Just a note: The C++ Standard resize_file() method I mentioned earlier requires C++17.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Apr 2019
    Posts
    2

    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.

  7. #7
    Join Date
    Feb 2017
    Posts
    432

    Re: trancate fonction

    Quote Originally Posted by manoir View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)