Browse Source

README-en.md edited online with Bitbucket

Jose R Ortiz Ubarri 8 years ago
parent
commit
6fb2c3f919
1 changed files with 137 additions and 0 deletions
  1. 137
    0
      README-en.md

+ 137
- 0
README-en.md View File

@@ -153,6 +153,143 @@ In this laboratory experience, you are provided a `GPOI` class with the followin
153 153
 ---
154 154
 
155 155
 ---
156
+## Reading data from text files in C++
157
+
158
+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... 
159
+
160
+
161
+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.
162
+
163
+```
164
+Tomas 34
165
+Marta 55
166
+Remigio 88
167
+Andrea 43
168
+```
169
+
170
+To **read** a text file in C++, we need to have a sense of how it is organized and what type of data you would like to read. The example `nameAge.txt` file contains four lines, each consisting of a string and an integer.  Here is a simple program to read that file entirely while printing its content. Read the comments to understand the various parts.
171
+
172
+```
173
+
174
+#include <iostream>
175
+
176
+// fstream is the header file that contains classes, functions and 
177
+// objects to deal with file input and output.
178
+#include <fstream>  
179
+
180
+using namespace std;
181
+
182
+int main(){
183
+
184
+    // We shall use these two variables to assign the values read
185
+    // from each line in the file.
186
+    string name;
187
+    int age;
188
+    
189
+    // This is the object that will represent the file.
190
+    ifstream inFile;
191
+    
192
+    // We call the open function to open the input file `nameAge.txt` 
193
+    inFile.open("nameAge.txt");
194
+    
195
+    
196
+    // We check if the file was correctly opened
197
+    if (!inFile.is_open()) {
198
+        cout << "Error openning file nameAge.txt\n";
199
+        exit(1);
200
+    }
201
+
202
+    // While there is data in the file, read a string and an int.
203
+    // Notice how the `>>` symbol is used, similar to when using cin
204
+ 
205
+    while (inFile  >> name >> age) {
206
+        cout << name << " : " << age << endl;
207
+    }
208
+    
209
+    // Close the file. 
210
+    inFile.close();
211
+    
212
+    return 0;
213
+}
214
+```
215
+
216
+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.
217
+
218
+Notice the line `inFile  >> name >> age`. This instruction accomplishes several tasks:
219
+
220
+* It reads a `string` and an `int` from the file (if available) and assigns them to the variables `name` and `age`.
221
+* If both data were read, the expression evaluates to `true`, thus entering the while block.
222
+* If both data could not be read, the expression evaluates to `false` thus ending the while block.  
223
+
224
+
225
+Here are some code snippets for common reading tasks. Observe that all of them:
226
+
227
+1. Create an `ifstream` object, call the `open` function and check if the file is opened correctly.
228
+2. Create one or more variables to assign the values that are read from the file. 
229
+3. Implement a loop which repeats until no more data is available in the file. 
230
+3. `close` the file at the end.
231
+
232
+
233
+**Example 1**: Read a file that consists only of integers, accumulate their values into a sum.
234
+
235
+```
236
+    ifstream inFile;
237
+    int n;
238
+    int accum = 0;
239
+    
240
+    inFile.open("nums.txt");
241
+
242
+    if (!inFile.is_open()) {
243
+        cout << "Error openning file nums.txt\n";
244
+        exit(1);
245
+    }
246
+
247
+    while (inFile  >> n) {
248
+        accum = accum + n;
249
+    }
250
+
251
+    cout << "Total: "  << accum << endl;
252
+    
253
+    inFile.close();
254
+```
255
+
256
+**Example 2**: Count the number of lines in a file that consists of names. Then choose the name at the center line.
257
+
258
+```
259
+    ifstream inFile;
260
+    string name;
261
+    int ctr = 0;
262
+    
263
+    inFile.open("names.txt");
264
+
265
+    if (!inFile.is_open()) {
266
+        cout << "Error openning file names.txt\n";
267
+        exit(1);
268
+    }
269
+
270
+    while (inFile  >> name) {
271
+        ctr++;
272
+    }
273
+
274
+    cout << "Total number of lines: " << ctr << endl;
275
+
276
+    // These two commands "rewind" the file so that we can start
277
+    // reading again from the beginning. 
278
+    inFile.clear();
279
+    inFile.seekg(0);
280
+
281
+    for (int i = 0; i <= ctr / 2; i++) {
282
+        inFile >> name;
283
+    }
284
+
285
+    cout << "The name at the position " << ctr / 2 << ": " << name << endl;
286
+
287
+    inFile.close();
288
+```
289
+
290
+---
291
+
292
+---
156 293
 
157 294
 ##Laboratory Session:
158 295