C File Write and Read Primer Tutorial

C File Write and Read Primer Tutorial

C File Write and Read Primer Tutorial

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:

  • Note use of fgets(inname, 300, stdin); rather than gets(inname); to avoid possibility of string running out of bounds.
  • Note use of strtok for delimitation login like explode in PHP or split in Javascript.
  • Note the use of sscanf format string “%[^n]s” for “up to first line break but not including it” … rather than risk a problem with filenames with spaces in them and to exclude a possible line break in the string.
  • Note algorithm for Moving Average … avgrad = (avgrad * ((double)(i – 1)) + tavgrad) / (double)i;

There are other file opening functions available other than the fopen we use here … see the open function and its associated functions written about here.

C how you go … chortle, chortle! C if it interests U … weaker chortle, chortle! C if it gets really annoying … practically non-existant chortle, chortle.


Previous tutorial of relevance C++ Formatting cout Primer Tutorial is shown below.

C++ Formatting cout Primer Tutorial

C++ Formatting cout Primer Tutorial

C++ uses iostream.h methods to format command line output. Programmers with a background in C may think the iostream.h “cout” stream output methods are a little harder to use (compared to C’s printf 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.

Today, inspired by a Yahoo Answers question (thanks) we take an introductory look at “cout” and the use of:

  • cout.precision([numberSignificantNumbers])
  • setw([colWidth]) // via iomanip.h
  • endl

Here is the C++ code (with comments) below within the Xcode IDE … project called “cout_formatting” … source code called “main.cpp” … show logarithmic table …


// cout_formatting.cpp
// Format a logarithmic table to standard output via cout and ...
// Also use:
// cout.precision([numberSignificantNumbers])
// setw([colWidth])
// endl

using namespace std;
#include "iostream.h" // needed for cout and cout.precision (and cin)
#include "iomanip.h" // needed for setw()
#include "math.h" // needed for log() method

int main() {
int rows, columns, n, i, j; // rows=number of base values in table, columns=number of log values in table
double base1 = 7.0, base2; // base1 is log value, base2 is base value ... double values
cout << "Log values program" << endl << endl; // Introduce ...
cout << "Enter start logarithm value: "; // Prompt and accept base2 ...
cin >> base2;
cout << "Enter how many logarithm values : "; // Prompt and accept rows ...
cin >> rows;
cout << "Enter start base value : "; // Prompt and accept base1 ...
cin >> base1;
cout << "Enter how many base values : " ; // Prompt and accept columns ...
cin >> columns;
cout << "Enter precision : "; // Prompt and accept precision (number of significant numbers) ...
cin >> n;
cout.precision(n); // set cout's precision for double values

cout << endl << endl << "Table starts at logarithm of b = " << base2 << " with base a = " << base1 << endl; // First output line on top of table ...
cout << "Log values "; // ... start of second line at top of Logarithmic Table ...
for( j=0; j < rows; j++) cout << setw(8) << " b + " << j; // ... rest of second line ...
cout << endl; // ... with end of line ...
for( i=0; i < columns; i++) { // ... for each base value ...
cout << " a + " << i << " ";
for( j=0; j < rows; j++) { // ... for each log value ...
cout << setw(8) << (log(base2+j) / log(base1+i)) << " "; // ... output (to standard output) a logarithmic table calculation ...
}
cout << endl; // ... end of a table row ...
}
return 0; // Finish ... bye.
}

Here is a link with fewer comments here you could rename to main.cpp ( 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++ ).

Good link that helped with answer is here.

Today’s tutorial is based on a Yahoo Answers question here.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Tutorials and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>