Tag Archives: istringstream

C++ MultiDimensional Array istringstream Tutorial

Today, building on previous C++ Formatting cout Primer Tutorial as shown below, and inspired by a Yahoo Answers question (thanks) we take a look at the reading of file data into a two dimensional (scores) array, featuring: cout and cin to get the name of the input file … cout used for debugging information too ifstream’s open method to open that file istream’s getline method to read a record at a time, in a loop, that will stop with the end of file condition istringstream‘s iss() method (of sstream) to tokenize and fill the name string variable and each scores array row values, column by column ifstream’s close method to finish up You can download some C++ programmable source code here that you could call main.cpp if you like. We hope you get something++ out of today’s tutorial, and we thank this link for its help. Previous relevant C File Write and Read Primer Tutorial is shown below. 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++ 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)), … Continue reading →

Posted in eLearning, Tutorials | Tagged , , , , , | 10 Comments