-
January 3rd, 2018, 12:16 AM
#1
[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.
-
January 3rd, 2018, 04:30 AM
#2
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)
-
January 3rd, 2018, 04:34 AM
#3
Re: File name change
Can't figure it out. Any more suggestions?
-
January 3rd, 2018, 05:09 AM
#4
Re: File name change
Originally Posted by A_Singh
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
-
January 3rd, 2018, 05:11 AM
#5
Re: File name change
I didn't understand that.
-
January 3rd, 2018, 05:11 AM
#6
Re: File name change
I'd appreciate if someone can write that in simple language.
-
January 3rd, 2018, 05:27 AM
#7
Re: File name change
Originally Posted by A_Singh
I'd appreciate if someone can write that in simple language.
Define "simple language".
Victor Nijegorodov
-
January 3rd, 2018, 05:28 AM
#8
Re: File name change
Write that in an easily understandable manner, ok ?
-
January 3rd, 2018, 06:15 AM
#9
Re: File name change
Originally Posted by A_Singh
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)
-
January 3rd, 2018, 06:19 AM
#10
Re: File name change
Thanks 2kaud , you are definately a professional programmer to answer all my queries.
-
January 3rd, 2018, 08:13 AM
#11
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.
-
January 3rd, 2018, 10:34 AM
#12
Re: File name change
Originally Posted by A_Singh
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)
-
January 3rd, 2018, 10:39 AM
#13
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?
-
January 3rd, 2018, 10:45 AM
#14
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)
-
January 3rd, 2018, 10:47 AM
#15
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)
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
|