image upload via c++ cgi

I'm trying to allow image upload to a web server via a web form (aware of the security risks). My code works great for text files but I am getting stuck on eof in image files which I know is not a special character in image files so cannot be used as a delimiter.

I've tried
getline(cin, formstr, '\0');
which, of course, fails at the first \0 in the file. I've tried other delimiters and have moved a little further into the file but never to the end. I think I'm going about this incorrectly with getline.

Maybe I need to get the file in binary, i.e.
ifstream thefile("/path/to/file.jpg", ios::binary);
but I don't have the file name until I receive the data, and I can't use a string variable for filename anyway. Also, I've received all the data I'm going to get at this point.

A push in the right direction would be appreciated, thanks!
Hi there,

I'm no expert on this, but perhaps you could do something like:

1
2
3
4
5
6
7
8
9
std::string image;
std::string tmp;
while (cin >> tmp)
{
    image.append(tmp);
}

//this might strip whitespaces though
//you could use the same technque using getline 


That should load the whole image into string "image".

Also, I believe it's possible to give cin.read http://cplusplus.com/reference/istream/istream/read/ a block size parameter to read out, so that may be an option too.

Hope that helps.

All the best,
NwN
Last edited on
but I don't have the file name until I receive the data, and I can't use a string variable for filename anyway. Also, I've received all the data I'm going to get at this point.

If you use a C++11 compiler you can use a std::string variable for the file name. Otherwise you can use a std::string by using the c_str() member function to convert the string to a constant C-string.

Also you should probably be using binary mode for the file and using the read() function to read a block of data or use get() to read the file character by character until you reach the end of file (eof()).
http://www.cplusplus.com/reference/istream/istream/get/

You will however need a buffer that is large enough to hold this information. You can determine the size of your file by using the seekg() and and tellg() functions.
I tried a lot of different methods after viewing both of your suggestions. Most methods failed on finding eof in the image file or corrupted the data in some way.

NwN,this does indeed strip whitespace so would corrupt file data:
1
2
3
4
while (cin >> tmp)
{
    image.append(tmp);
}

But it gave me more data than any of my previous attempts.

I eventually used the following solution after jlb's suggestion to
use get() to read the file character by character until you reach the end of file (eof())
:
1
2
3
4
5
6
7
8
9
10
	char c;

	system("touch /path/to/tempimgfile");
	ofstream tempfile;
	tempfile.open("/path/to/tempimgfile");

	while(cin.get(c))
	{ tempfile << c; }

	tempfile.close();


That code leaves everything intact (tested with jpg and png files). Now I just need to clean up the file of the other form data and copy it to a properly named file with a suitable extension.

Looking at it now it seems so simple, but it gave me _hours_ of grief! Thank you both for your assistance. cplusplus.com is awesome!
Topic archived. No new replies allowed.