Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using plain C.
I am getting responses from database that how many documents are available in DB.

I need to create a file which conains the information about that document.
Filename has specific format. The format is x000001.PDF
where,
x: document type
000001: is incnumber
PDF: extension
So for each response I need to change the 000001,000002,000003, etc....

Please help me out how can I do this.
rignt now I had used hard coded value.

char Incnumber[7] = "000001";
sprintf(filename,"%s%s.%s",type,Incnumber,"PDF");

FILE *fptr = fopen(filename,"w+");
.....

Thanks in advance.
Posted
Updated 23-Apr-10 1:05am
v2

You are already using sprintf, so try this:

sprintf(filename,"%s%07d.%s", type, i, "PDF");


%07d : d means print integer, 7 specifies the width of the number, and 0 means that pad the empty places with 0.

Check these links for more information:
http://msdn.microsoft.com/en-us/library/56e442dc(v=VS.90).aspx[^]
http://msdn.microsoft.com/en-us/library/25366k66(v=VS.90).aspx[^]

-Saurabh
 
Share this answer
 
I think you are getting the number.
Why dont you try the below format?
int nCount  = 1;
sprintf(filename,"00000%d%s.%s",type,nCount,"PDF");
 
Share this answer
 
Thanks for reply.

My IncNumber size is 6 chars 000000.
If i receive 1 digit/2digit/3digit...etc (12/100) responses, then its not working for me.
Depending on response numbers, how can I do the file name change?

Help me out please.
 
Share this answer
 
The solution I gave you works for all 7 digit number.

char filename[10];
sprintf(filename,"%07d", 1); // filename="0000001".
sprintf(filename,"%07d", 10); // filename="0000010".
sprintf(filename,"%07d", 100); // filename="0000100".
sprintf(filename,"%07d", 1000); // filename="0001000".
sprintf(filename,"%07d", 10000); // filename="0010000".
sprintf(filename,"%07d", 100000); // filename="0100000".
sprintf(filename,"%07d", 100000); // filename="1000000".


-Saurabh
 
Share this answer
 
v2
Thanks for the reply.

Working fine now :-O
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900