CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    What do you mean ?
    Last edited by A_Singh; January 3rd, 2018 at 11:13 AM.

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: File name change

    You say you edit .XLSX (excel) files using c++ and that after the editing the files are corrupt. Therefore the cause of the corruption is the action of the c++ code upon the files. Therefore the problem is with the c++ code.
    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)

  3. #18
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    Ok

  4. #19
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: File name change

    The problem with incompatibility of .csv and .xlsx files could happen, as .csv files are plain textual files while excel files have their own format, so even blank excel document has its own structure.
    Just my few cents.
    Last edited by AvDav; January 3rd, 2018 at 12:09 PM.

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: File name change

    first time I run the program, the file created should be: test, second time: test1, third time: test2 etc
    This has turned out quite interesting. See also http://forums.codeguru.com/showthrea...initialisation As well as the uses of c++ fstream, there is also the c++ TS filesystem and for windows, _access() (I think its access() for posix?). I've got some test code for timings that time the average time (over 5 runs) for creating 400 files using five different methods.

    The test code is

    Code:
    #include <string>
    #include <filesystem>
    #include <fstream>
    #include <chrono>
    #include <tuple>
    #include <iostream>
    #include <io.h>
    #include <numeric>
    using namespace std;
    using namespace std::chrono;
    using namespace std::experimental::filesystem;
    
    const string filstem = "test"s;
    const size_t fnumstart = 0U;
    const string fext = ".asd"s;
    
    using filfunc = string(*)();
    
    string newfile1()
    {
    	string filnam;
    
    	for (size_t fnum = fnumstart, got = true; got; ++fnum) {
    		ifstream ifs(filnam = filstem + (fnum ? to_string(fnum) : "") + fext);
    		got = ifs.is_open();
    	}
    
    	return filnam;
    }
    
    string newfile2()
    {
    	string filnam;
    
    	for (size_t fnum = fnumstart; exists(filnam = filstem + (fnum ? to_string(fnum) : "") + fext); ++fnum);
    
    	return filnam;
    }
    
    string newfile3()
    {
    	string filnam;
    
    	for (auto[fnum, ifs] = make_tuple(fnumstart, ifstream()); ifs.open(filnam = filstem + (fnum ? to_string(fnum) : "") + fext), ifs.is_open(); ifs.close(), ++fnum);
    
    	return filnam;
    }
    
    string newfile4()
    {
    	string filnam;
    
    	for (size_t fnum = fnumstart; !_access((filnam = filstem + (fnum ? to_string(fnum) : "") + fext).c_str(), 0); ++fnum);
    
    	return filnam;
    }
    
    string newfile5()
    {
    	string filnam;
    	size_t fnum = fnumstart;
    
    	for (ifstream ifs; ifs.open(filnam = filstem + (fnum ? to_string(fnum) : "") + fext), ifs.is_open(); ifs.close(), ++fnum);
    
    	return filnam;
    }
    
    double process(filfunc ff) {
    	const size_t numfiles = 400U;
    	const size_t loops = 5U;
    
    	double res[loops] = {0.0};
    
    	for (auto& r : res) {
    		system("del *.asd /q");
    		auto start = std::chrono::high_resolution_clock::now();
    		for (size_t n = 0U; n < numfiles; ++n)
    			ofstream ofs(ff());
    
    		auto diff = std::chrono::high_resolution_clock::now() - start;
    		r = std::chrono::duration<double, std::milli>(diff).count();
    	}
    
    	const double av = 0.0;
    
    	return accumulate(begin(res), end(res), av) / loops;
    }
    
    int main()
    {
    	cout << "Method 1" << endl;
    	cout << process(newfile1) << endl << endl;
    
    	cout << "Method 2" << endl;
    	cout << process(newfile2) << endl << endl;
    
    	cout << "Method 3" << endl;
    	cout << process(newfile3) << endl << endl;
    
    	cout << "Method 4" << endl;
    	cout << process(newfile4) << endl << endl;
    
    	cout << "Method 5" << endl;
    	cout << process(newfile5) << endl << endl;
    }
    The timings for my system are

    Code:
    Method 1
    19785.7
    
    Method 2
    7517.38
    
    Method 3
    19633.7
    
    Method 4
    7343.58
    
    Method 5
    19109.8
    The fastest method for this test is 4 which uses _access(). The issue for filesystem for VS is that it's namespace is experimental and I'm not sure about using experimental code in production systems?
    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)

  6. #21
    Join Date
    Jan 2015
    Posts
    16

    Re: File name change

    The issue for filesystem for VS is that it's namespace is experimental and I'm not sure about using experimental code in production systems?
    Yes std::filesysystem is considered experimental, as are any of the other C++17 features, remember C++17 has not been ratified. However if you're afraid to use std::filesystem then you may want to consider boost::filesystem.

  7. #22
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: File name change

    remember C++17 has not been ratified
    Yes it has - but <filesystem> has the status of a TS. You can now purchase the official c++17 standard. Work has already started on c++20. See https://isocpp.org/std/the-standard
    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)

  8. #23
    Join Date
    Jan 2015
    Posts
    16

    Re: File name change

    Yes it has
    Not according to the standards status page: https://isocpp.org/std/status
    The committee has completed work on C++17, which is now in its final ISO balloting process, and began work on C++20 in July.
    I don't believe that the final balloting is complete as of today.

    Even if there is a difference between the two pages the page you linked shows a publish date of 12-2017 so I doubt that your compiler has been updated since the publication date indicated.

    but <filesystem> has the status of a TS.
    The std::filesystem has been added to the standard with C++17. See the final working draft (link provided by you above) Table 16 on page 457.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured