Thread: linking file stream with input file?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    linking file stream with input file?

    ok i was given an assignment but i have no clue how to do this. can someone please explain to me what i'm supposed to do cause i'm really lost. this is the prompt.

    The sales commission for each salesperson at Cars-Inc. is computed
    based upon the following percentage of his/her total sales:

    Total Sales (in US$) Commission
    __________________________________________________ _
    sale <= 35000 5%
    35000 < sale <= 60000 7%
    60000 < sale <= 90000 9%
    90000 < sale 11%

    Define the file stream *infile and link it to the given input file
    "sales.dat" which is in the homework directory. Use
    while(fgets()!=NULL) to read all the lines in "sales.dat". For odd
    number lines (containing names) print the name on the screen. For
    even number lines (with sales), sscanf &sale and calculate the
    corresponding commission by using the above table; then print the sale
    and the commission on the screen.
    Finally, find the maximum value of the total sales. (You do not have to
    print the name of the best sales person since you need pointers to save
    the names.)

    Note: Order the conditional expressions so that you do not have to use
    the logical operators, || or &&. Specifically, if(35000<sale<=90000)
    is incorrect since the expression consists of two conditional
    expressions: 35000<sale and sale<=90000. The two expressions must be
    joined by || or &&.
    .................................................. .................
    Your output on the screen should look like:

    John Everhart
    sales= 75900.00 commission= 6831.00
    Paul Evans
    sales= 95700.00 commission= 10527.00
    ...........
    ......................................
    Steven King
    sales= 68000.00 commission= 6120.00
    Anne Rice
    sales= 130000.00 commission= 14300.00

    The maximum sales value is .....


    i'm really lost at:
    Define the file stream *infile and link it to the given input file
    "sales.dat" which is in the homework directory. Use
    while(fgets()!=NULL) to read all the lines in "sales.dat". For odd
    number lines (containing names) print the name on the screen. For
    even number lines (with sales), sscanf &sale and calculate the
    corresponding commission by using the above table; then print the sale
    and the commission on the screen. i opened the file sales.dat and this is what it was:

    sales.dat

    John Everhart
    75900
    Paul Evans
    95700
    Juan Hernandez
    45100
    Rwanda Jackson
    88500
    Steve Jhonson
    35800
    Henry Chen
    105000
    Tracy Ravad
    56000
    Scott Peters
    98000
    Alex Ma
    89800
    Alma Roberts
    34500
    Nelson Shah
    33800
    Steven King
    68000
    Anne Rice
    130000

    can anyone help?

  2. #2
    Code Abuser
    Join Date
    Jan 2009
    Posts
    16
    That just means opening a file with fopen() and setting a file pointer to the resulting address. Don't forget to close it when you're done.

    http://www.cplusplus.com/reference/c...dio/fopen.html
    http://www.cplusplus.com/reference/c...dio/fgets.html
    http://www.cplusplus.com/reference/c...io/fclose.html

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by flamehead144 View Post

    i'm really lost at:
    Define the file stream *infile and link it to the given input file
    "sales.dat" which is in the homework directory. Use
    while(fgets()!=NULL) to read all the lines in "sales.dat". For odd
    number lines (containing names) print the name on the screen. For
    even number lines (with sales), sscanf &sale and calculate the
    corresponding commission by using the above table; then print the sale
    and the commission on the screen. i opened the file sales.dat and this is what it was:

    can anyone help?
    In other words, what you want to do is use the fopen function to open the file sales.dat. fopen (you should consult your documentation for full details) returns a FILE * (file pointer) this is handy, because 'Define the file stream *infile' means that you want to define a FILE type pointer 'infile' -- if fopen returns a file pointer, then you can assign that to a pointer, in your case FILE * infile. Once you get all of this, then you should be able to open the file (with fopen), and then read the contents of the file, line by line with fgets. You can read a bit more about this type of thing in the FAQ.
    Last edited by kermit; 02-07-2009 at 06:50 PM.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    would it look something like this?
    Code:
    #include <stdio.h>
    main()
    {
            FILE *infile; 
            char text[81];
            int count=0;
            int tsales;
            infile = fopen("sales.dat", "r");
            if(infile == NULL){
             exit(1);
             }
            while(fgets(text, 81, infile)!=NULL){ 
                    count++;        
                    if(count%2 == 0){
                      sscanf(text, "%d", &tsales);
                      printf("tsales = $%d\n", tsales);
                    } else
                    printf("%s", text);
            }
            printf("There are %d lines.\n", count);
            fclose(infile);
    }
    when i a.out this nothing comes out.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Hmmmm

    This may seem like a silly question, but is the file sales.dat in the same directory where you execute a.out?

    As an aside, and of lesser importance, main returns an int. You should also #include <stdlib.h> for the call to exit. Are you compiling with gcc? If so, use the -Wall -Wextra flags. The compiler will tell you about these sorts of things when you use them.
    Last edited by kermit; 02-07-2009 at 06:57 PM.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Quote Originally Posted by kermit View Post
    Hmmmm

    This may seem like a silly question, but is the file sales.dat in the same directory where you execute a.out?

    As an aside, and of lesser importance, main returns an int. You should also #include <stdlib.h> for the call to exit. Are you compiling with gcc? If so, use the -Wall -Wextra flags. The compiler will tell you about these sorts of things when you use them.
    no, the sales.dat is in a different directory. the file i'm creating is in my home directory and to go to the sales.dat i have to type cd ../public and type more sales.dat to view it, which is what is confusing me. and yes, i compile with gcc.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well you can either copy sales.dat to your current directory from the command line (this assumes that you run the cp command from the directory where your executable program exists) :

    Code:
    cp ../public/sales.dat .
    Or you can change the call to fopen to search for sales.dat in the proper directory:

    Code:
    infile = fopen("../public/sales.dat", "r");
    Unless you tell it otherwise, the program will search for the file in the current directory, which will be the directory where you executed the program from.
    Last edited by kermit; 02-07-2009 at 08:01 PM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    ok i changed the infile = fopen so the program searches for the file and when i compile and a.out, it lists:
    John Everhart
    75900
    Paul Evans
    95700
    Juan Hernandez
    45100
    Rwanda Jackson
    88500
    Steve Jhonson
    35800
    Henry Chen
    105000
    Tracy Ravad
    56000
    Scott Peters
    98000
    Alex Ma
    89800
    Alma Roberts
    34500
    Nelson Shah
    33800
    Steven King
    68000
    Anne Rice
    130000

    how would i get it to print the names in the odd lines and calculate commission for the even lines?

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You are almost there now with your output. Compare what you have with the example output from the assignment, and you will see there is little more to do. Take 'John Everhart,' for example: Print his name (because it is odd numbered). The next line will be even, so you want to get the number on that line into a variable. Once you have it in the variable, you can test it to see how much commission is to be applied. In this case, it 75900, which is between 60000 and 90000 dollars, so his commission would be 9%. You then need to multiply the value 75900 by 9% - remember that integer arithmetic discards any fractional parts (decimals) so you will end up with a round number. If you want the decimal part, you need to use float or double. Then you just need to print the result to the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM