Thread: trouble with file pointers...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    21

    trouble with file pointers...

    Hi, all--this time I've got a program that's supposed to calculate passenger efficiency in passenger kilometers per liter, given an input file that lists number of passengers, total commuting distance per week, and gasoline consumed per week. I'm not sure what's going on, but it doesn't seem to be reading the input file properly. Here's the program:

    Code:
    #define SENTINEL 0
    int main()
    {
    	FILE *inp, *outp;
    	double min_efficiency, num_passengers, commute, gas_used, efficiency, subsidy;
    	printf("Please enter the minimum efficiency> ");
    	scanf("%lf", &min_efficiency);
    	inp=fopen("carpool.txt", "r");
    	outp=fopen("effic.txt", "w");
    	fprintf(outp, "%87s\n", "CARPOOLS MEETING MINIMUM PASSENGER EFFICIENCY OF 25 PASSENGER KM / L");
    	fprintf(outp, "%-16s%-21s%-20s%-19s%-10s\n", "Passengers", "Weekly Commute", "Gasoline", "Efficiency", "Weekly");
    	fprintf(outp, "%-16s%-21s%-20s%-19s%-10s\n", " ", "(km)", "Consumption(L)", "(pass km / L)", "Subsidy($)");
    	fscanf(inp, "%lf%lf%lf", &num_passengers, &commute, &gas_used);
    	while (num_passengers!=SENTINEL)
    	{
    		efficiency=num_passengers*commute/gas_used;
    		subsidy=.08*num_passengers*commute;
    		if (efficiency>=min_efficiency)
    		{
    			fprintf(outp, "%-16d%-21.0lf%-20.1lf%-19.1lf%-10.2lf\n", num_passengers, commute, gas_used, efficiency, subsidy);
    		}//if efficiency>=min_efficiency
    		fscanf(inp, "%lf%lf%lf", &num_passengers, &commute, &gas_used);
    	}//while num_passengers != 0
    	fclose(inp);
    	fclose(outp);
    	return 0;
    }//main
    And here's the output I get on running it:
    Code:
    teyla:~ quasigreat$ cat carpool.txt
    4 75 11.0
    2 50 9.0
    5 95 15.5
    2 60 4.5
    4 35 4.3
    6 70 20.7
    0 0 0
    
    teyla:~ quasigreat$ ~quasigreat/a.out
    Please enter the minimum efficiency> 25
    teyla:~ quasigreat$ cat effic.txt
                       CARPOOLS MEETING MINIMUM PASSENGER EFFICIENCY OF 25 PASSENGER KM / L
    Passengers      Weekly Commute       Gasoline            Efficiency         Weekly    
                    (km)                 Consumption(L)      (pass km / L)      Subsidy($)
    0               0                    0.0                 (it puts a random huge number here, which I've 
                                                                 removed to save your browser window)      
    0               0                    0.0                 (here, too)     
    0               0                    0.0                 -0.0               0.00      
    0               0                    0.0                 0.0                (and here)
    I'm probably making a really basic mistake somewhere, but I can't for the life of me figure it out. If someone could point out what I'm doing wrong, I'd be eternally grateful.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Your logic is shot, you use the uninitialized variables THEN you read their values from a file.

    Code:
    double min_efficiency, num_passengers, commute, gas_used, efficiency, subsidy;
    Is where your huge number comes from.

    The logic should be,
    Code:
    OPEN FILE
    
    WHILE reading from file DO
        CALC stuff
        PRINT stuff
    END WHILE
    
    CLOSE FILE
    ie
    Code:
    FILE * out, * in;
    
    /* ... */
    
    while(fscanf(in, ...) != EOF)
    {
        if(...)
        {
            fprintf(out, ...);
        }
    }
    Last edited by zacs7; 05-18-2008 at 10:00 PM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    21
    Hmm. I'm not sure I understand. Just before the while loop begins, there's this line:
    Code:
    fscanf(inp, "%lf%lf%lf", &num_passengers, &commute, &gas_used);
    Is there some reason I'm not aware of that this wouldn't read from the file into those variables? And efficiency and subsidy are both assigned before the if statement that prints them. I did try your suggestion to use "while (fscanf(inp, "%lf%lf%lf", &num_passengers, &commute, &gas_used)!=EOF)", and it gave me one line of 0s. Could you perhaps clarify a little further?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    OK, you declared num_passengers as a double and then print it as an int. Unless you need to account for the possibility of a serial killer carrying hacked-up bodies, or perhaps a hearse or medical examiner's van, you should consider changing num_passengers to an int. Don't forget to change the corresponding scanf formatters.

    If you must keep it as a double, cast the argument in fprintf to (int).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    21
    Thanks so much, that's fixed it. Eternal gratitude, coming right up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM