is there a way to write a program that can open up the source code of an html file?
is there a way to write a program that can open up the source code of an html file?
Try reading it like normal, using the 'get()' subfunc.
Heres how to use [fstream].get():
'get([variable], [max # of chars], [stop reading, '\n' for end of line])'
Just use a while loop with '!eof()' as its reason and itll keep reading until the end:
That 'should' print out the code, but I dont think get() will rewrite over itself, so heres idea 2:Code:char* buffer=new char[255]; std::ifstream readfile("index.html"); while(!eof()) { readfile.get(buffer, 255, '\n'); std::cout<<buffer; readfile.close(); }
Not so sure about this one, Im not used to using the string header. Max of 255 lines in the html file.Code:std::string line[255]; int read, display; std::ifstream readfile("index.html"); for(read=0; read<255; read++) { readfile.get(line[read], 255, '\n'); } readfile.close(); for(display=0; display<255;read++) { std::cout<<line[display]<<std::endl; }
Last edited by LloydUzari; 08-05-2004 at 06:28 PM.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351ust use a while loop with '!eof()' as its reason and itll keep reading until the end:
HTML is just plain text so anything like notepad or VIM will open it without any problems.
I never said 'feof()', I said 'eof()'. I personally dont see the dif, but oh well
The FAQ applies to both feof and eof. The loop goes one too many times because eof is set after an attempt to read fails.Originally Posted by LloydUzari
or you can just right click and press "View Source" in internet explorer... doh
>>...both feof and eof.
Whoops, didnt notice it was both.
>>..."viewsource" in internet explorer... doh
Thats true...
Oh and, both my codes are screwed, and since it aint really my coding, I aint gonna work to fix it. Should atleast give an idea
Unless you wanted to write an HTML parser that checks for HTML 4.01 validated code, in which case that doesn't do you a whole hell of a lot of good.Originally Posted by Noxir
As for the question.. just go through the normal process of reading from a file. It'll work.. HTML code is just text to C++'s point of view.
1. displays a few weird charactersCode:int main() { //Method 1 /* char test[15]; ifstream b_file("http://www.yahoo.com/index.html"); b_file >> test; b_file.close(); ofstream a_file("test1.txt"); a_file << test; a_file.close(); */ //Method 2 /* char* buffer=new char[255]; ifstream readfile("http://www.yaho.com/index.html"); for (int x = 0; x < 100; x++) { readfile.get(buffer, 255, '\n'); cout<<buffer; readfile.close(); } */ // Method 3 /* char line[255]; int read, display; ifstream readfile("http://www.yahoo.com/index.html"); for(read=0; read<255; read++) { readfile.get(line[read], 255, '\n'); } readfile.close(); for(display=0; display<255;read++) { cout<<line[display]<<endl; } */ }
2. displays nothing
3. compile error
note: i changed the code you gave me cuz my compiler doesn't like std:: or string
Your trying to open a url. If you want to open a html file save it to your hard drive first. If you want to know how to open a url that is different because you have to connect to the http server.
"Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
-- Cowper
Operating Systems=Slackware Linux 9.1,Windows 98/Xp
Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)
You may teach a person from now until doom's day, but that person will only know what he learns himself.
Now I know what doesn't work.
A problem is understood by solving it, not by pondering it.
For a bit of humor check out xkcd web comic http://xkcd.com/235/
What version of Dev-C++ are you using? Because if I were to not put the std:: or atleast declar with using, my Dev-C++ gets angry... I have funny feelng your using older version.
Oh and, I already said my code was screwed
The second one wont rewrite over the variable, so it will print the first line over and over.(try making index.html with notepad and try it)
The third one likes to be annoying. It seems that 'readfile.get' wont copy to a string variable, inother words you would not only need multiple variables, but would need a ton of loops to change it over. Ill try it later if I feel like it Busy with my stuff.
Last edited by LloydUzari; 08-06-2004 at 07:51 AM.
sorry. yes i'm trying to open a url. meant that instead of html file.
i'm using Digital Mars. it's very old and the only free compiler that works on my computer.
Oh well then you have a much bigger set of problems:
1) Have to make a socket connection to the webserver
2) Request the file
3) Recieve the file into a local copy
4) Open and print the local copy
Step 1 - 3 require that you follow the HTTP protocol
haha wo that looks a little over my head
alright after a month of thinking and other things i've decided to try and do this program again. how would i go about simply viewing the source of http://www.yahoo.com/index.html and being able to change that address to anything?
in other words..how do i do this:
"Oh well then you have a much bigger set of problems:
1) Have to make a socket connection to the webserver
2) Request the file
3) Recieve the file into a local copy
4) Open and print the local copy
Step 1 - 3 require that you follow the HTTP protocol"
Last edited by bballzone; 09-04-2004 at 04:13 PM.