{"id":7175,"date":"2014-05-25T05:03:37","date_gmt":"2014-05-24T19:03:37","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7175"},"modified":"2014-05-25T05:03:37","modified_gmt":"2014-05-24T19:03:37","slug":"c-file-write-and-read-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-file-write-and-read-primer-tutorial\/","title":{"rendered":"C File Write and Read Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/File\/C_File_String.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C File Write and Read Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/C\/File\/C_File_String.jpg\" title=\"C File Write and Read Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">C File Write and Read Primer Tutorial<\/p><\/div>\n<p>Today, inspired by a Yahoo Answers <a target=_blank title='Yahoo Answers question' href='https:\/\/au.answers.yahoo.com\/question\/index?qid=20140406092537AAuJO3Z'>question<\/a> (thanks) we take a look at some C code dealing with files (both writing, and then rereading) and interactive input and <a target=_blank title='C string functions' href='http:\/\/faculty.edcc.edu\/paul.bladek\/c_string_functions.htm'>string functionality<\/a> in the downloadable C programming code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/C\/File\/filestuff.c_GETME\" title=\"filestuff.c\">filestuff.c<\/a>:<\/p>\n<ul>\n<li>Note use of fgets(inname, 300, stdin); rather than gets(inname); to avoid possibility of string running out of bounds.<\/li>\n<li>Note use of <a target=_blank title='strtok information' href='http:\/\/www.cplusplus.com\/reference\/cstring\/strtok\/'>strtok<\/a> for delimitation login like explode in PHP or split in Javascript.<\/li>\n<li>Note the use of sscanf format string &#8220;%[^n]s&#8221; for &#8220;up to first line break but not including it&#8221; &#8230; rather than risk a problem with filenames with spaces in them and to exclude a possible line break in the string.<\/li>\n<li>Note algorithm for Moving Average &#8230; avgrad = (avgrad * ((double)(i &#8211; 1)) + tavgrad) \/ (double)i;<\/li>\n<\/ul>\n<p>There are other file opening functions available other than the <a target=_blank title='C fopen function' href='http:\/\/www.cplusplus.com\/reference\/cstdio\/fopen\/'>fopen<\/a> we use here &#8230; see the open function and its associated functions written about <a target=_blank title='open function in C' href='http:\/\/gd.tuwien.ac.at\/languages\/c\/programming-bbrown\/c_075.htm'>here<\/a>.<\/p>\n<p>C how you go &#8230; chortle, chortle!   C if it interests U &#8230; weaker chortle, chortle!   C if it gets really annoying &#8230; <span style='font-size:6px;'>practically non-existant chortle, <\/span><span style='font-size:4px;'>chortle.<\/span><\/p>\n<hr \/>\n<p id='cfcpt'>Previous tutorial of relevance <a target=_blank title='C++ Formatting cout Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=6632'>C++ Formatting cout Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/cout_formatting\/cout_formatting.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C++ Formatting cout Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/cout_formatting\/cout_formatting.jpg\" title=\"C++ Formatting cout Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">C++ Formatting cout Primer Tutorial<\/p><\/div>\n<p>C++ uses iostream.h methods to format command line output.   Programmers with a background in C may think the iostream.h &#8220;<a target=_blank title='cout' href='http:\/\/www.cplusplus.com\/reference\/iostream\/cout'>cout<\/a>&#8221; stream output methods are a little harder to use (compared to C&#8217;s <a target=_blank title='printf' href='http:\/\/www.cplusplus.com\/reference\/cstdio\/printf'>printf<\/a> and sprintf and fprintf (which, of course, you can still use if you wish)), and this may be true.   However if you add iomanip.h into the equation it gets easier.<\/p>\n<p>Today, inspired by a Yahoo Answers <a target=_blank title='Yahoo Answers question' href='http:\/\/au.answers.yahoo.com\/question\/index?qid=20140304120518AAaNR7L'>question<\/a> (thanks) we take an introductory look at &#8220;cout&#8221; and the use of:<\/p>\n<ul>\n<li>cout.precision([numberSignificantNumbers])<\/li>\n<li>setw([colWidth]) \/\/ via iomanip.h<\/li>\n<li>endl<\/li>\n<\/ul>\n<p>Here is the C++ code (with comments) below within the Xcode IDE &#8230; project called &#8220;cout_formatting&#8221; &#8230; source code called &#8220;main.cpp&#8221;  &#8230; show logarithmic table &#8230;<\/p>\n<p><code><br \/>\n\/\/ cout_formatting.cpp<br \/>\n\/\/ Format a logarithmic table to standard output via cout and ...<br \/>\n\/\/   Also use:<br \/>\n\/\/         cout.precision([numberSignificantNumbers])<br \/>\n\/\/         setw([colWidth])<br \/>\n\/\/         endl<\/p>\n<p>using namespace std;<br \/>\n#include \"iostream.h\"       \/\/ needed for cout and cout.precision (and cin)<br \/>\n#include \"iomanip.h\"        \/\/ needed for setw()<br \/>\n#include \"math.h\"           \/\/ needed for log() method<\/p>\n<p>int main() {<br \/>\n    int rows, columns, n, i, j;          \/\/ rows=number of base values in table, columns=number of log values in table<br \/>\n    double base1 = 7.0, base2;           \/\/ base1 is log value, base2 is base value ... double values<br \/>\n    cout << \"Log values program\" << endl << endl;    \/\/ Introduce ...<br \/>\n    cout << \"Enter start logarithm value: \";         \/\/ Prompt and accept base2 ...<br \/>\n    cin >> base2;<br \/>\n    cout << \"Enter how many logarithm values : \";    \/\/ Prompt and accept rows ...<br \/>\n    cin >> rows;<br \/>\n    cout << \"Enter start base value : \";              \/\/ Prompt and accept base1 ...<br \/>\n    cin >> base1;<br \/>\n    cout << \"Enter how many base values : \" ;        \/\/ Prompt and accept columns ...<br \/>\n    cin >> columns;<br \/>\n    cout << \"Enter precision : \";                     \/\/ Prompt and accept precision (number of significant numbers) ...<br \/>\n    cin >> n;<br \/>\n    cout.precision(n);                    \/\/ set cout's precision for double values<\/p>\n<p>    cout << endl << endl << \"Table starts at logarithm of b = \" << base2 << \" with base a = \" << base1 << endl; \/\/ First output line on top of table ...<br \/>\n    cout << \"Log values \";     \/\/ ... start of second line at top of Logarithmic Table ...<br \/>\n    for( j=0; j < rows; j++) cout << setw(8) << \" b + \" << j;    \/\/ ... rest of second line ...<br \/>\n    cout << endl;                                              \/\/ ... with end of line ...<br \/>\n    for( i=0; i < columns; i++) {                 \/\/ ... for each base value ...<br \/>\n        cout << \"     a + \" << i << \" \";<br \/>\n        for( j=0; j < rows; j++) {                \/\/ ... for each log value ...<br \/>\n            cout << setw(8) << (log(base2+j) \/ log(base1+i)) << \" \";   \/\/ ... output (to standard output) a logarithmic table calculation ...<br \/>\n        }<br \/>\n        cout << endl;    \/\/ ... end of a table row ...<br \/>\n    }<br \/>\n    return 0;   \/\/ Finish ... bye.<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Here is a link with fewer comments here you could rename to <a target=_blank title='main.cpp' href='http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/cout_formatting\/main.cpp_GETME'>main.cpp<\/a> ( and, for Xcode C++, at least, place it as the main.cpp source code for a  C++ new Command Line Tool  project of type C++ ).<\/p>\n<p>Good link that helped with answer is <a target=_blank title='setw usage' href='http:\/\/georgiakeays.me\/Cplus\/20120822\/1372033545199587.html'>here<\/a>.<\/p>\n<p>Today&#8217;s tutorial is based on a Yahoo Answers question <a target=_blank title='Yahoo Answers question' href='http:\/\/au.answers.yahoo.com\/question\/index?qid=20140304120518AAaNR7L'>here<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d6632' onclick='var dv=document.getElementById(\"d6632\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/??tag=c\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d6632' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d7175' onclick='var dv=document.getElementById(\"d7175\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/??tag=c\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d7175' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today, inspired by a Yahoo Answers question (thanks) we take a look at some C code dealing with files (both writing, and then rereading) and interactive input and string functionality in the downloadable C programming code you could call filestuff.c: &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-file-write-and-read-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[176,418,997,1206,1319],"class_list":["post-7175","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-c","tag-file","tag-programming","tag-string-functions","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7175"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=7175"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7175\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=7175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=7175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=7175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}