Question from a C student
Hi guys/gals. I'm a student taking an advanced C course, and I have a question that perhaps you may be able to help me with.
Here's the requirements:
Write a program to open and read an html file which contains a small list of a users favorite links. Prompt user for changes to the links, then substitute the user's new information between the A HREF tags and save to a separate file. I also have to prompt them to change the TITLE info as well.
Here's my question:
What should be the best overall approach. I'm starting by using fgets to read the file line by line, then I call strstr to see if the substring "title" or "a href" appears. Then I attempt to process the strings from there using strtok, etc. I just can't seem to figure out how to make the substitutions and print them out to the new file in an orderly way.
If any of you gurus have any wisdom, please share.
Use Perl... oh wait.. this is a C course... never mind.
I assume you'll not only read in the file one line at a time, but also go through the links the same way? Primpt the user with the link, ask if they want to edit it, move on to the next, etc etc etc ?
You could keep a modified buffer along with the original buffer you read in...
1. Read in the entire filke, line by line, into your first buffer...
2. Start processing the buffer line by line... if its a line that the user shouldn't touch, like a header or something, just copy it directly to the other buffer.. if the user should be offered the chance to modify it, just do the work needed on that one line, THEN copy it to the new file buffer... prcoeed through all the lines...
By doing it this way, you could also offer the user a chance to look at the new and original buffers and ask for confirmation to changes at the end... once they agree to the changes, then overwrite the physical file on the disk.
If you dont care about the confirmation stuff, you could just do all the prcoessing as the file is read in... ie:
Read in a line... send it right to your 'new file buffer' or prompt user for changes... once changes are done, send modified line to buffer.. keep copying lines/prompting the user for changes until EOF on the html file.. once EOF, close the file handle and overwrite the html file with the new buffer.
That will work, but there may be some issues with reading line by line. What if the anchor wraps a line, or two, or three? This programatically is a problem.
Try this. Read the whole file into a string, or big char array, returns and all.
First find "<Title>" and "</title>, there should be only one. Whatever is between the two indexes, output to user kinda sorta like "Current title is: My HTML page. Change to:"
Process the user input on enter. Iffin user just hit enter, keep current title. Set current var holding html accordingly.
Next while loop looking for "href" as you are already doing. When found, save everything upto to "href=" to another local string/char array. From index you found "href", find the ending ">". Hopefully the html is formatted well and the stuff betwixt "href" and the next ">" is the link. printf this formatted kinda like "The current link is:www.cnn.com Change to:" And process the user input on enter. Iffin user just hit enter, keep current link, else use input (validating input is an issue).
Save to same local var. Find next "href". Write everyting from last ending ">" to next "href" index. Do the same as above. If no href is found, save the rest to local
When finished, write local var to file, and done.
Hopefully not too confusing.
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke