• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 812
  • Last Modified:

automatic file renaming like file1, file2, file3, file4, so on.....

hi all!
i need to dynamically update file names.

all i need is to have a string (file path) updated like file1, file2, file3,, so on.
could some one help me!!!!!!!!!

thanks in advance.
0
vasureddym
Asked:
vasureddym
  • 4
  • 4
1 Solution
 
sunnycoderCommented:
Hi vasureddym,

man 3 mktemp

SYNOPSIS
       #include <stdlib.h>

       char *mktemp(char *template);

DESCRIPTION
       The  mktemp()  function  generates a unique temporary file
       name from template.  The last six characters  of  template
       must  be  XXXXXX and these are replaced with a string that
       makes the filename unique. Since it will be modified, tem­
       plate  must  not  be  a  string  constant,  but  should be
       declared as a character array.

Sunnycoder
0
 
vasureddymAuthor Commented:
sunnycoder,

that's ok but i need all files to have names sequentially like file1,file2,file3.

-vasu
0
 
sunnycoderCommented:
if you want to generate the names yourself

int i = 1;

char name[10];

while ( .. )
{
       snprintf ( name, 9, "file%d", i++ );
       fopen ( name, .. );
       ...
}
0
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

 
vasureddymAuthor Commented:
hi sunny!

i'm not able to find the library for the function "snprintf()"

-vasu
0
 
vasureddymAuthor Commented:
hi sunny!

i'm not able to find the library for the function "snprintf()"

-vasu
0
 
sunnycoderCommented:
you do not link to the library explicitly .... it should be linked automatically ...
the function is delcares in stdio.h and is implemented in glibc

what is the error message you get ? ... Can you work with sprintf ?
0
 
vasureddymAuthor Commented:
sorry!
i read it as snprintf instead of sprintf.

it is working.


void main()
{
int i = 1;

char name[10];
clrscr();
for(;i<=4;i++)
{
       sprintf( name,"file%d", i );

      printf("\n%s", name);
}

getch();

}

. thanks sunny..
but a dought, what for did u use 9 in sprintf function?
thanks
-vasu
0
 
sunnycoderCommented:
That was actually snprintf ... snprintf is a safer version of sprintf in which you can specify the maximum length of string to be printed thereby avoiding risks of buffer overruns
0
 
stefan73Commented:
Hi vasureddym,
try _snprintf.

Cheers,
Stefan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now