Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to get the extension in ".txt" with a date format time?

Posted on 2008-09-29
13
Medium Priority
?
196 Views
Last Modified: 2010-04-01
Hello EE,
i have some problem with this code
how to get "myfile-somerandomnumberhere-Mon_sep_29_21:59:01_2008.txt"?
std::stringstream ssFilename; //nuove variabile per uploaddare il file
        time( &aclock );                 /* Get time in seconds */
 
        newtime = localtime( &aclock );  /* Convert time to struct */
                                         /* tm form */
 
        ssFilename
        << asctime(newtime)
        << "-"
        
        << "myfile-"
        << acUserName// add user name
        << "-"
        << std::setw(5)
        << std::setfill('0')
        << (rand() % 100000)
        << ".txt";
 
        std::string const &  sFilename = ssFilename.str();

Open in new window

0
Comment
Question by:kagami01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 450 total points
ID: 22602546
You need to rearranage the code

   // Seed the random-number generator with the current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );
 
	ssFilename
		<< "myfile-"
		<< (rand() % 100000)
		<< "-"
		<< "acUserName"// add user name
		<< "-"
        << asctime(newtime)
        << ".txt";
	std::string const &  sFilename = ssFilename.str();
	cout << sFilename.c_str();

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22603502
You probably also want to replace the spaces in the filename with underscores after constructing it ...
0
 

Author Comment

by:kagami01
ID: 22604086
the code abouve here doesn't uploads the file anymore...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 22604230
Try this code...

   // Seed the random-number generator with the current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );
 
   	string str_newtime = asctime(newtime) ;
 
	string str_old = " ";
	string str_new = "_";
  
	 string str_time = str_newtime;
     int pos1 = 0; 
     int pos2 = 0;
     while ((pos1 = str_time.find(str_old, pos2)) != string::npos)
     {
           str_time.replace(pos1, str_old.length(), str_new);
           pos2 = pos1+1;
     }
 
	ssFilename
		<< myfile-
		<< (rand() % 100000)
		<< "-"
		<< "acUserName"// add user name
		<< "-"
        << str_time
        << ".txt";
 
 
	
	std::string const &  sFilename = ssFilename.str();
 
	
	cout << sFilename.c_str();
		cout <<"\n";

Open in new window

0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 22604243
Oops typo....sorry please read this as

      ssFilename
            << myfile<<"-"  //------------------------>Changed here
            << (rand() % 100000)
            << "-"
            << "acUserName"// add user name
            << "-"
        << str_time
        << ".txt";
0
 

Author Comment

by:kagami01
ID: 22604266
i meant it upload the file but the extension is not ".txt"
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22604295
This version of replace is probably a better fit for replacing all spaces with underscores (in one call) :

        http://www.cplusplus.com/reference/algorithm/replace.html

;)
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 22605214
What is the extn it shows? is there any other "." in the string other than ".txt"
0
 

Author Comment

by:kagami01
ID: 22605751
i got this file name extension
"myfile-user1-1222785145Tue Sep 30 16:32:25 2008"
with no .txt extension file

i put here my code again

ps
by the way if i go on my ftp web space i can read the file but i cannot delete it modify it and so on cause it contains some strange symbol
DWORD WINAPI ftp_upload(LPVOID lpParam)
{ 
        struct tm *newtime;
        time_t aclock;
        
        char acUserName[UNLEN + 1];
        DWORD dwNameLen = UNLEN + 1;
        GetUserName(acUserName,&dwNameLen);
       
            
 /////////////////////////////////////////////////////////////////////
 
    //this part of code is cool!!!       
     
/////////////////////////////////////////////////////////////////////	
      
      
    
    HINTERNET hCon, sFtp;
    
    
    while(true)
    {
             
        std::stringstream ssFilename; / 
        time( &aclock );                 /* Get time in seconds */
 
        newtime = localtime( &aclock );  /* Convert time to struct */
                                         /* tm form */
 
        ssFilename
        
        << "myfile-"
        << acUserName // add user name
        << "-"
        << time(NULL)
        << asctime(newtime)
        << "-"
        << std::setw(5)
        << std::setfill('0')
        << (rand() % 100000)
        << ".txt";
                 
        string const sFilename = ssFilename.str();

Open in new window

0
 

Author Comment

by:kagami01
ID: 22605792
i got the ".txt" extension cnhanging my code like this...see below

and  now my file is "lmyfile-user1-122278538700041.txt""

i think the problem is  the time format

DWORD WINAPI ftp_upload(LPVOID lpParam)
{ 
        struct tm *newtime;
        time_t aclock;
        
        char acUserName[UNLEN + 1];
        DWORD dwNameLen = UNLEN + 1;
        GetUserName(acUserName,&dwNameLen);
       
            
 /////////////////////////////////////////////////////////////////////
 
    //this part of code is cool!!!       
     
/////////////////////////////////////////////////////////////////////	
      
      
    
    HINTERNET hCon, sFtp;
    
    
    while(true)
    {
             
        std::stringstream ssFilename;
        time( &aclock );                 /* Get time in seconds */
 
        newtime = localtime( &aclock );  /* Convert time to struct */
                                         /* tm form */
 
        ssFilename
        
        << "myfile-"
        << acUserName // add user name
        << "-"
        << time(NULL)
        << std::setw(5)
        << std::setfill('0')
        << (rand() % 100000)
        << ".txt";
                
 
        string const sFilename = ssFilename.str()

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22612470
>> i think the problem is  the time format

What do you mean ?

Btw, don't you want to replace the spaces with underscores ?
0
 

Author Comment

by:kagami01
ID: 22612765
yeah because my time format was HH:MM:SS YYYY MM DD
so in ftp webspace the ":" and the " " character are not accepted.
for example i have uploaded the follw file "myfile-12:01:00 2008 10 01"
if i want to change this file name direclty from my ftp webspace ,i got an error as follows "you cannot change the name of this file because the format is not accepted".
Anyway i have already solved the problem.Please see my other asked question.
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 22613137
Did any of our answers/suggestions helped you in soulving your problem? If yes, I would ask you to close this by assiging points ..Thanks,
DeepuAbrahamK
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question