0

well im writing a program where i saved some employee names and proffessions on a file , and i did another file where ive put the proffesions and its salary , and i wanna make a program which write every employee info and add it it its proper salary , so i went to use an fread instruction to open the employee file , then while reading employe info , i did another fread that read from the salary file , to at the end print the proper salary for that employee , and on and on , but its not working , it gives me nothing , only employee infos without their salary , thanks for helping

code:

include <stdio.h>
#include <stdlib.h>
typedef struct { int d,m,y; } dtype ;

typedef struct { char nom[10]; char prenom[10] ; char poste[20]; dtype date ; } LEM ;

typedef struct { char postes[20]; int salaire ; } LP ;



int main()
{

    FILE * F;
    F= fopen("C:/Users/ipoga/Desktop/BUNCH OF STUFF/TP ALGO/auto/employés.db","r" );
    LEM R ;


    FILE * E;
    E= fopen("C:/Users/ipoga/Desktop/BUNCH OF STUFF/TP ALGO/auto/poste.db","r" );
    LP O  ;


    fread(&R , sizeof(R),1,F);
    while(!feof(F))
    {

     


        printf("nom : %s \t",R.nom);
        printf("prenom : %s \t",R.prenom);
        printf("poste : %s \t",R.poste);
        printf("date de rec : %d/%d/%d \n",R.date.d , R.date.m ,R.date.y);


                                fread(&O , sizeof(O),1,E);
                                while(!feof(E))
                                {

                                if(O.postes == R.poste)
                                {
                                    printf( "salaire : %d ",O.salaire);
                                }

                                fread(&O , sizeof(O),1,E);
                                }


        fread(&R , sizeof(R),1,F);



    }
 

}
 



Lyes Ye
  • 9
  • 3
  • This code won't compile. Please make a [mcve]. – Ted Lyngmo Aug 07 '20 at 23:04
  • 1
    Where are the `fopen` statements? – Thomas Matthews Aug 07 '20 at 23:04
  • 1
    This question's shown code does not meet stackoverflow.com's requirements for a [mre]. This means it's unlikely that anyone here can conclusively answer the question; but only guess at the most. You should [edit] your question to show a minimal example, no more than one or two pages of code (the "minimal" part), that everyone else can cut/paste, compile, run, and reproduce the described issue (the "reproducible" part) ***exactly as shown*** (this includes any ancillary information, like the input to the program). See [ask] for more information. – Sam Varshavchik Aug 07 '20 at 23:04
  • sorry for the one who responded i made a mistake , i meant that i used fread instruction not fopen , i edited the ost , thanks again and sorry – Lyes Ye Aug 07 '20 at 23:09
  • 1
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Aug 07 '20 at 23:10
  • You need to completely restructure your loop. Yes, you can call `fread` in multiple locations. No, you cannot use `feof` to control your loops. – William Pursell Aug 07 '20 at 23:11
  • `employés.db` are you sure you got the encoding for the file name right? Special characters in file names can be a pain.... As for you primary issue, you will need to seek in the salary file to reset back to the beginning in case it isn't in same order, or read from both in the same loop. Also, `strcmp` instead of pointer comparisons for C style strings. – Ext3h Aug 07 '20 at 23:16

1 Answers1

0

O.postes is a char[]. For a C style string, comparison should not be done with == (which compares the addresses, not the content) but with strcmp() from <string.h> instead.

Ext3h
  • 5,713
  • 17
  • 43
Ben
  • 149
  • 1
  • 6
  • yes its char , i re edited the code to show the whole code , thanks – Lyes Ye Aug 07 '20 at 23:15
  • Then despite the down vote on my answer I think i'm right. if(O.postes == R.poste) can't work, you should use strcmp() – Ben Aug 07 '20 at 23:18