|
|
|
|
4
|
![main2.png](images/main2.png)
|
4
|
![main2.png](images/main2.png)
|
5
|
![main3.png](images/main3.png)
|
5
|
![main3.png](images/main3.png)
|
6
|
|
6
|
|
7
|
-[Verano 2016 - Ive]
|
|
|
|
|
7
|
+[Verano 2016 - Ive - Rafa - Ive]
|
8
|
|
8
|
|
9
|
Arrays help us to store and work with groups of data of the same type. The data is stored in consecutive memory spaces which can be accessed by using the name of the array and indexes or subscripts that indicate the position where the data is stored. Repetition structures provide us a simple way of accessing the data within an array. In today's laboratory experience you will practice the use of counters and one dimensional arrays to implement a program in which you will use Benford’s Law to detect files with bogus data.
|
9
|
Arrays help us to store and work with groups of data of the same type. The data is stored in consecutive memory spaces which can be accessed by using the name of the array and indexes or subscripts that indicate the position where the data is stored. Repetition structures provide us a simple way of accessing the data within an array. In today's laboratory experience you will practice the use of counters and one dimensional arrays to implement a program in which you will use Benford’s Law to detect files with bogus data.
|
10
|
|
10
|
|
|
|
|
|
131
|
|
131
|
|
132
|
---
|
132
|
---
|
133
|
|
133
|
|
134
|
-This lab requires you to read data from a text file. You can skip the next section if you feel that your file reading skills are competent. Otherwise, read on..
|
|
|
135
|
|
134
|
|
136
|
|
135
|
|
137
|
## Reading data from text files in C++
|
136
|
## Reading data from text files in C++
|
138
|
|
137
|
|
|
|
138
|
+This laboratory experience requires you to read data from a text file. You can skip the next section if you feel that your file reading skills are competent. Otherwise, read on...
|
139
|
|
139
|
|
140
|
-C++ provides functions to read and write data to/from files. In this lab you will be using one of the most rudimentary file input/output schemes provided in C++ to read/write from **text** files. Text files consist exclusively of ASCII characters which represent data in any of the primitive types provided by C++. Typically, the values are separated by spaces. For instance lets asumme that the file `nameAge.txt` contains some data about names and ages.
|
|
|
|
|
140
|
+
|
|
|
141
|
+C++ provides functions to read and write data to/from files. In this laboratory experience you will be using one of the most rudimentary file input/output schemes provided in C++ to read/write from **text** files. Text files consist exclusively of ASCII characters which represent data in any of the primitive types provided by C++. Typically, the values are separated by spaces. For instance lets asumme that the file `nameAge.txt` contains some data about names and ages.
|
141
|
|
142
|
|
142
|
```
|
143
|
```
|
143
|
Tomas 34
|
144
|
Tomas 34
|
|
|
|
|
192
|
}
|
193
|
}
|
193
|
```
|
194
|
```
|
194
|
|
195
|
|
195
|
-The ifstream object is used for reading a text file **sequentially**. It keeps track of the next position in the file that should be read. Each time that a data is read from the file (using `inFile >> ____`) it advances its position so that the next `inFile >> ___` reads the next data and so forth.
|
|
|
|
|
196
|
+The `ifstream` object is used for reading a text file **sequentially**. It keeps track of the next position in the file that should be read. Each time that a data is read from the file (using `inFile >> ____`) it advances its position so that the next `inFile >> ___` reads the next data and so forth.
|
196
|
|
197
|
|
197
|
Notice the line `inFile >> name >> age`. This instruction accomplishes several tasks:
|
198
|
Notice the line `inFile >> name >> age`. This instruction accomplishes several tasks:
|
198
|
|
199
|
|
199
|
-* It reads a string and an int from the file (if available) and assigns them to the variables `name` and `age`.
|
|
|
|
|
200
|
+* It reads a `string` and an `int` from the file (if available) and assigns them to the variables `name` and `age`.
|
200
|
* If both data were read, the expression evaluates to `true`, thus entering the while block.
|
201
|
* If both data were read, the expression evaluates to `true`, thus entering the while block.
|
201
|
* If both data could not be read, the expression evaluates to `false` thus ending the while block.
|
202
|
* If both data could not be read, the expression evaluates to `false` thus ending the while block.
|
202
|
|
203
|
|
203
|
|
204
|
|
204
|
Here are some code snippets for common reading tasks. Observe that all of them:
|
205
|
Here are some code snippets for common reading tasks. Observe that all of them:
|
205
|
|
206
|
|
206
|
-1. Create a ifstream object, call the `open` function and check if the file is opened correctly.
|
|
|
|
|
207
|
+1. Create an `ifstream` object, call the `open` function and check if the file is opened correctly.
|
207
|
2. Create one or more variables to assign the values that are read from the file.
|
208
|
2. Create one or more variables to assign the values that are read from the file.
|
208
|
3. Implement a loop which repeats until no more data is available in the file.
|
209
|
3. Implement a loop which repeats until no more data is available in the file.
|
209
|
3. `close` the file at the end.
|
210
|
3. `close` the file at the end.
|
|
|
|
|
266
|
inFile.close();
|
267
|
inFile.close();
|
267
|
```
|
268
|
```
|
268
|
|
269
|
|
|
|
270
|
+---
|
269
|
|
271
|
|
|
|
272
|
+---
|
270
|
|
273
|
|
271
|
##Laboratory session
|
274
|
##Laboratory session
|
272
|
|
275
|
|