Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

message au Christian ASTOR

1 réponse
Avatar
programmation
Bonjour,

Christian ASTOR, vous avez donner une solution dans le sujet nomm=E9
"traitement de fichier"
http://groups.google.fr/group/fr.comp.os.ms-windows.programmation/browse_th=
read/thread/a253d400f7f3286a/61b1a577ad0eaf?hl=3Dfr&q&pli=3D1
vous dites :

Pour identifier les colonnes ne contenant que des "0", tu peux faire
une
premi=E8re 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 =E9gal au nombre
de
lignes, c'est qu'il n'y a que des "0" dans la colonne donn=E9e.
Ensuite tu lis ton tableau de structures et tu supprimes les "0" pour
chaque position de ton attribut correspondant au compteur pr=E9c=E9dent
lorsque le compteur est =E9gal au nombre de lignes ("if (nColumn[i] =3D=3D
nCpt)" ci-dessous)

Par exemple =3D>

#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] =3D {0}, nCpt =3D 0, nAttribLength=3D 0,
nMaxAttribLength =3D 0;
FILE *pFile, *pFileOut;
pFile =3D fopen("test.txt","r");
pFileOut =3D fopen("test2.txt","w");
int dataTabStringMax =3D STEP;
dataStruct *dataTabString;
if (pFile && pFileOut)
{
dataTabString =3D (dataStruct *) malloc(dataTabStringMax
*
sizeof(dataStruct));
while (fgets(sLine, MAXLINE, pFile))
{
strncpy(dataTabString[nCpt].sNum, sLine, 5);
dataTabString[nCpt].sNum[5] =3D '\0';
nAttribLength =3D strlen(sLine)-6;
if (nAttribLength>0)
{
strncpy(dataTabString[nCpt].sAttrib,
&sLine[5], nAttribLength);
=20
dataTabString[nCpt].sAttrib[nAttribLength] =3D '\0';
for (int i =3D 0; i<nAttribLength; i++)
{
if (dataTabString->sAttrib[i]
=3D=3D '0')
nColumn[i]+=3D1;
}
nCpt+=3D1;
if (nAttribLength > nMaxAttribLength)
nMaxAttribLength =3D
nAttribLength;
}
if (nCpt =3D=3D dataTabStringMax)
{
dataTabStringMax +=3D STEP;
dataTabString =3D (dataStruct *)
realloc(dataTabString,
dataTabStringMax *
sizeof(dataStruct));
if (dataTabString =3D=3D NULL)
exit(1);
}
}
// Lecture tableau de structures pour supprimer les
"0"
for (int j =3D 0; j<nCpt; j++)
{
int nShift =3D 0;
for (int i =3D 0; i<nMaxAttribLength; i++)
{
if (nColumn[i] =3D=3D nCpt)
{
=20
memmove(&dataTabString[j].sAttrib[i-nShift],
&dataTabString[j].sAttrib[i-nShift+1],
strlen(&dataTabString[j].sAttrib[i-nShift+1]) + 1);
nShift+=3D1;
}
}
if (strcspn (dataTabString[j].sAttrib, "1") <
strlen(dataTabString[j].sAttrib))
fprintf(pFileOut, "%s%s\n",
dataTabString[j].sNum,
dataTabString[j].sAttrib);
}

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


- Pouvez vous me d=E9tailler encore =E9tape par =E9tape votre solution ?

- Que pr=E9sentent les deux variables de deux boucles for : nCpt et
nMaxAttribLength ?

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


Merci.

1 réponse

Avatar
programmation
Bonjour,

Voici le lien original de cette solution:

http://groups.google.fr/group/fr.comp.os.ms-windows.programmation/browse_thread/thread/a253d400f7f3286a/61b1a577ad0eaf?hl=fr&q=#0061b1a577ad0eaf