-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvf.cpp
169 lines (168 loc) · 5.38 KB
/
csvf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*****************************************\
|* Code C++ pour lire un fichier CSV. *|
|* *|
|* Auteur : Alnotz . *|
|* Note : On peut surement faire mieux ! *|
\*****************************************/
/*g++ -Wall -Wextra -c csvf.cpp && g++ -Wall -Wextra csvf.o -o csvf*/
/* Bibliotheques standards de C++ 11. */
#include <iostream>// Entree/sortie.
#include <fstream>// Fichiers.
#include <cstring>// Caracteres.
#include <cstdlib>// Controles de procedures.
#define LINES 8// Lignes.
#define ROWS 6// Colonnes.
/* Fonction secondaire. */
/* Un texte d'aide pour connaître les commandes. */
/* Une seule commande ici : '-h' pour l'aide. X-) */
void helper(void)// Que du texte... :-|
{
std::cout << "/*********************************************\\" << std::endl;
std::cout << "|* Section d'aide pour le programme 'csvf'. *|" << std::endl;
std::cout << "|* *|" << std::endl;
std::cout << "|* csvf [-h|--help] *|" << std::endl;
std::cout << "|* *|" << std::endl;
std::cout << "\\*********************************************/" << std::endl;
std::cout << std::endl;
std::cout << "Pour manipuler ce programme, il suffit d'avoir";
std::cout << " le fichier 'csvin.csv' dans le meme repertoire." << std::endl;
std::cout << std::endl;
std::cout << " Contenu de 'csvin.csv' :" << std::endl;
std::cout << std::endl;
std::cout << "\"X\",\"Y\",\"Z\",\"U\",\"V\",\"P\"" << std::endl;
std::cout << "1,1,1,1,1,1" << std::endl;
std::cout << "2,2,2,2,2,2" << std::endl;
std::cout << "3,3,3,3,3,3" << std::endl;
std::cout << "4,4,4,4,4,4" << std::endl;
std::cout << "5,5,5,5,5,5" << std::endl;
std::cout << "6,6,6,6,6,6" << std::endl;
std::cout << "7,7,7,7,7,7" << std::endl;
std::cout << "8,8,8,8,8,8" << std::endl;
std::cout << std::endl;
std::cout << "Apres ca, on execute 'csvf'."<< std::endl;
}
/* Fonction principale. */
int main(int argc, char** argv)
{
/* Avant de commencer, a-t-on ajoute une option ? */
if (argc == 2)// si une option...
{
/* En cas d'option '-h' ou '--help'... */
if (strcmp(argv[1],"--help")==0 || strcmp(argv[1],"-h")==0)
{
/* Page d'aide invoquee. */
helper();
/* Fin du programme avec succes. */
exit(EXIT_SUCCESS);
}
}
/* Texte memorise. */
char text[LINES+1][5*ROWS-1];
/* Indices ligne/colonne. */
char i,j;
/* Matrice d'entiers. */
int matrix[LINES][ROWS];
/* Pour s'assurer la fin de chaque ligne.
* Une ligne = une sous-chaine.
*/
for (i=1; i<ROWS; i++)
{
text[i][5*ROWS-1-1]='\0';
}
/* Pour la deco. car j'aime ca. :-D */
std::cout << "/*****************************************\\"
<< std::endl;
std::cout << "|* Code C++ pour lire un fichier CSV. *|"
<< std::endl;
std::cout << "|* *|"
<< std::endl;
std::cout << "|* Auteur : Frederic Henry. *|"
<< std::endl;
std::cout << "|* Note : On peut surement faire mieux ! *|"
<< std::endl;
std::cout << "\\*****************************************/"
<< std::endl;
std::cout << std::endl;
/* Lecture de fichier texte avec "std::ifstream". */
std::cout << "Lecture de \"./csvin.csv\"..." << std::endl;
std::ifstream entree("csvin.csv");// Pointeur de fichier.
std::cout << "Table CSV (en-tete) :" << std::endl;
/* Ligne 0 lue. */
entree >> text[0];
std::cout << text[0] << std::endl;
/* Les autres lignes. */
std::cout << "Table CSV (corps) :" << std::endl;
for (i=1; i<LINES+1; i++)
{
entree >> text[i];
std::cout << text[i] << std::endl;
}
/* Lecture integrale. */
std::cout << "Table CSV (tout) :" << std::endl;
for (i=0; i<LINES+1; i++)
{
std::cout << text[i] << std::endl;
}
/* Fermeture du fichier. */
entree.close();
std::cout << std::endl;
/* Affichage du tableau de caracteres. */
std::cout << "Version tableau de caracteres :" << std::endl;
/* En-tete. */
for (j=0; j<ROWS; j++)
{
std::cout << text[0][4*j+1] << ' ';
}
/* Corps. */
std::cout << std::endl;
for (i=1; i<LINES+1; i++)
{
for (j=0; j<ROWS; j++)
{
std::cout << text[i][2*j] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
/* Pareil, facon nombres entiers. */
std::cout << "Version tableau d'entiers :" << std::endl;
/* Conversion du texte a la matrice entiere. */
for (i=0; i<LINES; i++)
{
for (j=0; j<ROWS; j++)
{
matrix[i][j]=(int)text[i+1][2*j]-48;
}
}
/* Affichage de la matrice. */
for (i=0; i<LINES; i++)
{
for (j=0; j<ROWS; j++)
{
std::cout << matrix[i][j] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
/* Ce coup-ci, ecriture de tableau dans un fichier. */
std::cout << "Ecriture d'un tableau CSV \"./csvout.csv\"..." << std::endl;
std::ofstream sortie("csvout.csv");
/* Dabord l'en-tete. */
sortie << "\"X\",\"Y\",\"Z\",\"U\",\"V\",\"P\"" << std::endl;
/* En suite le corps. */
for (i=0; i<LINES; i++)
{
for (j=0; j<ROWS-1; j++)
{
sortie << matrix[i][j] << ',';
}
sortie << matrix[i][ROWS-1] << std::endl;
}
/* Fichier ferme. */
sortie.close();
std::cout << std::endl;
/* Fini. */
std::cout << "Fin" << std::endl;
/* Pour dire que tout a bien marche. */
return EXIT_SUCCESS;
}