view source of html file

This is a discussion on view source of html file within the C++ Programming forums, part of the General Programming Boards category; is there a way to write a program that can open up the source code of an html file?...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    67

    view source of html file

    is there a way to write a program that can open up the source code of an html file?

  2. #2
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    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:
    Code:
    char* buffer=new char[255];
    std::ifstream readfile("index.html");
    while(!eof()) {
      readfile.get(buffer, 255, '\n');
      std::cout<<buffer;
      readfile.close();
    }
    That 'should' print out the code, but I dont think get() will rewrite over itself, so heres idea 2:
    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;
    }
    Not so sure about this one, Im not used to using the string header. Max of 255 lines in the html file.
    Last edited by LloydUzari; 08-05-2004 at 06:28 PM.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ust use a while loop with '!eof()' as its reason and itll keep reading until the end:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    HTML is just plain text so anything like notepad or VIM will open it without any problems.

  4. #4
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    I never said 'feof()', I said 'eof()'. I personally dont see the dif, but oh well

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by LloydUzari
    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.

  6. #6
    Banned
    Join Date
    May 2004
    Posts
    55
    or you can just right click and press "View Source" in internet explorer... doh

  7. #7
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    >>...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

  8. #8
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Noxir
    or you can just right click and press "View Source" in internet explorer... doh
    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.

    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.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    Code:
    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;
    	}
    */
    }
    1. displays a few weird characters
    2. displays nothing
    3. compile error

    note: i changed the code you gave me cuz my compiler doesn't like std:: or string

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    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/

  11. #11
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    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.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    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.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    haha wo that looks a little over my head

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Source file inclusion
    By sh3rpa in forum C++ Programming
    Replies: 7
    Last Post: 10-02-2007, 11:10 AM
  3. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21