Link to home
Start Free TrialLog in
Avatar of aitsu01
aitsu01

asked on

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

Avatar of abith
abith
Flag of India image

file name can be passed as a parameter,
or file name can be created with time stamp, just like myfile20072611hhmmss...
Avatar of aitsu01
aitsu01

ASKER

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
Avatar of evilrix
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.
Avatar of aitsu01

ASKER

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.
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!
Avatar of aitsu01

ASKER

helo evilrix,
i meant both.
refer this link http://www.thescripts.com/forum/thread432803.html
it gives to take timestamp. use sprinf to convert as a string.
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

>> 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 :)
Avatar of aitsu01

ASKER

hello evilrix,
why the procedure invialog() is INT now?i got soem error and i could not find the solution
>> 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?

#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

Avatar of aitsu01

ASKER

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
Avatar of aitsu01

ASKER

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??
So, have you tried stepping into the code int he debugger to see what's happening?
Avatar of aitsu01

ASKER

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

>> i will attach here my old code for the ftp connection

Um, posting current code might be more helpful :)
Avatar of aitsu01

ASKER

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

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

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?
Avatar of aitsu01

ASKER

I got this error

C stdafx.h: No such file or directory.

what do you mean for #include "stdafx.h"??
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of aitsu01

ASKER

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
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
 
Avatar of aitsu01

ASKER

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!
You are very welcome my friend :)