@timmy_i_chen/

Extraction Donnees

C

No description

fork
loading
Files
  • main.c
  • blah.c
  • data.txt

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4048969110101086","path":"main.c","file":{"path":"main.c","content":{"asEncoding":{"base64":"LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKTHVrYSBMYWZheWUgZGUgTWljaGVhdXgKVFBFIDFTCkRpbWFuY2hlLCAyMCBKYW52aWVyIDIwMTkKU2NyaXB0IEMgcGVybWV0dGFudCBkJ2V4dHJhaXJlIGxlcyBkb25uZWVzIGNvbGxlY3RlZXMgYSBwYXJ0aXIgZHUgUmFzcGJlcnJ5IFBpIDNCKwoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiNpbmNsdWRlIDxhc3NlcnQuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBaRVJPIDAKI2RlZmluZSBPTkUgMQojZGVmaW5lIEZJVkUgNQojZGVmaW5lIENIQUlORV9WSURFICIgIgojZGVmaW5lIExPTkdVRVVSX1JFU1VMVCAzMAojZGVmaW5lIExPTkdVRVVSX0xJR05FIDIyCiNkZWZpbmUgTk9NX0ZJQ0hJRVIgImRhdGEudHh0IgojZGVmaW5lIE9QVElPTl9MSVJFICJyIgoKI2RlZmluZSBKQU4gIjAxIgojZGVmaW5lIERFQyAiMTIiCgojZGVmaW5lIERFQlVUX0pPVVIgNAojZGVmaW5lIEZJTl9KT1VSIDUKI2RlZmluZSBERUJVVF9NT0lTIDcKI2RlZmluZSBGSU5fTU9JUyA5CiNkZWZpbmUgREVCVVRfQU5ORUUgMjQKI2RlZmluZSBGSU5fQU5ORUUgMjcKI2RlZmluZSBERUJVVF9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RlYyI7CgljaGFyICpwdHJDaGFpbmUxMiA9IGNoYWluZTEyOwoJY2hhciAqbW9pczEyID0gbnVtZXJvTW9pcyhwdHJDaGFpbmUxMiwgMCk7Cglhc3NlcnQobW9pczEyW1pFUk9dID09ICcxJyk7CiAgYXNzZXJ0KG1vaXMxMltPTkVdID09ICcyJyk7Cn0="},"asBuffer":null},"loaded":true}}
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
/****************************************************************************
Luka Lafaye de Micheaux
TPE 1S
Dimanche, 20 Janvier 2019
Script C permettant d'extraire les donnees collectees a partir du Raspberry Pi 3B+
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define ZERO 0
#define ONE 1
#define FIVE 5
#define CHAINE_VIDE " "
#define LONGUEUR_RESULT 30
#define LONGUEUR_LIGNE 22
#define NOM_FICHIER "data.txt"
#define OPTION_LIRE "r"

#define JAN "01"
#define DEC "12"

#define DEBUT_JOUR 4
#define FIN_JOUR 5
#define DEBUT_MOIS 7
#define FIN_MOIS 9
#define DEBUT_ANNEE 24
#define FIN_ANNEE 27
#define DEBUT_TEMP 5
#define FIN_TEMP 8
#define DEBUT_HUM 21
#define FIN_HUM 24

#define DEBUT_TEMP_RESULT 11
#define FIN_TEMP_RESULT 14

#define DEBUT_HUM_RESULT 16
#define FIN_HUM_RESULT 19

char *numeroMois(char *chaine, int position);
void extraireChaine(char *resultat, char *ligne, char separateur, int debut, int fin);
void testNumeroMois();

int main(int argc, char *argv[]) {
  FILE *fichier = fopen(NOM_FICHIER, OPTION_LIRE); // Ouvrir le fichier data.txt

  if (fichier != NULL) { // Si le fichier n'est pas vide continuer
		char ligne[LONGUEUR_RESULT]; // Copie de chaque ligne de data.txt (chaine)
		char lignePrecedente[FIVE]; // Sauvegarde les cinq premiers caracteres de chaque ligne de data.txt (chaine)
    char resultat[LONGUEUR_RESULT] = CHAINE_VIDE; // Contient le resultat avec date en lettres (Jan)
		char resultatFinal[LONGUEUR_RESULT-1]; // Contient le contenu de chaque ligne affichee pour Excel
    char *p = resultat; // Pointeur vers resultat
		int i = ZERO;
    int resultOffset = ZERO;

		while (fgets(ligne, sizeof ligne, fichier) != NULL) {
      int decalage = ZERO; // Nombre d'espaces decalant les valeurs vers la droite
	  	
			// Check for leading spaces.
			for(int i = 0; i < (sizeof(ligne) / sizeof(ligne[0])); i++) {
				if (ligne[i] != ' ') break;
        decalage++;
			}

			// S'assurer qu'il n'y a une difference entre la ligne precedente et la nouvelle
			if (strstr(ligne, lignePrecedente) != NULL) continue;

			// Extraire la temperature et l'humidite d'une ligne de data.txt et placer les valeurs aux bons emplacements dans la chaine resultat (avec p) 
			if (strstr(ligne, "Temp") != NULL) { // Si la ligne commence avec Temp, proceder
        extraireChaine(p + resultOffset, ligne, ' ', DEBUT_TEMP + decalage, FIN_TEMP + decalage); // Extraire la temperature en tentant compte des espaces en debut de ligne (decalage)
        resultOffset += FIN_TEMP - DEBUT_TEMP + 2;

				extraireChaine(p + resultOffset, ligne, '\0', DEBUT_HUM + decalage, FIN_HUM + decalage); // Extraire l'humidite en tentant compte des espaces en debut de ligne (decalage)
        resultOffset += FIN_HUM - DEBUT_HUM + 2;
			} else { //  Extraire la date d'une ligne de data.txt et placer les valeurs aux bons emplacements dans la chaine resultat (avec p)

        extraireChaine(p + resultOffset, ligne, '/', DEBUT_JOUR + decalage, FIN_JOUR + decalage);  // Extraire le jour
        resultOffset += FIN_JOUR - DEBUT_JOUR + 2;

			  extraireChaine(p + resultOffset, ligne, '/', DEBUT_MOIS + decalage, FIN_MOIS + decalage); // // Extraire le mois
        resultOffset += FIN_MOIS - DEBUT_MOIS + 2;

				extraireChaine(p + resultOffset, ligne, ' ', DEBUT_ANNEE + decalage, FIN_ANNEE + decalage); // Extraire l'annee
        resultOffset += FIN_ANNEE - DEBUT_ANNEE + 2;

				char *mois = numeroMois(ligne, DEBUT_MOIS + decalage);
				
				for (i=0; i < LONGUEUR_RESULT-1; i++) {
						if (i < 3) {
							resultatFinal[i] = resultat[i];
						}
						if (i >= 3) {
						resultatFinal[i] = resultat[i+1];
					  }
				}
				resultatFinal[3] = mois[0];
				resultatFinal[4] = mois[1];
			}
      
      // Regler les differences de lignes de data.txt
      strncpy(lignePrecedente, ligne, 4);
      
      printf("%s\n", resultatFinal);

      if (resultOffset == LONGUEUR_LIGNE) {
        p = resultat;
        resultOffset = 0;
      }
		}

		fclose(fichier);  // Fermer le fichier
  } else {
    perror(NOM_FICHIER); // Afficher l'erreur d'ouverture
  }

  return EXIT_SUCCESS;
}

char *numeroMois(char *chaine, int position) {
	char *mois = malloc(sizeof(char) * 2);
	if (chaine[position] == 'J') {
		mois = JAN;
	} else {
		mois = DEC;
	}
	return mois;
}

void extraireChaine(char *resultat, char *ligne, char separateur, int debut, int fin) {
	for(int i=debut; i <= fin; i++) {
		*resultat++ = ligne[i];
	}
  *resultat = separateur;
}

void testNumeroMois() {
	char chaine1[3] = "Jan";
	char *ptrChaine1 = chaine1;
	char *mois1 = numeroMois(ptrChaine1, 0);
	assert(mois1[ZERO] == '0');
  assert(mois1[ONE] == '1');

	char chaine12[3] = "Dec";
	char *ptrChaine12 = chaine12;
	char *mois12 = numeroMois(ptrChaine12, 0);
	assert(mois12[ZERO] == '1');
  assert(mois12[ONE] == '2');
}