'Find and edit function in file in C
Helps me please thx for advance.
I want two function to search and edit
I want search function via "grade" and change line with "code"
eg. when I type 1 (ID= code). the program must search if the value 1 exists and asks me to modify the name all the other information except the 'code' in my file.
Second for search when i type CEO (grade value). The program checks if this value exists in my file and shows me the line and all the lines where it found the value with all the information code name first name etc...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TMAX 1000
typedef struct employees employees;
struct employees
{
char code[10];
char nom[20];
char prenom[20];
int salaire;
char grade[20];
};
void ajoutEmployees(employees employee);
void afficherEmployees(employees employee);
void supprimer(employees employee);
void rechercher(employees employee);
int main()
{
employees test;
int choix;
int tmp;
printf("\n\n\n\n\n\n\n");
printf("\t \t \t $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$ \n");
printf("\t \t \t $$ To add an employee .............. 1 \n");
printf("\t \t \t $$ To display the list of employees ..... 2 \n");
printf("\t \t \t $$ To modify an employee .............. 3 \n");
printf("\t \t \t $$ To delete an employee .............. 4 \n");
printf("\t \t \t $$ To search for an employee .............. 5 \n");
printf("\t \t \t $$ To quit the program .............. 0 \n");
printf("\t \t \t $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$ \n");
printf("\n");
printf("\t \t \t Faites votre choix : ");
scanf("%d", &choix);
switch (choix)
{
case 1:
do
{
printf("\n\n");
printf("\t \t \t Ajouter les coordonnees de l'employes \n");
ajoutEmployees(test);
printf("\n \t \t \t Voulez vous ajouter encore ? (1 oui | 0 non) : ");
scanf("%d", &tmp);
} while (tmp == 1);
break;
case 2:
printf("\n\n");
printf("\t \t \t La liste des employes \n");
printf("\n\n");
printf("\t\t\t ****************************");
afficherEmployees(test);
printf("\n\n");
printf("\t\t\t ****************************");
break;
case 4:
do
{
printf("\n\n");
printf("\t \t \t Ici vous allez supprimer un employee : \n");
supprimer(test);
printf("\n \t \t \t Voulez vous ajouter encore ? (1 oui | 0 non) : ");
scanf("%d", &tmp);
} while (tmp == 1);
break;
case 5:
printf("OKO");
rechercher(test);
break;
}
}
void ajoutEmployees(employees employee)
{
FILE *fichier = NULL;
fichier = fopen("employes.txt", "a");
if (fichier == NULL)
{
exit(1);
}
fseek(fichier, 0, 2);
printf("\t \t \t Entrer le code : ");
scanf("%s", &employee.code);
printf("\t \t \t Entrer le nom : ");
scanf("%s", &employee.nom);
printf("\t \t \t Entrer le prenom : ");
scanf("%s", &employee.prenom);
printf("\t \t \t Entrer le salaire : ");
scanf("%d", &employee.salaire);
printf("\t \t \t Entrer le grade : ");
scanf("%s", &employee.grade);
fprintf(fichier, "\n %s %s %s %d %s ", employee.code, employee.nom, employee.prenom, employee.salaire, employee.grade);
fclose(fichier);
}
this is my function...
void afficherEmployees(employees employee)
{
FILE *fichier = NULL;
fichier = fopen("employes.txt", "r");
char contener[TMAX];
if (fichier == NULL)
{
exit(1);
}
fseek(fichier, 0, 1);
while (fgets(contener, TMAX, fichier) != NULL)
printf("\t\t\t %s ", contener);
fclose(fichier);
}
void supprimer(employees employee)
{
FILE *fichier = NULL;
FILE *tmp = NULL;
fichier = fopen("employes.txt", "r");
tmp = fopen("tmp.txt", "w");
char contener[TMAX];
int test = 0;
if (fichier == NULL || tmp == NULL){ exit(1); }
char codeEntre[10];
printf("\t \t \t Entre le code de l'employe que vous voulez supprimer : ");
scanf("%s", &codeEntre);
do{
fscanf(fichier, "%s %s %s %d %s ", &employee.code, &employee.nom, &employee.prenom, &employee.salaire, &employee.grade);
if (strcmp(codeEntre, employee.code) == 0){
test = 1;
}else{
fprintf(tmp, "\n %s %s %s %d %s ", employee.code, employee.nom, employee.prenom, employee.salaire, employee.grade);
}
} while (!feof(fichier));
if (test == 1){
printf("\t \t \t L'employe a bien ete supprimer \n");
}else{
printf("\t \t \t Le code entre n'existe pas dans le fichier ");
}
fclose(fichier);
fclose(tmp);
remove("employes.txt");
rename("tmp.txt", "employes.txt");
}
void rechercher(employees employee)
{
FILE *in_file = fopen("employes.txt", "r");
if (in_file == NULL){exit(-1);}
printf("\t \t \t Veuillez entrer un mot (entrez 0 pour terminer)\n");
scanf("%s", employee.grade);
printf("\t \t \t Nous avons trouvé le mot %s dans le fichier (nbre) fois\n", employee.grade);
fclose(in_file);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
