Thread: rewritting code

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    rewritting code

    I would like to rewrite the first code sample into the form of the second sample and still get the same results. How would I do that?

    Code:
    while(fscanf(fp,"%s %s %s %s",info[numList].fname,
    	info[numList].lname,info[numList].phone,
    	info[numList].address)!= EOF || numList > MAX)
    {
    	numList++;
    }

    Code:
    while(what condition?)
    	{
    		fscanf(fp,"%s",[numList].fname);
    		fscanf(fp,"%s",info[numList].lname);
    		fscanf(fp,"%s",info[numList].phone);
    		fscanf(fp,"%s",info[numList].address)
    		numList++;
    	}

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I would like to rewrite the first code sample into the form of the second sample and still get the same results. How would I do that?
    Why?

    In your first sample, the fscanf() does all the work in one go, and is neatly tested for EOF. In the second sample, you are doing 4 seperate calls to fscanf(), none of which check for EOF, so you're bound to run into trouble at some point.

    There is a problem with the conditioning on the first sample though. You shouldn't be using this
    >|| numList > MAX;
    because it's an OR condition. This won't have the desired affect of stopping the loop when the array runs out of room, because the first condition (fscanf's return code) will still be true, and the loop will be processed. I'd suggest using
    Code:
    numList < MAX && fscanf......
    Also, reading directly into your target struct is not always good practice. It is sometimes better to read into a temporary buffer, validate what you read, then copy it to the struct.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Atleast to me having the four fscanf statements and a smaller while condition is easier to read than the complex while statement.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, Why not use fgets() in the while statement, to get a line into memory, then within the loop body, use sscanf().

    At least that way, you're only doing one file read.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM