You are here:

C++/Very tricky. html - cgi - c++ programming

Advertisement


Question
Hello, this is the worst situation i've been in.
Here it goes.

I am making a web interpreter, and this is a brief version of the code.

#include <iostream.h>
#include <fstream.h>

int main(){
char ch;
cout << "Content-type: text/html\n\n";
ifstream block("newpass.shenu");
while (!block.eof()){
ch = block.get();
if (ch == '\n'){
//do nothing
}
else {
cout << ch;
}
}
block.close();
return 0;
}
test.cpp





[%System%]
SnSkinVersion=1
SnSkinVision=2
Thisdoesntwork=3
DONOTQIUT=4
[%End%]

[%Body%]

[%End%]
newpass.shenu


everything compiles fine and works good.
however, there just happens to be one thing that shouldn't of happened.

the result is this http://www.shenu.net/new/error3.cgi
and when you view source.
the following are in separate lines

SnSkinVersion=1
SnSkinVision=2
Thisdoesntwork=3
DONOTQIUT=4 ";

instead of SnSkinVersion=1 SnSkinVision=2 Thisdoesntwork=3 DONOTQIUT=4 ";

please help me out.


Answer
In traditional DOS, end of line was a combination of two characters CHR(13)+CHR(10). One is the NEW-LINE and the other is LINE-FEED. The input file that you are using, possibly is in traditional DOS format and has two characters at the end of each line. When you use '\n' character, it will just check for NEW-LINE character and it leave the LINE-FEED character intact. That's why when you see the result, the LINE-FEED behaves line new line. You can solve the problem by doing:
if(ch == char(13) || ch == char(10){
  //do nothing
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

Experience

I'm a professional programer in C++ and network/internet programming.

©2016 About.com. All rights reserved.