c:c_io_tips
Differences
This shows you the differences between two versions of the page.
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.145 | c: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:: | **std:: | ||
- | | + | |
<code c++> | <code c++> | ||
Line 83: | Line 83: | ||
</ | </ | ||
- | | + | |
<code c++> | <code c++> | ||
Line 89: | Line 89: | ||
</ | </ | ||
- | | + | |
<code c++> | <code c++> | ||
Line 95: | Line 95: | ||
</ | </ | ||
- | Reading in numbers directly is problematic | ||
- | If std::cin is presented with input it cannot process, std::cin goes into a " | + | ===== Reading in numbers directly is problematic ===== |
+ | |||
+ | If **std::cin** is presented with input it cannot process, std::cin goes into a " | ||
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 " | + | |
+ | All input will be ignored by **std::cin** until the " | ||
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:: | + | |
- | Call std:: | + | |
- | Remove from the stream the input that caused the problem: std:: | + | |
- | Get the input again if appropriate or otherwise handle the error | + | |
- | Inputing | + | |
+ | |||
+ | Inputting | ||
<code c++> | <code c++> | ||
Line 126: | Line 132: | ||
</ | </ | ||
| | ||
- | Inputing | + | Inputting |
<code c++> | <code c++> | ||
Line 138: | Line 144: | ||
</ | </ | ||
| | ||
- | A note on limits. If your compiler doesn' | + | **NOTE**: |
<code c++> | <code c++> | ||
Line 146: | Line 152: | ||
</ | </ | ||
- | 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: | ||
</ | </ | ||
- | getline will read both strings and numbers without going into a " | + | * getline will read both strings and numbers without going into a " |
- | 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: | ||
</ | </ | ||
- | 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: | ||
</ | </ | ||
- | 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: | + | |
- | 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: |
+ | |||
+ | |||
+ | ===== Getline can be told to stop grabbing input at any designated character | ||
<code c++> | <code c++> | ||
Line 195: | Line 208: | ||
</ | </ | ||
- | 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. | + | |
- | The stop character is not copied to the string. | + | |
- | The stop character is " | + | |
- | 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: | ||
</ | </ | ||
- | 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: | ||
- | 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 " | + | 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 " |
Given data file: | Given data file: | ||
Line 279: | Line 294: | ||
</ | </ | ||
- | 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: | ||
</ | </ | ||
- | The default fill character is a space. | + | * The default fill character is a space. |
+ | * A common fill character when printing numbers is zero " | ||
- | A common fill character when printing numbers is zero " |
c/c_io_tips.1595769219.txt.gz · Last modified: 2020/07/26 13:13 by 184.15.160.145