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

    [RESOLVED] File name change

    Here’s my code to create a file to c ++.
    Code:
    Code:
    #include <iostream>
    #include <conio.h>
    #include <fstream>
    using namespace std;
    
    int main()
    {
            ofstream myFile;
            myFile.open(’’test.txt’’);
            myFile.close();
    
    _getch();
    return 0;
    }
    What I want is that first time I run the program, the file created should be: test, second time: test1, third time: test2 etc...
    How can I do that .?
    Please help !
    Last edited by A_Singh; January 3rd, 2018 at 12:32 AM.

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

    Re: File name change

    Try to open the file for input. If the file exists then add 1 or 2 or 3 etc to the file name and then try to open again. Repeat until the file doesn't open. Then you have the name of the new file to create.

    A refinement is to save the last created name in an ini file or registry and use that as the start basis for the above method.
    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. #3
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    Can't figure it out. Any more suggestions?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: File name change

    Quote Originally Posted by A_Singh View Post
    Can't figure it out. Any more suggestions?
    2kaud suggested you the simplest way to implement what you asked.
    What is the problem to implement it?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    I didn't understand that.

  6. #6
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    I'd appreciate if someone can write that in simple language.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: File name change

    Quote Originally Posted by A_Singh View Post
    I'd appreciate if someone can write that in simple language.
    Define "simple language".
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    Write that in an easily understandable manner, ok ?

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

    Re: File name change

    Quote Originally Posted by A_Singh View Post
    Write that in an easily understandable manner, ok ?
    It was!

    This code will create 10 different named files starting from test1 to test10 (assuming they don't already exist!).

    Code:
    #include <string>
    #include <fstream>
    using namespace std;
    
    string newfile()
    {
    	const string filstem = "test";
    	const size_t fnumstart = 1;
    
    	string filnam;
    	size_t fnum = fnumstart;
    
    	for (ifstream ifs; ifs.open(filnam = filstem + to_string(fnum++)), ifs.is_open(); ifs.close());
    
    	return filnam;
    }
    
    int main()
    {
    	for (size_t i = 0; i < 10; ++i)
    		ofstream ofs(newfile());
    }
    Last edited by 2kaud; January 3rd, 2018 at 06:47 AM.
    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)

  10. #10
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    Thanks 2kaud , you are definately a professional programmer to answer all my queries.

  11. #11
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    Actually I want my first run of the program of create test, second test1, third test2. Etc..
    Your program creates these files in one run only.

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

    Re: File name change

    Quote Originally Posted by A_Singh View Post
    Actually I want my first run of the program of create test, second test1, third test2. Etc..
    Your program creates these files in one run only.
    My test program just uses a loop to create the 10 files for testing.

    Code:
    int main()
    {
    	//for (size_t i = 0; i < 10; ++i)
    		ofstream ofs(newfile());
    }
    This will just create the next file in sequence for each time the program is run. The code in main() is just a test of newfile() function.
    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)

  13. #13
    Join Date
    Mar 2017
    Posts
    105

    Re: File name change

    Thanks 2kaud it's resolved.
    By the way, do you know that whenever we edit a .XLSX (excel) file using c++, after editing excel shows the file as corrupt, any solution to that?

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

    Re: File name change

    Also note that if you ant the file names to start test, test1, test2 rather than test1, test2, test3 then consider

    Code:
    string newfile()
    {
    	const string filstem = "test";
    	const size_t fnumstart = 0;
    
    	string filnam;
    	size_t fnum = fnumstart;
    
    	for (ifstream ifs; ifs.open(filnam = filstem + (fnum ? to_string(fnum) : "")), ifs.is_open(); ifs.close(), ++fnum);
    
    	return filnam;
    }
    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)

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

    Re: File name change

    By the way, do you know that whenever we edit a .XLSX (excel) file using c++, after editing excel shows the file as corrupt, any solution to that?
    That would depend upon how the c++ code interacts with the .xlsx file.
    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)

Page 1 of 2 12 LastLast

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