Переглянути джерело

README-en.md edited online with Bitbucket

Rafael Arce Nazario 8 роки тому
джерело
коміт
1879ee4bb9
1 змінених файлів з 137 додано та 0 видалено
  1. 137
    0
      README-en.md

+ 137
- 0
README-en.md Переглянути файл

@@ -131,6 +131,143 @@ The **frequency of occurrence** is defined as the ratio of times that a digit ap
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
+
136
+
137
+## Reading data from text files in C++
138
+
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.
141
+
142
+```
143
+Tomas 34
144
+Marta 55
145
+Remigio 88
146
+Andrea 43
147
+```
148
+
149
+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.
150
+
151
+```
152
+
153
+#include <iostream>
154
+
155
+// fstream is the header file that contains classes, functions and 
156
+// objects to deal with file input and output.
157
+#include <fstream>  
158
+
159
+using namespace std;
160
+
161
+int main(){
162
+
163
+    // We shall use these two variables to assign the values read
164
+    // from each line in the file.
165
+    string name;
166
+    int age;
167
+    
168
+    // This is the object that will represent the file.
169
+    ifstream inFile;
170
+    
171
+    // We call the open function to open the input file `nameAge.txt` 
172
+    inFile.open("nameAge.txt");
173
+    
174
+    
175
+    // We check if the file was correctly opened
176
+    if (!inFile.is_open()) {
177
+        cout << "Error openning file nameAge.txt\n";
178
+        exit(1);
179
+    }
180
+
181
+    // While there is data in the file, read a string and an int.
182
+    // Notice how the `>>` symbol is used, similar to when using cin
183
+ 
184
+    while (inFile  >> name >> age) {
185
+        cout << name << " : " << age << endl;
186
+    }
187
+    
188
+    // Close the file. 
189
+    inFile.close();
190
+    
191
+    return 0;
192
+}
193
+```
194
+
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
+
197
+Notice the line `inFile  >> name >> age`. This instruction accomplishes several tasks:
198
+
199
+* 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 could not be read, the expression evaluates to `false` thus ending the while block.  
202
+
203
+
204
+Here are some code snippets for common reading tasks. Observe that all of them:
205
+
206
+1. Create a 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
+3. Implement a loop which repeats until no more data is available in the file. 
209
+3. `close` the file at the end.
210
+
211
+
212
+**Example 1**: Read a file that consists only of integers, accumulate their values into a sum.
213
+
214
+```
215
+    ifstream inFile;
216
+    int n;
217
+    int accum = 0;
218
+    
219
+    inFile.open("nums.txt");
220
+
221
+    if (!inFile.is_open()) {
222
+        cout << "Error openning file nums.txt\n";
223
+        exit(1);
224
+    }
225
+
226
+    while (inFile  >> n) {
227
+        accum = accum + n;
228
+    }
229
+
230
+    cout << "Total: "  << accum << endl;
231
+    
232
+    inFile.close();
233
+```
234
+
235
+**Example 2**: Count the number of lines in a file that consists of names. Then choose the name at the center line.
236
+
237
+```
238
+    ifstream inFile;
239
+    string name;
240
+    int ctr = 0;
241
+    
242
+    inFile.open("names.txt");
243
+
244
+    if (!inFile.is_open()) {
245
+        cout << "Error openning file names.txt\n";
246
+        exit(1);
247
+    }
248
+
249
+    while (inFile  >> name) {
250
+        ctr++;
251
+    }
252
+
253
+    cout << "Total number of lines: " << ctr << endl;
254
+
255
+    // These two commands "rewind" the file so that we can start
256
+    // reading again from the beginning. 
257
+    inFile.clear();
258
+    inFile.seekg(0);
259
+
260
+    for (int i = 0; i <= ctr / 2; i++) {
261
+        inFile >> name;
262
+    }
263
+
264
+    cout << "The name at the position " << ctr / 2 << ": " << name << endl;
265
+
266
+    inFile.close();
267
+```
268
+
269
+
270
+
134 271
 ##Laboratory session
135 272
 
136 273
 ###Exercise 1: Understand the data files and the provided code