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 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int COMPTER_L(char *file_path);
struct buffer *RECHERCHER_LIGNES(char CHAINE[], char PATH[], int L);
int RECHERCHER_CH(char str1[], char str2[]);
int COMPTER_CH(char str[]);
void AFFICHER_RES(int a, struct buffer x);
int main()
{
char path[] = "C:\\Sauvegarde_B\\Fichiers_txt\\ROUSSEAU.txt";
char chaine[] = "promeneur";
RECHERCHER_LIGNES(chaine, path, COMPTER_L(path));
}
struct buffer *RECHERCHER_LIGNES(char CHAINE[], char PATH[], int L)
{
int a;
int count1; /* count1 représente le numéro de la ligne à laquelle nous sommes dans le fichier */
int count1bis = 0;
int find = 0;
int NBR = 0;
struct buffer { /* Buffer temporaire pour stocker des lignes de fichiers */
char BUFF_lignes[256];
};
struct buffer list1[L-1]; /* Initialisation et déclaration */
struct buffer *ptr = &list1[0];
int fin_fichier;
FILE *fp;
if ((fp = fopen(PATH, "r")) != NULL)
{
count1 = 0;
do
{
count1++;
fgets(list1[count1].BUFF_lignes, sizeof(list1[count1].BUFF_lignes), fp);
printf("%s", list1[count1].BUFF_lignes); /* Le buffer contient successivement chaque ligne du fichier en cours de lecture */
if (!(fin_fichier = feof(fp))) /* Si la fin du fichier n'est pas détectée... */
{
if ((RECHERCHER_CH(list1[count1].BUFF_lignes, CHAINE)) != 0) /* Si on trouve la chaîne recherchée... */
{
find = 1;
count1bis = count1;
}
}
} while (!fin_fichier);
printf("FIND = %d\n", find);
system("pause");
/* Lorsque la fin d'un fichier est détectée, on le ferme */
if (find == 1)
{
AFFICHER_RES(count1bis, list1[L-1]);
printf("\nVoulez-vous modifier une ligne ? (tapez le numéro de la ligne pour oui, 0 pour non) > ");
scanf("%d", &NBR);
fflush(stdin);
if (NBR != 0)
{
printf("En construction !\n");
fclose(fp);
}
if (NBR == 0)
{
fclose(fp);
}
}
fflush(stdin);
}
for (a = 0; a < L; a++)
{
printf("%s", list1[a].BUFF_lignes);
}
system("pause");
}
/* Fonction : CHERCHE_L
* But : Compter le nombre de lignes dans un fichier texte.
* Argument : file_path[] -> chemin d'accès du fichier.
* Return : LIGNES = nombre de lignes dans le fichier si le fichier existe, sinon L = 0.
*-------------------------------------------------------------------------------------*/
int COMPTER_L(char *file_path) /* Compte le nombre de ligne d'un fichier : renvoie le nombre de lignes ou -1 si échec de l'ouverture */
{
int LIGNES = 0;
int fin_fichier;
char buffer[256];
char *ptr = &buffer[0];
FILE *fp;
if ((fp = fopen(file_path, "r")) != NULL)
{
do
{
fgets(buffer, sizeof(buffer), fp); /* On enregistre la première ligne du fichier dans le buffer... */
LIGNES = LIGNES+1; /* on incrémente L de 1 à chaque fois que fgets récupère une ligne */
if (fin_fichier = feof(fp)) /* si on atteint la fin du fichier... */
{
return(LIGNES);
fclose(fp); /* on renoie le nombre de lignes, sinon on continue */
}
free(ptr); /* On libère la mémoire du buffer avant d'enregistrer la ligne suivante */
} while (!fin_fichier); /* sinon on continue */
}
else /* s'il y a une erreur à l'ouverture du fichier, on renvoie LIGNES = 0 */
{
return(LIGNES);
}
}
int RECHERCHER_CH(char str1[], char str2[])
{
int a;
int i = 0;
int j = 0;
int taille_str1 = COMPTER_CH(str1);
int taille_str2 = COMPTER_CH(str2);
for (a = 0; a < taille_str1; a++)
{
if (str1[a] == str2[0] || str1[a] == str2[0]+32 || str1[a] == str2[0]-32)
{
do
{
/* printf("STR1 : %c => STR2 : %c\n", str1[a], str2[j]);
printf("STR1 : %c => STR2 : %c\n", str1[a], str2[j]+32);
printf("STR1 : %c => STR2 : %c\n", str1[a], str2[j]-32);
printf("a = %d, j = %d\n", a, j); */
a++;
j++;
}
while ((str1[a] == str2[j] || str1[a] == str2[j]+32 || str1[a] == str2[j]-32) && j < taille_str2);
if (str1[a-1] == str2[j-1] || str1[a-1] == str2[j-1]+32 || str1[a-1] == str2[j-1]-32)
{
if (j == taille_str2)
{
return(1);
}
}
}
else
{
j = 0;
}
}
return(0);
}
int COMPTER_CH(char str[])
{
int x = 0;
while (str[x] != '\0')
{
x++;
}
return(x);
}
void AFFICHER_RES(int a, struct buffer x)
{
printf("\n");
printf("%d - %s", a, list1[a].BUFF_lignes);
printf("%d - %s", a+1, list1[a+1].BUFF_lignes);
printf("%d - %s", a+2, list1[a+2].BUFF_lignes);
printf("%d - %s", a+3, list1[a+3].BUFF_lignes);
printf("%d - %s", a+4, list1[a+4].BUFF_lignes);
fflush(stdout);
} |
Partager