Thread: while condition question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    while condition question..

    a cellphone company updates its data in a daily basis.
    the main file of all customers if updated till yesterday
    each line in this file has
    20 chars name
    9 chars Social security number
    10 chars cell phone number
    20 chars address
    2 chars type of customer
    8 chars date when the customer left the company
    the file is sorted by cellphone number in ascended order.

    each day the company makes another two files
    one file is the file of new customers or present customers which changed their type.
    regarding first file:

    the first line is the date of the last update on this file.
    20 chars name
    9 chars Social security number
    10 chars cell phone number
    20 chars address
    2 chars type of customer
    8 chars date when the customer left the company
    10 chars the name who wrote the update
    the file is sorted by cellphone number in ascending order.

    the second file is the list of people who left the company.
    regarding the second file:
    the first line is the date of the last update on this file.
    10 chars cell phone number
    10 chars the name of the man who wrote the update
    the file is sorted by cellphone number in ascending order.
    all the fields are sticked together without any space between them.


    write a function which which takes the 3 files and creates a new file
    which in the new main file.

    why they say while(flagOld==3 || flagNew==3)
    ??
    Code:
    void update(char *filename, FILE* fp1, FILE* fp2, char* newfilename)
    {
    	FILE *fOld, *fNew;
    	int flagOld, flagNew, flagDel;
    	char details1Old[39],details1New[30],telOld[11],telNew[11],telDel[11],details2Old[31],details2New[31],dateNew[9];
    		
    	if(!((fOld = fopen(filename, "r"))&&(fNew = fopen(newfilename,"w"))))
           {
    		puts(" The program cannot open a file");
    		exit(1);
    	}
    	
    	flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
    	fscanf(fp1,"%s%*c",dateNew);
    	flagNew= scanf(fp1,"%29[^$]%10s%30[^$]%11*c",details1New,telNew,details2New);
    	fscanf(fp2,"%9*c");			// Update date
    	flagDel= fscanf(fp2,"%10s%*11c",telDel);
    
    	while(flagOld==3 || flagNew==3)
            {
    		if(flagOld < 3 || (flagNew==3 && strcmp(telNew, telOld)<0)){			// New number
    			fprintf(fNew,"%s%s%s%s\n",details1New,telNew,details2New,dateNew);
    			flagNew= fscanf(fp1,"%29[^$]%10s%30[^$]%*11c",details1New,telNew,details2New);
    			continue;
    		}
    		if(flagDel==1 && !strcmp(telOld,telDel))
                   {							// Number to delete
    			flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
    			flagDel= fscanf(fp2,"%10s%*11c",telDel);
    			continue;
    		}
    		if(flagNew < 3 || strcmp(telOld, telNew)<0){			// No change for this number
    			fprintf(fNew,"%s%s%s\n", details1Old,telOld,details2Old);
    			flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
    			continue;		
    		}
    		fprintf(fNew,"%s%s%s%s\n",details1New,telNew,details2New,dateNew);	// Change of date
    		flagOld = fscanf(fOld,"%29[^$]%10s%38[^$]%*c",details1Old,telOld,details2Old);
    		flagNew= fscanf(fp1,"%29[^$]%10s%30[^$]%*11c",details1New,telNew,details2New);
    	}
    
    
    	if(fclose(fOld)==EOF || fclose(fNew)==EOF)
            {
    		puts(" The program cannot close a file");
    		exit(1);
    	}
    	
    }

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    basicly i understand the code

    i cant understand how the flag system works
    why it allways has to be equal 3
    ??

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    scanf returns the number of variables it successfully parsed out of the string. The code asks for 3, so a return value of 3 means all were successfully parsed.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Well you understand the code ..... why ask us to explain it?

    Given that the flags are both set as a return value from fscanf(), the meaning of the value 3 is pretty obvious. Just read the documentation of fscanf().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:32 AM
  2. Fmod question
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 05:26 PM
  3. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM