Thread: Israeli politics mixed with C

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Wink Israeli politics mixed with C

    Check this one out:
    I have 3 files: one is current parliament members, second is previous parliament members, and third file is a general current parties list in parliament.
    Need to:
    A. Find the new parliament members out of 2 first files & write it to a fourth file, which will be created. In other words: who is shown in the current parliament file & is not shown in previous parliament file.
    B. Display on screen the list of parties & the number of new parliament members belong to it.
    this is my code:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    #define READ "rb"
    #define WRITE "wb" 
    
    typedef struct{
    	char Pname[10];
    	char Fanme[10];
    	char address[20];
    	long ID;
    	int code;
    }PrlmntMbr;
    PrlmntMbr prev[4]=   //initializing with real names 
    	{				//of parliament members in Israel
    	
    		{"Yigal",  "Alon",   "st.6 50 Tel-Aviv", 2283475, 12},
    		{"Shula",  "Aloni",  "st.5 34 Herzelia", 1934624, 11},
    		{"Dan" ,   "Meridor","st.3 2 Jerusalem", 14932845,18},
    		{"Matan",  "Vilnai", "st.2 5 Mev. Zion", 3854486, 12} 
    	};
    
    PrlmntMbr crnt[4]=
    	{
    		{"Dan",    "Meridor","Rhv3 2 Jerusalem", 14932845,18},
    		{"Reshef", "Chen",   "Rehov9 136 Haifa", 25341903,10},
    		{"Yechiel","Hazan",  "Rehov1 79 Hadera", 1393472, 18},
    		{"Matan",  "Vilnai", "Rehov2 5 Mev.Zion",3854486, 12}
    	};
    
    
    typedef struct{
    	char PartyNmae[10];
    	int code;
    }Parties;
    	Parties par[4]=		//initializing with real parties in Israel
    	{
    		{"Avoda",   12},
    		{"Merez",   11},
    		{"Likud",   18},
    		{"shinui",  10},
    	};
    
    
    FILE *OpenFile(char *name, char *mode)			//open files function
    {
    	FILE *fp;
    
    		if ((fp=fopen (name, mode))==NULL)
    	{
    		fprintf(stderr, "eror opening file %s\n", name);
    		exit(1);
    	}
    	return fp;
    }
    
    int main()
    {
    	FILE *FPNewMbr, *FPprev, *FPcrnt, *FParty;
    
    	long code[100]={0};
    	int i=1,
    		j=1,
    		size;
    	char  FNamePrev[]="prev.txt", FNameCrnt[]="crnt.txt", FNameNew[]="newmbr.txt", FPrt[]= "parties.txt";
    	
    	FPprev=OpenFile(FNamePrev, WRITE);
    	fwrite (prev, sizeof(PrlmntMbr), 4, FPprev);
    
    	FPcrnt=OpenFile(FNameCrnt, WRITE);	
    	fwrite (crnt, sizeof(PrlmntMbr), 4, FPcrnt);
    
    	FParty=OpenFile(FPrt, WRITE);	
    	fwrite (par, sizeof(Parties), 4, FParty);
    
    	fclose (FPcrnt);
    	fclose (FPprev);
    	fclose (FParty);
    
    	FPprev=OpenFile(FNamePrev, READ);	
    	FPcrnt=OpenFile(FNameCrnt, READ);	
    	FPNewMbr=OpenFile(FNameNew, WRITE);
    	FParty=OpenFile(FPrt, READ);
    	
    	size=sizeof (PrlmntMbr);
    
    	
    	//Section A
    	fread (prev, size, 1, FPprev);
    	fread (crnt, size, 1, FPcrnt);
    	while (i<120 && j< 120)			//searching algorithm
    	{
    		while (crnt->ID==prev->ID && !feof (FPprev) && fread (prev, size, 1, FPprev)
    			&& ! feof (FPcrnt) && fread (crnt, size, 1, FPcrnt))
    		{
    			i++;
    			j++;
    		}
    		while (crnt->ID < prev->ID && !feof (FPcrnt) && fwrite (crnt, size, 1, FPNewMbr))
    		{
    			j++;
    			fread (crnt, size, 1, FPcrnt);
    		}
    		while (crnt->ID > prev->ID && !feof (FPprev) && fread (prev, size, 1, FPprev))
    		{
    			i++;
    		}
    	} 
    	
    	while (j++<120) //writing to new file
    	{
    		if (crnt->ID == prev->ID)
    			fwrite(crnt, size, 1, FPNewMbr);
    			fread (crnt, size, 1, FPcrnt);
    	}
    	fclose (FPprev);
    	fclose (FPNewMbr);
    	rewind (FPcrnt);
    
    
    //section B
    while (!feof(FPcrnt) && fread (crnt, size, 1, FPcrnt))   //searching algorithm
    	code[crnt->code]++;							
    printf ("      party name  number of representatives \n");
    printf ("==============================================");
    while (!feof(FParty) && fread (par, sizeof(Parties), 1, FParty))
    	   if  (code [par->code])
    		   printf ("%15s %10d \n",par->PartyNmae, par->code);
    	   fclose (FParty);
    return (0);
    }
    The thing is I do not get any output: not in out put file newmbr.txt, nor screen.
    help will be appreciated!
    Last edited by ronenk; 10-17-2004 at 12:13 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    At 71 posts, you have no excuse for using void main.

    >The thing is I do not get any output
    Well debugging this should be fairly easy since you know where your program tries to write output. Go there by stepping through a debug session and see why nothing is getting printed.
    My best code is written with the delete key.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    //section B
    while (!feof(FPcrnt) && fread (crnt, size, 1, FPcrnt))   //searching algorithm
    	code[crnt->code]++;
    Not so much to do with your question perhaps, but informative nonetheless.

    ~/

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Hang on a sec! Don't just leave me here...
    I'm not special in debuging either.
    Debugger shows I stuck in infinite loop with ID 25341903 in the while loop of section A.
    cannot figure out why.




    while (!feof(FPcrnt) && fread (crnt, size, 1, FPcrnt)) //searching algorithm
    code[crnt->code]++;

    Not so much to do with your question perhaps, but informative nonetheless.
    OK. thanx! meanhile this is the tool I was given by the teacher, so I have to use it & not other things I havn't learned yet...
    Last edited by ronenk; 10-17-2004 at 12:19 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cannot figure out why
    Obviously all of your conditions are failing for the nested loops, so i and j are never incremented. Once again, step through the code with a debugger and see what the values of your variables are, what the return values of your function calls are, and see what input is causing the problem. If you don't have a fancy debugger, or even a simple minded one, then you can scatter printf calls all over creation and get the info that way.

    >this is the tool I was given by the teacher
    Get a new teacher then, or inform him of his mistake. If he still insists that you use it, do so, but remember that it's wrong.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using FSCANF to read mixed text/double files
    By cfdprogrammer in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 03:18 AM
  2. Replies: 11
    Last Post: 08-20-2005, 05:50 AM
  3. Should the US stop funding the Israeli military?
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 55
    Last Post: 12-28-2002, 05:46 PM
  4. stuff
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 01:40 AM