User Tools

Site Tools


c:c_io_tips

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
c:c_io_tips [2020/07/26 13:13] – old revision restored (2016/08/05 14:01) 184.15.160.145c:c_io_tips [2020/07/26 13:13] (current) – old revision restored (2016/08/05 14:11) 184.15.160.145
Line 77: Line 77:
 **std::cin.ignore()** can be called three different ways: **std::cin.ignore()** can be called three different ways:
  
-  No arguments: A single character is taken from the input buffer and discarded:+  No arguments: A single character is taken from the input buffer and discarded:
  
 <code c++> <code c++>
Line 83: Line 83:
 </code> </code>
  
-  One argument: The number of characters specified are taken from the input buffer and discarded:+  One argument: The number of characters specified are taken from the input buffer and discarded:
  
 <code c++> <code c++>
Line 89: Line 89:
 </code> </code>
  
-  Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first):+  Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first):
  
 <code c++> <code c++>
Line 95: Line 95:
 </code> </code>
  
-Reading in numbers directly is problematic 
  
-If std::cin is presented with input it cannot process, std::cin goes into a "fail" state+===== Reading in numbers directly is problematic ===== 
 + 
 +If **std::cin** is presented with input it cannot process, std::cin goes into a "fail" state
 The input it cannot process is left on the input stream. The input it cannot process is left on the input stream.
-All input will be ignored by std::cin until the "fail" state is cleared: std::cin.clear()+ 
 +All input will be ignored by **std::cin** until the "fail" state is cleared: **std::cin.clear()**. 
 A routine that reads a number directly should: A routine that reads a number directly should:
-Read in the number + 
-Check to see that the input stream is still valid +  * Read in the number. 
-If the input stream is not good (!std::cin) +  Check to see that the input stream is still valid. 
-Call std::cin.clear() to take the stream out of the "fail" state. +  If the input stream is not good (!std::cin) 
-Remove from the stream the input that caused the problem: std::cin.ignore(...) +    Call std::cin.clear() to take the stream out of the "fail" state. 
-Get the input again if appropriate or otherwise handle the error +    Remove from the stream the input that caused the problem: std::cin.ignore(...) 
-Inputing numbers directly, version 1:+    Get the input again if appropriate or otherwise handle the error 
 + 
 +Inputting numbers directly, version 1:
  
 <code c++> <code c++>
Line 126: Line 132:
 </code> </code>
      
-Inputing numbers directly, version 2:+Inputting numbers directly, version 2:
  
 <code c++> <code c++>
Line 138: Line 144:
 </code> </code>
      
-A note on limits. If your compiler doesn't support std::numeric_limits<streamsize>::max(), an alternative is to use the c-style method for determining the maximum integer allowed:+**NOTE**:  A note on limits.  If your compiler doesn't support **std::numeric_limits<streamsize>::max()**, an alternative is to use the c-style method for determining the maximum integer allowed:
  
 <code c++> <code c++>
Line 146: Line 152:
 </code> </code>
  
-Using getline to input numbers is a more robust alternate to reading numbers directly+ 
 +===== Using getline to input numbers is a more robust alternate to reading numbers directly =====
  
 <code c++> <code c++>
Line 161: Line 168:
 </code> </code>
  
-getline will read both strings and numbers without going into a "fail" state. +  * getline will read both strings and numbers without going into a "fail" state. 
-Include cstdlib to use the converter functions: string-to-long-integer (strtol), string-to-double (strtod), string-to-float (strtof), and string-to-long-double (strtold). Refer to a reference for more information on the second (and third for strtol) arguments to these converter functions. +  Include **cstdlib** to use the converter functions: string-to-long-integer (**strtol**), string-to-double (**strtod**), string-to-float (**strtof**), and string-to-long-double (**strtold**).  Refer to a reference for more information on the second (and third for **strtol**) arguments to these converter functions. 
-Once a file is opened, it may be used exactly as std::cin is used.+ 
 + 
 +===== Once a file is opened, it may be used exactly as std::cin is used. =====
  
 <code c++> <code c++>
Line 175: Line 184:
 </code> </code>
  
-When reading an entire file, embed the file input inside of the loop condition+ 
 +===== When reading an entire file, embed the file input inside of the loop condition =====
  
 <code c++> <code c++>
Line 186: Line 196:
 </code> </code>
  
-the loop will exit once the end of the file is reached +  * the loop will exit once the end of the file is reached 
-If the expression in the while() loop above is confusing, I've provided an explanation: UnderstandingComplexExpressions.pdf. + 
-Getline can be told to stop grabbing input at any designated character+If the expression in the while() loop above is confusing, I've provided an explanation: [[http://www.augustcouncil.com/~tgibson/tutorial/UnderstandingComplexExpressions.pdf|UnderstandingComplexExpressions.pdf]]. 
 + 
 + 
 +===== Getline can be told to stop grabbing input at any designated character =====
  
 <code c++> <code c++>
Line 195: Line 208:
 </code> </code>
  
-If only two arguments are supplied to getline, getline will stop at the end of the line (at the newline character). +  * If only two arguments are supplied to getline, getline will stop at the end of the line (at the newline character). 
-If three arguments are supplied to getline, getline will stop at the character designated by the third argument. +  If three arguments are supplied to getline, getline will stop at the character designated by the third argument. 
-The stop character is not copied to the string. +  The stop character is not copied to the string. 
-The stop character is "eaten" (removed from the input stream). +  The stop character is "eaten" (removed from the input stream). 
-Delimited files can easily be read using a while loop and getline.+ 
 + 
 +===== Delimited files can easily be read using a while loop and getline. =====
  
 Given data file: Given data file:
Line 231: Line 246:
 </code> </code>
  
-In a delimited file, only the first field should be in the while loop+  * In a delimited file, only the first field should be in the while loop 
 +  * For each field:  If the field is the last field in the line or the only field in the line, be sure that **getline** stops at a newline and not some other delimiter
  
-For each field: If the field is the last field in the line or the only field in the line, be sure that getline stops at a newline and not some other delimiter 
  
-Using C++-style strings+===== Using C++-style strings =====
  
-All of the previous examples have assumed that C-style strings (null-terminated character arrays) were being used. C++ provides a string class that, when combined with a particular "getline" function, can dynamically resize to accommodate user input. In general, C++ strings are preferred over C strings.+All of the previous examples have assumed that C-style strings (null-terminated character arrays) were being used.  C++ provides a string class that, when combined with a particular "getline" function, can dynamically resize to accommodate user input.  In general, C++ strings are preferred over C strings.
  
 Given data file: Given data file:
Line 279: Line 294:
 </code>   </code>  
  
-How to set the width of a printing field+ 
 +===== How to set the width of a printing field =====
  
 Given: int one=4, two=44; Given: int one=4, two=44;
Line 299: Line 315:
 </code>   </code>  
  
-The default fill character is a space.+  * The default fill character is a space
 +  * A common fill character when printing numbers is zero "0".
  
-A common fill character when printing numbers is zero "0". 
c/c_io_tips.1595769219.txt.gz · Last modified: 2020/07/26 13:13 by 184.15.160.145

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki