is there a way to write a program that can open up the source code of an html file?
Printable View
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;
}
http://faq.cprogramming.com/cgi-bin/...&id=1043284351Quote:
ust 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 :rolleyes:
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.Quote:
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 :p
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.Quote:
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.
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 :p
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 :rolleyes: Busy with my stuff.
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 :eek:
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"
Here's the plain english steps for you.
1.) Create a method for the person to be able to put in the URL. Either GUI or command-line.
2.) Convert the input string into the domain name, and the document to pull from the server.
3.) Convert the URL from the domain name to the IP address.
4.) Create a Winsock connection to the newfound IP address.
5.) Send an HTTP "GET" request to the server using the document part of the user input.
6.) Pipe the output to the screen or to a text file.
I probally forgot a step, but that's essentially what you need to do.
hmm...alright
that puts it in english for me but could you direct me to a tutorial? or what general topic that is under so i can run a search for it?
Here's some extra help: Sample Winsock code!
This should help get you started as to what you need to do. You might also want to look into send() and recv(), as well as converting the domain name to IP.Code:// Port Opener v1.1
// By: Steven Merrick
// For educational use only.
// Includes
#include <winsock.h>
#include <iostream>
// Variables
int port;
// Functions
int Winsock();
// Code
int main()
{
std::cout << "Input port to open: ";
std::cin >> port;
if (port > 65535) // Sanity Check.
{
std::cout << "\nInvalid port number, quitting.\n" << std::endl;
return(0);
}
Winsock(); // Calls Winsock().
std::cout << "\nPress any key to quit listening." << std::endl; // Program waits in "listening" state here until keypress.
std::cin.ignore(2, '\n');
WSACleanup(); // Cleans up and quits using Winsock. Do NOT forget this.
return(0);
}
int Winsock() // Calls the Winsock initalization and opens the port to "listening" state.
{
WSADATA WsaDat; // Not entirely sure, but is required. Appears to just hold our connection state.
SOCKET Socket; // What do we want to call our socket?
SOCKADDR_IN SockAddr; // Holds the data for our socket: type (sin_family), port (sin_port), and address (sin_addr.s_addr).
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = htons(port); // Opens the specified port.
SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Statically set address to bind to, localhost.
memset(&(SockAddr.sin_zero), '\0', 8); // Sets the remainder of the SockAddr struct to 0.
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0) // If the Winsock v1.1 startup fails..
{
std::cout << "WSA Initialization failed." << std::endl;
}
if ((Socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) // If the socket cannot be opened..
{
std::cout << "Socket creation failed." << std::endl;
}
if (bind(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR) // If the socket cannot be bound to the port..
{
std::cout << "Attempt to bind failed." << std::endl;
}
listen(Socket, 1); // Sets the socket to listening state on the specified port number.
return(0);
}
But this should help get you started. :)