Sending a file to an FTP

Started by
7 comments, last by DividedByZero 17 years, 2 months ago
Hello all, I'm wondering how to send a file as fast as possible to an FTP. I am sending line by line at the moment, but I don't think it is the faster way because it can take up to 10 seconds, when my FTP programm seems to goes twice faster. I am sending .txt, .html and .php file. The size of the .txt can expand infinitely. Now lets see the code:

//OPENING A FILE AND READING LINE BY LINE
void sendfile(const string& filename)
{
	ifstream myfile(filename.c_str());
	if (myfile) {

		string line;
		while (getline(myfile, line)) {
			sendby(line, ftpsend); //SEND ONE LINE
		}

	}
}


//SENDING A LINE
void sendby(const string& e, const int& socketWasActive)
{
	unsigned size_sent = 0;
	unsigned total_size = (unsigned) e.size();
	while (size_sent < total_size)
		size_sent += send(socketWasActive, e.c_str() + size_sent, (unsigned) e.size() + size_sent, 0);

	cout << e << endl;
}


Thanks for any kind of help.
Advertisement
Treat the file as binary. Read the entire thing into memory, and call send() on the entire data, walking the pointer forward each time it returns until you reach the end. This means you don't have to wait for the disk between each send().

enum Bool { True, False, FileNotFound };
Any advice on the format I should use please ?! Would a char array be fine to hold a whole 1 mb text file please ?

[Edited by - Jarry on February 14, 2007 12:21:45 PM]
Ok I did that:

void sendfile(const string& filename, const int& ftpsend){	ifstream myfile(filename.c_str(), ios::binary | ios::ate);	if (myfile) {		ifstream::pos_type size = myfile.tellg();		char *file = new char [size];		//STORING THE FILE		myfile.seekg(0, ios::beg);		myfile.read(file, size);		myfile.close();		sendby(file, ftpsend);		delete[] file;	}}void sendby(const char * e, const int& socketWasActive){	int size_sent = 0;	int total_size = (int) strlen(e);	while (size_sent < total_size)		size_sent += send(socketWasActive, e + size_sent, (int) strlen(e + size_sent), 0);	cout << e << endl;}



The problem is that the function adds weird chars: "ýýýý««««««««îþîþ" to the file. Might be because the char pointer is too big, and the null char appends too late. Any idea please ? Otherwise is the function good please ?

[Edited by - Jarry on February 14, 2007 1:33:06 PM]
sendby should take the data pointer and the size, rather than trying to measure the length using strlen. If you ever want to send data containing 0, sendby wouldn't send the right amount otherwise.
enum Bool { True, False, FileNotFound };
I'm not sure, sorry, if I have to remove both strlen or only the first one. So far I have removed the first one, but still it seems it adds extra char when sending, must be due to the file pointer.

void sendby(const char * file, const int& socket, const int& size){	int size_sent = 0;	while (size_sent < size)		size_sent += send(socket, file + size_sent, (int) strlen(file + size_sent), 0);	cout << file << endl;}void sendfile(const string& filename, const int& ftpsend){	ifstream myfile(filename.c_str(), ios::binary | ios::in | ios::ate);	if (myfile) {		ifstream::pos_type size = myfile.tellg();		char *file = new char [size];		//STORING THE FILE		myfile.seekg(0, ios::beg);		myfile.read(file, size);		myfile.close();		sendby(file, ftpsend, size);		delete[] file;	}}


I have no idea how to do if I must not use strlen in the send() function tough, and I have no idea how to remove those unwanted chars.

Thank you.
Wouldn't you need to do something like this?

char *file = new char [size+1];

Then I would initialize the 'file' buffer to zero.

As the file you are trying to send might not finish with a zero.
Thanks !

Now it works. Great ! :-)
No problem.
Glad I could be of some help. :)

This topic is closed to new replies.

Advertisement