CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2018
    Posts
    3

    Create file, then increment by 1

    Hey Guys,

    I'm looking for some code that works as follows:-

    I have a click box, whenever I press the button it creates a file in c:/results called results1.txt.

    the second time I press the button, if the file results1.txt exists it creates a new file called results2.txt and so on increasing the number.

    Would anybody be able to help please?

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

    Re: Create file, then increment by 1

    So what is your problem? Search for a file? Create a new file?
    Something else?
    Victor Nijegorodov

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

    Re: Create file, then increment by 1

    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)

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

    Re: Create file, then increment by 1

    Quote Originally Posted by Merlin_3000uk View Post
    ...
    I have a click box, whenever I press the button it creates a file in c:/results called results1.txt.

    the second time I press the button, if the file results1.txt exists it creates a new file called results2.txt and so on increasing the number.

    Would anybody be able to help please?
    Some useful links:
    https://msdn.microsoft.com/en-us/lib...or=-2147217396
    https://msdn.microsoft.com/en-us/lib...or=-2147217396
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2018
    Posts
    3

    Re: Create file, then increment by 1

    Quote Originally Posted by VictorN View Post
    So what is your problem? Search for a file? Create a new file?
    Something else?
    Hey Victor,

    What I'm looking to do is create a file, then if the file exists don't overwrite the old file but create a new file.

    Each time the code runs it increases the file name to say "file.1.txt", "file.2.txt", "file.3.txt" each time creating a new file.

    Thanks
    Simon

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

    Re: Create file, then increment by 1

    Quote Originally Posted by Merlin_3000uk View Post
    Hey Victor,

    What I'm looking to do is create a file, then if the file exists don't overwrite the old file but create a new file.

    Each time the code runs it increases the file name to say "file.1.txt", "file.2.txt", "file.3.txt" each time creating a new file.

    Thanks
    Simon
    Well, again: what is the thing (exactly!) you cannot achieve?
    Victor Nijegorodov

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

    Re: Create file, then increment by 1

    Quote Originally Posted by Merlin_3000uk View Post
    Hey Victor,

    What I'm looking to do is create a file, then if the file exists don't overwrite the old file but create a new file.

    Each time the code runs it increases the file name to say "file.1.txt", "file.2.txt", "file.3.txt" each time creating a new file.

    Thanks
    Simon
    ... and see post #3!
    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)

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