c:c_io_tips
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
c:c_io_tips [2016/08/05 12:53] – created peter | c: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 | + | ===== C++ IO Header Files ===== |
+ | |||
+ | Header | ||
<code c++> | <code c++> | ||
Line 7: | Line 9: | ||
</ | </ | ||
- | Include this file whenever using C++ I/O | + | Include this file whenever using C++ I/O. |
<code c++> | <code c++> | ||
Line 13: | Line 16: | ||
</ | </ | ||
- | This file must be included for most C++ manipulators. | + | 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< | And you type: 3.14< | ||
- | 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 " | + | |
- | std:: | ||
- | getline can be provided a third argument--a " | + | ===== Read in an entire line ===== |
+ | |||
+ | **std:: | ||
+ | |||
+ | **getline** can be provided a third argument; a " | ||
+ | |||
+ | Example: | ||
<code c++> | <code c++> | ||
Line 45: | Line 56: | ||
</ | </ | ||
- | If std:: | + | If **std:: |
Given: | Given: | ||
Line 58: | Line 69: | ||
And you type: 3.14< | And you type: 3.14< | ||
- | 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:: | + | |
- | The illusion is that the application " | + | |
- | The solution is to add std:: | + | |
+ | The solution is to add **std:: | ||
- | std:: | + | **std:: |
- | 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: | ||
</ | </ | ||
- | 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: | ||
</ | </ | ||
- | 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: | ||
</ | </ | ||
- | 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 114: | Line 132: | ||
</ | </ | ||
| | ||
- | Inputing | + | Inputting |
<code c++> | <code c++> | ||
Line 126: | Line 144: | ||
</ | </ | ||
| | ||
- | A note on limits. If your compiler doesn' | + | **NOTE**: |
<code c++> | <code c++> | ||
Line 134: | 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 149: | 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 163: | 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 174: | 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 183: | 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 219: | 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 267: | 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 287: | 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 " | + | |
c/c_io_tips.1470401586.txt.gz · Last modified: 2020/07/15 09:30 (external edit)