Filename counter and strings

I want to have a program that outputs numbered files using a 4 digit scheme. So file #1 would be file0001. It must be 4 digits.

I was thinking of doing something like having a counter of where you're at, converting the digit to a string and putting the appropriate amount of 0's in front of it. Is there any way to place characters in front of a string? strcat only does it at the end, which is wrong.

Another way I thought to do it is to add the number to the end of the string and just take the 4 characters, but I see no apparent way of doing this either. Plus I like the above solution more.

Also, is int conversion to a string possible as that's the idea behind this?

Thanks.
[736 byte] By [Sivakk] at [2007-11-20 11:47:20]
# 1 Re: Filename counter and strings
There are a number of ways to do this ... here is one:

#include <sstream> // stringstream
#include <string> // string
#include <iomanip> // setw() and setfill()
#include <iostream> // cout

std::string GetFilename(const char * fname , int counter , int width , const char * ext)
{
std::stringstream ss;
ss << fname << std::setw(width) << std::setfill('0') << counter << ext;
return ss.str();
}

int main()
{
for (int i=0; i<10; ++i)
{
std::cout << GetFilename("file",i,4,".txt") << "\n";
}

return 0;
}
Philip Nicoletti at 2007-11-9 1:26:12 >
# 2 Re: Filename counter and strings
Is there any way to place characters in front of a string? strcat only does it at the end, which is wrong.

Apply strcat to get the o's at the end then reverse the string.

If file name is coming in your function as 'file#1' then i dont see any issue in extracting 'file' in a seprate string. Extract your file number in a seprate string say 'num' and put required number of zeros at the end of 'num'. Reverse 'num' and append it at the end of 'file'.
ajaysinha at 2007-11-9 1:27:15 >
# 3 Re: Filename counter and strings
I want to have a program that outputs numbered files using a 4 digit scheme. So file #1 would be file0001. It must be 4 digits.

The easiest way to do this is:

char filebasename[100];
char filenamenumbered[100];

sprintf(filenamenumbered,"%s%04d.txt",filebasename,count);

I've never understood why C++ moved away from the printf/scanf class of IO functions, anyway. They're so much more convenient than cin/cout in many cases.

Specifically:

%s: replace with string.
%d: replace with int.
%4d: replace with int, pad up to four characters.
%04d: replace with int, pad up to four characters which are 0s.
Lindley at 2007-11-9 1:28:16 >
# 4 Re: Filename counter and strings
I've never understood why C++ moved away from the printf/scanf class of IO functions, anyway. They're so much more convenient than cin/cout in many cases.
Because they are not typesafe and they are not compatible with objects.

Viggy
MrViggy at 2007-11-9 1:29:10 >
# 5 Re: Filename counter and strings
To be honest, I find the notion of doing IO on objects to be generally bizarre anyway. It can lead to some nice behavior in a few cases, but in general it just encourages people to write unreadable code.

I'm one of those people who likes objects primarily as a means of clarifying code by abstracting lower-level details, but prefers to keep a primarily C style otherwise.
Lindley at 2007-11-9 1:30:20 >