GNT sans publicité, site mobile, fonctionnalitées exclusives...

message au Christian ASTOR

Le
programmation
Bonjour,

Christian ASTOR, vous avez donner une solution dans le sujet nommé
"traitement de fichier"
http://groups.google.fr/group/fr.co...browse_th=
read/thread/a253d400f7f3286a/61b1a577ad0eaf?hl=fr&q&pli=1
vous dites :

Pour identifier les colonnes ne contenant que des "0", tu peux faire
une
première lecture du fichier pour mettre les valeurs (num + attribut)
dans un tableau de structures et utiliser un tableau d'entier pour
compter le nombre de '0' total par colonne : si c'est égal au nombre
de
lignes, c'est qu'il n'y a que des "0" dans la colonne donnée.
Ensuite tu lis ton tableau de structures et tu supprimes les "0" pour
chaque position de ton attribut correspondant au compteur précédent
lorsque le compteur est égal au nombre de lignes ("if (nColumn[i] ==
nCpt)" ci-dessous)

Par exemple =>

#include <tchar.h>
#include <iostream>

#define MAXLINE 1024
#define STEP 10

typedef struct dataStruct
{
char sNum[6];
char sAttrib[32];

} dataStruct;

int _tmain(int argc, _TCHAR* argv[])
{
char sLine[MAXLINE];
int nColumn[32] = {0}, nCpt = 0, nAttribLength= 0,
nMaxAttribLength = 0;
FILE *pFile, *pFileOut;
pFile = fopen("test.txt","r");
pFileOut = fopen("test2.txt","w");
int dataTabStringMax = STEP;
dataStruct *dataTabString;
if (pFile && pFileOut)
{
dataTabString = (dataStruct *) malloc(dataTabStringMax
*
sizeof(dataStruct));
while (fgets(sLine, MAXLINE, pFile))
{
strncpy(dataTabString[nCpt].sNum, sLine, 5);
dataTabString[nCpt].sNum[5] = '\0';
nAttribLength = strlen(sLine)-6;
if (nAttribLength>0)
{
strncpy(dataTabString[nCpt].sAttrib,
&sLine[5], nAttribLength);

dataTabString[nCpt].sAttrib[nAttribLength] = '\0';
for (int i = 0; i<nAttribLength; i++)
{
if (dataTabString->sAttrib[i]
== '0')
nColumn[i]+=1;
}
nCpt+=1;
if (nAttribLength > nMaxAttribLength)
nMaxAttribLength =
nAttribLength;
}
if (nCpt == dataTabStringMax)
{
dataTabStringMax += STEP;
dataTabString = (dataStruct *)
realloc(dataTabString,
dataTabStringMax *
sizeof(dataStruct));
if (dataTabString == NULL)
exit(1);
}
}
// Lecture tableau de structures pour supprimer les
"0"
for (int j = 0; j<nCpt; j++)
{
int nShift = 0;
for (int i = 0; i<nMaxAttribLength; i++)
{
if (nColumn[i] == nCpt)
{

memmove(&dataTabString[j].sAttrib[i-nShift],
&dataTabString[j].sAttrib[i-nShift+1],
strlen(&dataTabString[j].sAttrib[i-nShift+1]) + 1);
nShift+=1;
}
}
if (strcspn (dataTabString[j].sAttrib, "1") <
strlen(dataTabString[j].sAttrib))
fprintf(pFileOut, "%s%s",
dataTabString[j].sNum,
dataTabString[j].sAttrib);
}

free(dataTabString);
fclose(pFile);
fclose(pFileOut);
}
return 0;
}


- Pouvez vous me détailler encore étape par étape votre solution ?

- Que présentent les deux variables de deux boucles for : nCpt et
nMaxAttribLength ?

- quelle est le rôle de la fonction memmove() ?


Merci.
Lire la réponse

Questions / Réponses high-tech
Vidéos High-Tech et Jeu Vidéo
Téléchargements
Vos réponses
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
programmation
Le #21552211
Bonjour,

Voici le lien original de cette solution:

http://groups.google.fr/group/fr.co...a577ad0eaf
Publicité
Suivre les réponses
Poster une réponse
Anonyme