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
Next revision
Previous revision
c:c_io_tips [2020/07/26 13:13] – old revision restored (2016/08/05 13:53) 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 1: Line 1:
 ====== C - C++ IO Tips ====== ====== C - C++ IO Tips ======
  
-There are three header files to include when using C++ I/O+===== C++ IO Header Files ===== 
 + 
 +Header files to include when using C++ I/O.
  
 <code c++> <code c++>
Line 7: Line 9:
 </code> </code>
  
-Include this file whenever using C++ I/O+Include this file whenever using C++ I/O
  
 <code c++> <code c++>
Line 13: Line 16:
 </code> </code>
  
-This file must be included for most C++ manipulators. If you don't know what a manipulator is, don't worry. Just include this file along with iostream and you can't go wrong+This file must be included for most C++ manipulators.
  
 <code c++> <code c++>
Line 20: Line 23:
  
 Include this file whenever working with files. Include this file whenever working with files.
 +
 +
 +===== Leading whitespace =====
  
 By default, leading whitespace (carriage returns, tabs, spaces) is ignored by cin. By default, leading whitespace (carriage returns, tabs, spaces) is ignored by cin.
Line 34: Line 40:
 And you type: 3.14<return>42<return> And you type: 3.14<return>42<return>
  
-3.14 is read into fl . The carriage return (newline) following the 3.14 is still sitting on the input buffer. +  - 3.14 is read into fl . The carriage return (newline) following the 3.14 is still sitting on the input buffer. 
-Since std::cin ignores whitespace, the first return is "eaten" by std::cin >> i . Then the integer 42 is read into i and the second return is left on the input buffer.+  Since std::cin ignores whitespace, the first return is "eaten" by std::cin >> i.  Then the integer 42 is read into i and the second return is left on the input buffer.
  
-std::cin.getline() can run into problems when used with std::cin >> var. 
  
-getline can be provided a third argument--a "stop" character. This character ends getline's input. The character is eaten and the string is terminated. Example:+===== Read in an entire line ===== 
 + 
 +**std::cin.getline()** can run into problems when used with **std::cin >> var**. 
 + 
 +**getline** can be provided a third argumenta "stop" character.  This character ends getline's input.  The character is eaten and the string is terminated.   
 + 
 +Example:
  
 <code c++> <code c++>
Line 45: Line 56:
 </code> </code>
  
-If std::cin.getline() is not provided a "stop" character as a third argument, it will stop when it reaches a newline.+If **std::cin.getline()** is not provided a "stop" character as a third argument, it will stop when it reaches a newline.
  
 Given: Given:
Line 58: Line 69:
 And you type: 3.14<return> And you type: 3.14<return>
  
-3.14 is read into fl . The newline following the 3.14 is still sitting on the input buffer. +  - 3.14 is read into fl . The newline following the 3.14 is still sitting on the input buffer. 
-std::cin.getline(str, 101) immediately processes the newline that is still on the input buffer. str becomes an empty string. +  std::cin.getline(str, 101) immediately processes the newline that is still on the input buffer.  str becomes an empty string. 
-The illusion is that the application "skipped" the std::cin.getline() statement. +  The illusion is that the application "skipped" the std::cin.getline() statement. 
-The solution is to add std::cin.ignore(); immediately after the first std::cin statement. This will grab a character off of the input buffer (in this case, newline) and discard it.+ 
 +The solution is to add **std::cin.ignore();** immediately after the first **std::cin** statement.  This will grab a character off of the input buffer (in this case, newline) and discard it.
  
-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 71: 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 77: 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 83: 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 114: Line 132:
 </code> </code>
      
-Inputing numbers directly, version 2:+Inputting numbers directly, version 2:
  
 <code c++> <code c++>
Line 126: 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 134: 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 149: 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 163: 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 174: 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 183: 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 219: 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 267: 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 287: 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.1595769214.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