Thread: little help with a code

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    16

    little help with a code

    Code:
       int readfile(FILE *ifp, struct employee list[]){ 
    
       //define ints  
        char LastName[MAX_LEN];
        char FirstName[MAX_LEN];
        int clockinhr = 0, clockinmin = 0, clockouthr = 0, clockoutmin = 0, weekEntries = 0;
        int j, k, l, index;
        int num = 0, dataset;
        
        // read in the number of employees
        fscanf(ifp, "%d", &num);
    
        // Loops to the amount of employees    
        for (index=0 ; index < num; index++) {
        
        
          //This reads in the first and last name and there pay per hour
          fscanf(ifp, "%s", list[index].first);
          fscanf(ifp, "%s", list[index].last);
          fscanf(ifp, "%lf", &list[index].payperhr);
        }
        
        //Reads in the data set
        fscanf(ifp, "%d", &dataset);
        
        // This will loop through the data set  
        for (j = 0; j < dataset; j++){
          
          
           //Reads in each week
           fscanf(ifp, "%d", &weekEntries);
           
            for (k = 0; k < weekEntries; k++){
                
                //This will take in the employees first and last name and the time they clocked in and out
                fscanf(ifp, "%s %s %d %d %d %d", LastName, FirstName, &clockinhr, &clockinmin, &clockouthr, &clockoutmin);
                
                //Runs through each employee
                for (index =0; index < num; index++){
                   
                    //if (( strcmp(LastName, list[index].last) == 0 ) && ( strcmp(FirstName, list[index].first) == 0 ) ){
    
                     
                        //calculates total hours for the week
                        list[index].hours_in_week += ((clockouthr - clockinhr) + ((clockoutmin - clockinmin)/60.00)); 
                       //  }
    
                         printf(" %lf ", list[index].hours_in_week);
    
                        //This will take care of over time
                        if (list[index].hours_in_week > 40){
                           
                           //Calculates amount of overtime
                           list[index].gross += (1.50*list[index].payperhr)*(list[index].hours_in_week - 40.00) + (list[index].payperhr * 40);
                           
                           //Calculates taxes on the overtime
                           list[index].taxes += (.20 * ((1.50 * list[index].payperhr) * (list[index].hours_in_week - 40.00))) + (.10 * 40); 
                        }
                        
                        list[index].gross += list[index].payperhr * list[index].hours_in_week;
                        list[index].taxes += 0.10 * list[index].payperhr * list[index].hours_in_week;
                        
                        
                    //resets the hours back to zero
                    list[index].hours_in_week = 0;
                    
                }
            }    
        }
        
        return num;
    }
    for some odd reason the damn thing wont sum the list[index].hours_in_week before it goes onto the next if statement any help would be great

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    since i dunno the actual content of your file. I suspect the value read from the file are not right. What do u get printed at this state

    Code:
    printf(" &#37;lf ", list[index].hours_in_week);
    And i will also check the value of clockoutmin , clockinmin after they have been read from the file, just then and see what do get. And perhaps do a manual cal see what you get.

    ssharish

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use fscanf for reading strings, please! It's unsafe. Use fgets instead.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No it's not, you can prevent potential buffer overflows by limiting the length of the string (&#37;1024s for example). You'll still most likely use sscanf() on the fgets() string, which is basically the same thing.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    scanf is safe when reading non-strings I believe, since it won't do a buffer overflow.
    I don't know which to recommend scanf with &#37;1024s (or similar) or fgets.
    fgets read the entire row and won't leave out anything, but scanf with %1024s will probably read an entire line either. Hmmm. Matter of choice, maybe?

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Nah, I'd go with fgets(). But I was saying, you can avoid buffer overflows with fscanf() by limiting the amount you read

    With fgets() say you had a line with a max of 255 characters, and it contains 2 strings you want to parse, you know the max length of either of those strings is 255 characters, hence no buffer over run, eg:

    Code:
    char line[256];
    char string1[256], string2[256];
    
    fgets(line, sizeof(line), fp);
    /* parse line into string1 and string2 ... */

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The entire *scanf family of functions isn't so useless as one might initially think. The *scanf functions are for reading formatted data such as condensed files or a string or something, not for user input which is for the most part unformatted.

    scanf has it's issues because it reads from the keyboard usually, but it has uses too. And when you want to read a specific string such as %1024[^0123456709\t \n\v] *scanf can be inconvenient, but it "works" in the here and now.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So long as you limit the max amount of characters it reads, it's OK. Many tend to NOT do so, while such is mandatory with fgets.
    (Although the new runtime library scanf_s requires that you specify buffer size too.)

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    The entire *scanf family of functions isn't so useless as one might initially think. The *scanf functions are for reading formatted data such as condensed files or a string or something, not for user input which is for the most part unformatted.

    scanf has it's issues because it reads from the keyboard usually, but it has uses too. And when you want to read a specific string such as %1024[^0123456709\t \n\v] *scanf can be inconvenient, but it "works" in the here and now.
    Yes, I agree with that. The problem with "user input" is that there are too many ways that *scanf can be confused, and a simple mistake from the user [e.g typing a letter as part of a number] can derail it instantly, and prevent it from ever working again if you don't make extra efforts to detect such situations and rectify them.

    This is different from reading files that you generate by code. This is fine to read back in with *scanf.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf is safe when reading non-strings I believe, since it won't do a buffer overflow.
    But none of the scanf numeric conversions protect from arithmetic underflow (or overflow).

    To get that functionality, you need strtol() and strtod(), which ties in nicely with using fgets() to read the line to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So the safest way is simply to use fgets to read anything, then to strtol or strtod to convert to numeric, unless it's a string.

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. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  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