By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,927 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,927 IT Pros & Developers. It's quick & easy.

get(buffer, size) and CGI

P: n/a
Hi, I'm using CGI with MS IIS and Visual Studio 2003.

My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").

My problem is, it cuts off one character at the end. And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?
Dec 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris wrote:
Hi, I'm using CGI with MS IIS and Visual Studio 2003.

My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").

My problem is, it cuts off one character at the end.
cin.get(foo, n) will extract up to n-1 characters, so that is the expected
behavior. (The idea is that if you allocate n characters, you just continue
passing n around instead of having to do arithmetic on it.)
And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.
Hmm... that doesn't sound right. Just tested with Apache (don't have IIS),
and it worked fine. If you really need to get that method working for some
reason, try posting to an IIS- (or at least CGI-) specific group.
Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?
Try using std::string.

#incude <iostream>
#include <string>

int main() {
using namespace std;

cout << "Content-type: text/plain\r\n";
cout << "\r\n";

string post_data;
getline(cin, post_data);

// parse post_data, etc

return 0;
}

You don't even need to worry about Content-length.

--
Josh
Dec 15 '07 #2

P: n/a
On Dec 15, 9:01 am, Josh Sebastian <sebas...@gmail.comwrote:
Chris wrote:
Hi, I'm using CGI with MS IIS and Visual Studio 2003.
My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").
My problem is, it cuts off one character at the end.

cin.get(foo, n) will extract up to n-1 characters, so that is the expected
behavior. (The idea is that if you allocate n characters, you just continue
passing n around instead of having to do arithmetic on it.)
And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Hmm... that doesn't sound right. Just tested with Apache (don't have IIS),
and it worked fine. If you really need to get that method working for some
reason, try posting to an IIS- (or at least CGI-) specific group.
Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?

Try using std::string.

#incude <iostream>
#include <string>

int main() {
using namespace std;

cout << "Content-type: text/plain\r\n";
cout << "\r\n";

string post_data;
getline(cin, post_data);

// parse post_data, etc

return 0;
}

You don't even need to worry about Content-length.

--
Josh
Well, I will try posting to an IIS group, but I suspected it was a
problem with my C++ compiler or my C++ code. The problem with using
getline is it doesn't fit the CGI standard, and when you get into
using multipart data, it could contain a text file, which would have
newlines in it already.
Dec 15 '07 #3

P: n/a
On Dec 15, 9:01 am, Josh Sebastian <sebas...@gmail.comwrote:
Chris wrote:
Hi, I'm using CGI with MS IIS and Visual Studio 2003.
My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").
My problem is, it cuts off one character at the end.

cin.get(foo, n) will extract up to n-1 characters, so that is the expected
behavior. (The idea is that if you allocate n characters, you just continue
passing n around instead of having to do arithmetic on it.)
And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Hmm... that doesn't sound right. Just tested with Apache (don't have IIS),
and it worked fine. If you really need to get that method working for some
reason, try posting to an IIS- (or at least CGI-) specific group.
Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?

Try using std::string.

#incude <iostream>
#include <string>

int main() {
using namespace std;

cout << "Content-type: text/plain\r\n";
cout << "\r\n";

string post_data;
getline(cin, post_data);

// parse post_data, etc

return 0;
}

You don't even need to worry about Content-length.

--
Josh
I forgot to add, how would I go about viewing the state of cin (kinda
like extended cin.peek() and such)? You know, what it's got left in
it's buffer? Looking at the MS library source, it looks like cin.get
just keeps reading from cin.rdbuf()
Dec 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.