Académique Documents
Professionnel Documents
Culture Documents
Objectifs:
Créer des fichiers de données selon des formats bien définis,
Lire le contenu d'un fichier de données connaissant son format et le mémoriser en utilisant
des tableaux 1D (vecteurs),
Générer des données aléatoires dans des intervalles de valeurs à choisir,
Créer des fichiers de données artificielles.
1 %%writefile notes.txt
2 5
3 1
4 Aouzou Abdelhakim 12.5
5 Ben_Mouina Achraf 11
6 Arjdal Abderrahim 13.25
7 Labihi Abdelhamid 10
8 Sittaf Abdelghani 14.3
9
Writing notes.txt
1 ls -lt
2
total 8
-rw-r--r-- 1 root root 155 Apr 21 22:00 notes.txt
drwxr-xr-x 1 root root 4096 Apr 19 14:23 sample_data/
1 !nl notes.txt
1 5
2 1
3 Aouzou Abdelhakim 12.5
4 Ben_Mouina Achraf 11
5 Arjdal Abderrahim 13.25
6 Labihi Abdelhamid 10
7 Sittaf Abdelghani 14.3
1 !cat notes.txt
5
1
Aouzou Abdelhakim 12.5
Ben_Mouina Achraf 11
Arjdal Abderrahim 13.25
Labihi Abdelhamid 10
Sittaf Abdelghani 14.3
1 !head -5 notes.txt
5
1
Aouzou Abdelhakim 12.5
Ben_Mouina Achraf 11
Arjdal Abderrahim 13.25
1 !tail -3 notes.txt
2
1 %%writefile readNotes.cpp
2 #include <iostream>
3 #include <fstream>
4 using namespace std;
5
6 int main() {
7 ifstream f;
8 f.open("notes.txt");
9 char c;
10 //while(!f.eof()) {f >> c; cout << c;} // >> ne lit pas les espaces
11 while(!f.eof()) {f.get(c); cout << c;} // f.get(c) lit tout
12 f.close();
13 return 0;
14 }
Writing readNotes.cpp
1 !g++ readNotes.cpp
2 !./a.out
5
1
Aouzou Abdelhakim 12.5
Ben_Mouina Achraf 11
Arjdal Abderrahim 13.25
Labihi Abdelhamid 10
Sittaf Abdelghani 14.33
1 %%writefile readData.cpp
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5
6 using namespace std;
7
8 int main() {
9 ifstream f;
10 int m, n;
11 f.open("notes.txt");
12 f >> m >> n;
13 string nom[m], prenom[m];
14 float note[m];
15 for(int i = 0; i < m; i++)
16 f >> nom[i] >> prenom[i] >> note[i];
17 f.close();
18 cout << "Nombre d'étudiants: " << m << endl
19 << "Nombre de notes par étudiant: " << n << endl;
20 for(int i = 0; i < m; i++)
21 cout << nom[i] << " " << prenom[i] << '\t'
22 << note[i] << endl;
23 return 0;
24 }
Writing readData.cpp
1 !g++ readData.cpp
2 !./a.out
Nombre d'étudiants: 5
Nombre de notes par étudiant: 1
Aouzou Abdelhakim 12.5
Ben_Mouina Achraf 11
Arjdal Abderrahim 13.25
Labihi Abdelhamid 10
Sittaf Abdelghani 14.3
La note minimale et l'étudiant qui l'a obtenue (nécessite une variable imin),
La note maximale et l'étudiant qui l'a obtenue (nécessite une variable imax),
La moyenne des m notes (nécessite une variable som).
1 %%writefile smps4.hpp
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5
6 using namespace std;
7
Writing smps4.hpp
1 %%writefile processData.cpp
2 #include "smps4.hpp"
3
4 int main() {
5 ifstream f("notes.txt"); // <==> {ifstream f; f.open("notes.txt");}
6 int m, n, imin = 0, imax = 0;
7 f >> m >> n;
8 string nom[m], prenom[m];
9 float note[m], som;
10 for(int i = 0; i < m; i++) {
11 f >> nom[i] >> prenom[i] >> note[i];
12 som += note[i];
13 if(note[i] < note[imin]) imin = i;
14 else if(note[i] > note[imax]) imax = i;
15 }
16 cout << "Nombre d'étudiants: " << m << endl
17 << "Nombre de notes par étudiant: " << n << endl;
18 for(int i = 0; i < m; i++)
19 cout << nom[i] << " " << prenom[i] << '\t'
20 << note[i] << endl;
21 cout << "Note minimale: " << note[imin] << " ("
22 << nom[imin] + " " + prenom[imin] << ")" << endl;
23 cout << "Note maximale: " << note[imax] << " ("
24 << nom[imax] + " " + prenom[imax] << ")" << endl;
25 cout << "Moyenne des " << m << " notes: " << som/m << endl;
26 return 0;
27 }
Writing processData.cpp
1 %%bash
2 g++ processData.cpp
3 ./a.out
Nombre d'étudiants: 5
Nombre de notes par étudiant: 1
Aouzou Abdelhakim 12.5
Ben_Mouina Achraf 11
Arjdal Abderrahim 13.25
Labihi Abdelhamid 10
Sittaf Abdelghani 14.3
processData.cpp:1:10: fatal error: smps4.hpp: No such file or directory
#include "smps4.hpp"
^~~~~~~~~~~
compilation terminated.
1 !man rand
1 %%writefile smps4.hpp
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5
6 #include <stdlib.h>
7 using namespace std;
8
Writing smps4.hpp
Création du fichier
Comme pour la lecture à partir d'un fchier disque, l'écriture dans un fichier disque nécessite les
étapes suivantes:
1 %%writefile create2Ddata.cpp
2 // Création d'un fichier de m*n données artificielles
3 #include "smps4.hpp"
4
5 int main() {
6 string file;
7 cout <<"Nom du fichier à créer (sans extension): ";
8 cin >> file; file += ".smp";
9 ofstream g(file); // <==> { ofstream g; g.open(file); }
10 g << "SMPS4" << endl; // Ligne 1
11 int m, n, a, b;
12 cout << "Entrer m et n: "; cin >> m >> n;
13 cout << "Entrer a et b: "; cin >> a >> b;
14 g << m << " " << n << endl; // Ligne 2
15 g << a << " " << b << endl; // Ligne 3
16 // Boucles imbriquées pour l'écriture des m lignes suivantes
17 for(int i = 0; i < m; i++) {
18 for (int j = 0; j < n; j++)
19 g << a + (float)rand()/RAND_MAX*(b-a) << '\t';
20 g << endl;
21 }
22 g.close();
23 cout << "Création de " << file << " terminée." << endl;
24 return 0;
25 }
Writing create2Ddata.cpp
1 %%shell
2 g++ create2Ddata.cpp
3 ./a.out
1 !head mesures.smp
SMPS4
10 5
-50 100
76.0282 9.15744 67.4649 69.766 86.7471
-20.3673 0.283413 65.2344 -8.33379 33.0955
21.6096 44.3306 4.71767 27.0101 92.8345
87.4293 45.3568 57.5945 -28.7596 41.0453
-47.5549 -13.567 -29.4153 70.6265 -26.4981
10.1417 -30.5314 -33.6787 99.8387 -17.2615
26.9399 75.8668 41.896 -5.59526 45.6328
1 !tail mesures.smp
1 !wc mesures.smp
13 55 440 mesures.smp
1 !wc -l mesures.smp
13 mesures.smp
1 !./a.out
2
1 ls -lt *.smp
1 !head bigdata.smp
SMPS4
1000000 10
-10000 20000
15205.6 1831.49 13493 13953.2 17349.4 -4073.46 56.6826 13046.9 -1666.76
4321.91 8866.13 943.534 5402.03 18566.9 17485.9 9071.35 11518.9 -5751.92 8209
-9510.98 -2713.4 -5883.05 14125.3 -5299.63 2028.33 -6106.29
5387.97 15173.4 8379.2 -1119.05 9126.57 5728.62 4807.49 19183.3 -1224.5 13140
5802.35 13097.4 2006.86 16745.9 -1500.56 573.751 14231.7 17570.8 -7907.34
5779.86 -7418.32 -4233.58 9896.81 16707 466.788 -8074.86 -9399
-2851.6 19119 17066.2 15527.6 -2000.03 6192.81 1256.21 12807.5 5376.06 10031
1 !tail -5 bigdata.smp
1 !wc -l bigdata.smp
1000003 bigdata.smp
1
check 0 s terminée à 22:08