250 likes | 430 Views
CS221. File Output Using Special Formats. What is a File?. A file is a collection of information The type of information in the file can differ image, sound, video, text, web page, etc… Files are created, modified, and read by programs
E N D
CS221 File Output Using Special Formats
What is a File? • A file is a collection of information • The type of information in the file can differ • image, sound, video, text, web page, etc… • Files are created, modified, and read by programs • File extensions help the user AND the computer determine what type of information is in the file and what program is most suitable to use that file • jpg, mp3, mpeg, txt, html, doc, xls, ppt, …
File Formats • You can write a C++ program which can read and write files using file streams • It is possible for your C++ program to read and write files which have special formats IF the programmer is aware of the format… • Thus, if you know the format of an HTML file, you can write a C++ program which will read OR create/write a web page!
HTML and CSV • These are special file formats which are based on plain text • HTML is the format of Web Pages • CSV is a format which can be used by MS Excel • We will learn how to make our C++ programs create files using these formats
MS Excel in Brief • Microsoft Excel is a Spreadsheet Program • The spreadsheet consists of a grid of rows and columns • rows are numbered, columns are lettered • When a row and column meet, there is a cell • cells are identified by their row and column • A1, E5, C13, etc… • Cells can contain data (text or numeric) or formulas which act upon that data
MS Excel in Brief • Useful engineering aspects of Excel • store and organize a moderate amount of data • make calculations on that data • make graphs to analyze data
Graphing in Excel • Store data in columns with labels at the top • Select (click+drag) the data series you want to graph • Click the graph wizard button • follow simple directions to creat the graph • you can make your graph “fancy” • axis labels, graph label, key, etc…
CSV Files • CSV stands for “comma separated value” • CSV is a special file format • Microsoft Excel can recognize and open CSV files • Your C++ program can use a file output stream to create CSV files
CSV Files • In a CSV file, Excel will read the text in the following way… • each comma encountered in the file will move Excel to the next cell in the current row • each new line character (endl) encountered in the file will move Excel to the next row
Using File Output with CSV • You can output from your C++ code to a csv file, then open that output file up in Excel and graph, calculate, etc… (as long as the format is correct) • Code to produce the preceding example ofstream fileOut; fileOut.open(“test.csv”); fileOut << “Time,Velocity,Acceleration” <<endl; fileOut << 1 << “,” << 2 << “,” << 3 << endl; fileOut << 4 << “,” << 5 << “,” << 6 << endl; fileOut << 7 << “,” << 8 << “,” << 9 << endl; fileOut << “,,” << 10 << endl;
Web Browsers in Brief • Web Browsers are Special Programs which interpret Web Pages and display the results graphically • Explorer, FireFox, etc… • Web Browsers give users the ability to access information which is stored in remote places and see it on their computer quickly and efficiently
Web Browsers in Brief • Useful engineering aspects of Web Browsers • make data available to colleagues/the public • present data to non-engineers in a familiar format
HTML Files • HTML stands for “HyperText Markup Language” • HTML is a special file format • Web Browsers can recognize and open HTML files • Your C++ program can use a file output stream to create HTML files
HTML Files • In an HTML file, the browser will read the text and produce a web page by interpreting “tags” • each HTML tag tells the browser to do something different with the data it is reading • we will learn a VERY SMALL set of HTML tags • for a complete list of HTML tags, see this link http://www.w3schools.com/tags/
More About Tags • Each HTML tag is enclosed in angle braces <tag> • Many HTML tags must be accompanied by a closing/ending tag, which looks the same as its opening/beginning tag, but is marked by a slash </tag> • Think of a tag set as a set of bookends, anything between the opening and closing tag is described by the tag set.
Some Important Tags • <html> - marks the beginning and end of the HTML file • <title> - tells the browser what to display as the page’s title • <body> - marks the beginning and end of the web page’s body (where the contents to be displayed are located
Some More Tags • <h1> - tells the browser that this text is a heading (can also have h2 through h6 for smaller sized headings) • <b> <i> <u> - tells the browser that this area of text is to be bold, underlined, and/or italic respectively • <p> - begin a new paragraph in the text
TableTags • <table border=“1”> - tells the browser to begin a table (if border is 1, show the border, if border is 0 don’t show it) • <tr> - start a new row in the table • <td> - start a new data entry (cell) in the table
Using File Output with HTML • You can output from your C++ code to an html file, then open that output file up in a standard web browser as long as the format is correct.
Using File Output with HTML ofstream fileOut; //declare output file stream fileOut.open(“test.html”); //open file, NOTE extension fileOut << “<html>” <<endl; //begin html page //output title tag fileOut << “<title>Testing Results</title>” << endl; //output heading fileOut << “<h1>Results of Test Run 1</h1>” << endl; fileOut << “<body>” << endl; //begin page body //first paragraph fileOut << “<p>These are the test results from “; fileOut << “<b><u>Test Run 1.</b></u>” << endl; //continued on next slide…
Code Continued… //begin a table with visible border fileOut << “<table border=\"1\">” << endl; //output first table row (table header) fileOut<<“<tr>”<<“<td>Min”<<“<td>Max”<<“<td>Reading”<<endl; //output subsequent table rows with data in them fileOut<<“<tr>”<<“<td>0”<<“<td>20”<<“<td>15”<<endl; fileOut<<“<tr>”<<“<td>0”<<“<td>20”<<“<td>10”<<endl; fileOut<< “</table>” << endl; //end the table fileOut<< “</body>” << endl; //end page body fileOut<< “</html>” << endl; //end html page