Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

ftp & changing textfile name

Hello experts,
my problem is i mae a ftl uploading file but i have the problem cause the name of my text file it's static
so for everyn uploading it overwrite the last file with the new file content.
how can i change the text file name for every uploading?thank you
check my code pls
bool InviaLog()
{
    HINTERNET connessione, sessione_ftp;
 
    connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
     if(!connessione) {
            logga("\n*** connection problem***\n");
            return FALSE;
        }
     sessione_ftp = InternetConnect(connessione,"ftp.my spaceweb",INTERNET_DEFAULT_FTP_PORT, "myUserID","mypass", INTERNET_SERVICE_FTP, 0,0 );
      if(!sessione_ftp) {
            logga("\n*** starting FTP session failed ***\n");
            return FALSE;
        }
      if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){
             logga("\n*** file sending ended ***\n");
             return FALSE;
        }
 
     InternetCloseHandle(sessione_ftp);
    InternetCloseHandle(connessione);
   DeleteFile(log_dir);  // if the file is uploade i delete the file
  StartLog();  // i delete the file
 return TRUE;
}

Open in new window

0
aitsu01
Asked:
aitsu01
  • 14
  • 11
  • 2
1 Solution
 
abithCommented:
file name can be passed as a parameter,
or file name can be created with time stamp, just like myfile20072611hhmmss...
0
 
aitsu01Author Commented:
hello abith,
ok i am agree for the file name can be created with the time stamp but how can i code it with you
suggestions??Thank you
0
 
evilrixSenior Software Engineer (Avast)Commented:
You'll have to be a little smarter than that I'm afraid... what if 2 or more people connect at the same time? The more users you have the more chance you have of a collision. Consider using time date as abtih has suggested but with an additional random element, such as the connecting clients IP address or MAC address.
0
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.

 
aitsu01Author Commented:
hello elverix ,
this is a good idea for example with the user name added too.
but my problem is how to append the time stamp  (or in youca case the IP)to my text file before the uploading.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Are you asking how to acquire these data or how to append them to a string? I'm not quite sure what it is you need help with!
0
 
aitsu01Author Commented:
helo evilrix,
i meant both.
0
 
abithCommented:
refer this link http://www.thescripts.com/forum/thread432803.html
it gives to take timestamp. use sprinf to convert as a string.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Hmm. Trying to get IP-ADDR from IntenetConnect() isn't simple and would necessitate a lot of work. This; however, should be pretty safe. It won't guarantee no collisions but the chances are very slim. You'd have to have 2 people generate the same random number (0 - 99999) within a milli-second of each other! It's a working example (it uses STL -- all C++ compilers support STL) that you'll need to stitch into your own code (including the headers!). It should be enough for you to figure out what you need to do.

-Rx.
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <string>
 
void FtpPutFile(char const * pFilename)
{
	// Dummy ftp function -- pretend it is the Mricrosoft one :)
}
 
int InviaLog ()
{
	//////////////////////////////////////////////////////////////////////////
	// Insert this code into yours
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
	return 1;
}
 
int main()
{
	// Put this line in you main() function to prime the random seed generator
	srand (static_cast<int>(time(NULL)));
 
	InviaLog();
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> within a milli-second of each other
Sorry, typo -- that should have said "within a second of each other" -- I got a little bit over ambitious with the value held by time_t :)
0
 
aitsu01Author Commented:
hello evilrix,
why the procedure invialog() is INT now?i got soem error and i could not find the solution
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> why the procedure invialog() is INT now?

Oh, sorry -- not for any specific reason... you can ignore that function return type, it was purely just used as an example... the only part you are specifically interested in is the part inside it, which needs to be
transplanted into your function InviaLog().

>> i got soem error and i could not find the solution
What error?
0
 
evilrixSenior Software Engineer (Avast)Commented:

#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <string>
 
void FtpPutFile(char const * pFilename)
{
	// Dummy ftp function -- pretend it is the Mricrosoft one :)
}
 
bool InviaLog ()
{
	//////////////////////////////////////////////////////////////////////////
	// Insert this code into yours
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
	return TRUE;
}
 
int main()
{
	// Put this line in you main() function to prime the random seed generator
	srand (static_cast<int>(time(NULL)));
 
	InviaLog();
}

Open in new window

0
 
aitsu01Author Commented:
Hello evilrix,
thank you for your supporting.
well i have been busy recenlty anyway sooon i will try your code
and if possible i will attach here the error logs.
talk to you soon
0
 
aitsu01Author Commented:
Hello,
well the file has been compiled in .exe but once launche noithing happen
i mena on my ftp server there not uploaded text file i just wonder why??
maybe the myfile.txt name has been changed with the new code??
0
 
evilrixSenior Software Engineer (Avast)Commented:
So, have you tried stepping into the code int he debugger to see what's happening?
0
 
aitsu01Author Commented:
Hello evilrix,
well the debugging was ok i compiled the exe without any error
but it program cannot send the file anymore may i forgot soemthing in the code





i will attach here my old code for the ftp connection



bool InviaLog()
{
    HINTERNET connessione, sessione_ftp;
 
    connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
     if(!connessione) {
            logga("\n*** connection problem***\n");
            return FALSE;
        }
     sessione_ftp = InternetConnect(connessione,"ftp.my spaceweb",INTERNET_DEFAULT_FTP_PORT, "myUserID","mypass", INTERNET_SERVICE_FTP, 0,0 );
      if(!sessione_ftp) {
            logga("\n*** starting FTP session failed ***\n");
            return FALSE;
        }
      if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){
             logga("\n*** file sending ended ***\n");
             return FALSE;
        }
 
     InternetCloseHandle(sessione_ftp);
    InternetCloseHandle(connessione);
   DeleteFile(log_dir);  // if the file is uploade i delete the file
  StartLog();  // i delete the file
 return TRUE;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> i will attach here my old code for the ftp connection

Um, posting current code might be more helpful :)
0
 
aitsu01Author Commented:
oops sorry evilrix,
keep in mind this code by now

 
 
 
void FtpPutFile(char const * pFilename)
{
        // Dummy ftp function -- pretend it is the Mricrosoft one :)
}
 
 
bool InviaLog()
{
     std::stringstream ssFilename;
 
        ssFilename
                << "myfile-"
                << time(NULL)
                << "-"
                << std::setw(5)
                << std::setfill('0')
                << (rand() % 100000)
                << ".txt";
 
        std::string const & sFilename = ssFilename.str();
        //////////////////////////////////////////////////////////////////////////
 
        // Usage
        FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
        return TRUE;
 
    HINTERNET connessione, sessione_ftp;
 
    connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
     if(!connessione) {
            logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
            return FALSE;
        }
     sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
      if(!sessione_ftp) {
            logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
            return FALSE;
        }
      if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){
             logga("\n*** file sending ended ***\n");
             return FALSE;
 
        }
 
     InternetCloseHandle(sessione_ftp);
    InternetCloseHandle(connessione);
   DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
  StartLog();  // Cancellato il file, riscrivo lo startup log
 return TRUE;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
You were calling the dummy FtpPutFile and returning immediately.
#include "stdafx.h"
 
bool InviaLog()
{
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	HINTERNET connessione, sessione_ftp;
 
	connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	if(!connessione) {
		logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
		return FALSE;
	}
	sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
	if(!sessione_ftp) {
		logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
		return FALSE;
	}
	if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){
		logga("\n*** file sending ended ***\n");
		return FALSE;
 
	}
 
	InternetCloseHandle(sessione_ftp);
	InternetCloseHandle(connessione);
	DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
	StartLog();  // Cancellato il file, riscrivo lo startup log
	return TRUE;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
I removed this...

        //////////////////////////////////////////////////////////////////////////
 
        // Usage
        FtpPutFile(sFilename.c_str()); // Dummy to demo usage
 
        return TRUE;


and change this...


if(!FtpPutFile(sessione_ftp, log_dir, "myfile.txt", INTERNET_FLAG_TRANSFER_BINARY, 0)){


to this...

if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){


Does that make sense?
0
 
aitsu01Author Commented:
I got this error

C stdafx.h: No such file or directory.

what do you mean for #include "stdafx.h"??
0
 
evilrixSenior Software Engineer (Avast)Commented:
Oops, sorry -- delete that... it's a precompiled header -- you don't need it!
bool InviaLog()
{
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	HINTERNET connessione, sessione_ftp;
 
	connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	if(!connessione) {
		logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
		return FALSE;
	}
	sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
	if(!sessione_ftp) {
		logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
		return FALSE;
	}
	if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){
		logga("\n*** file sending ended ***\n");
		return FALSE;
 
	}
 
	InternetCloseHandle(sessione_ftp);
	InternetCloseHandle(connessione);
	DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
	StartLog();  // Cancellato il file, riscrivo lo startup log
	return TRUE;
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
BTW: You will need to include the headers...

#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

I've just noticed you didn't include them in your post.
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
 
bool InviaLog()
{
	std::stringstream ssFilename;
 
	ssFilename
		<< "myfile-"
		<< time(NULL)
		<< "-"
		<< std::setw(5)
		<< std::setfill('0')
		<< (rand() % 100000)
		<< ".txt";
 
	std::string const & sFilename = ssFilename.str();
	//////////////////////////////////////////////////////////////////////////
 
	// Usage
	HINTERNET connessione, sessione_ftp;
 
	connessione = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	if(!connessione) {
		logga("\n*** INIZIO CONNESSIONE FALLITO ***\n");
		return FALSE;
	}
	sessione_ftp = InternetConnect(connessione,"mftpspace.com",INTERNET_DEFAULT_FTP_PORT, "adult12","mypassword", INTERNET_SERVICE_FTP, 0,0 );
	if(!sessione_ftp) {
		logga("\n*** INIZIO SESSIONE FTP FALLITA ***\n");
		return FALSE;
	}
	if(!FtpPutFile(sessione_ftp, log_dir, sFilename.c_str(), INTERNET_FLAG_TRANSFER_BINARY, 0)){
		logga("\n*** file sending ended ***\n");
		return FALSE;
 
	}
 
	InternetCloseHandle(sessione_ftp);
	InternetCloseHandle(connessione);
	DeleteFile(log_dir);  // SE UPPATO CANCELLO IL LOG
	StartLog();  // Cancellato il file, riscrivo lo startup log
	return TRUE;
}

Open in new window

0
 
aitsu01Author Commented:
now i just got 6 error
somme connession error i think:


  [Linker error] undefined reference to `InternetOpenA@20'
  [Linker error] undefined reference to `InternetConnectA@32'
  [Linker error] undefined reference to `FtpPutFileA@20'
  [Linker error] undefined reference to `InternetCloseHandle@4'
  [Linker error] undefined reference to `InternetCloseHandle@4'
  ld returned 1 exit status
0
 
evilrixSenior Software Engineer (Avast)Commented:
Well, I'm guessing you haven't linked against wininet.lib -- you must have removed or modified something as you say it worked previously! The code I provided was meant to be stitched into your existing code, not a replacement for it. You still need to ensure all your previous prerequisites are met, like including windows.h and linking to the same libraries.

InternetOpen(), InternetConnect(), FtpPutFile(), InternetCloseHandle(): -

Header Declared in Wininet.h.
Library Use Wininet.lib.
DLL Requires Wininet.dll.

http://msdn2.microsoft.com/en-us/library/aa385483.aspx
 
0
 
aitsu01Author Commented:
Hello Evilrix,
i have a good news.he project is working now!
as you right said ,i missed some dll ,so after that i fixed it everything working good,thank you so much!
0
 
evilrixSenior Software Engineer (Avast)Commented:
You are very welcome my friend :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 14
  • 11
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now