Thread: New to 'C' using fseek(), please help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    New to 'C' using fseek(), Any suggestions??

    I am writing a program that saves all of its input to a binary file. I have three entries: your code(1-5), date, and sale amount. I have the program storing everything in the file, but I need to access information by the code that it was entered under. The first function retrieves the corresponding name for the code entered and them adds up and prints the corresponding sales. Now I seem to have it working fine, but the second function I need to have the user enter the code the program retrieve the date along with price for that date, and if there is more that one date it will print them all with sales. I can get it to print all the dates and sales that were entered, but not just the ones for the code entered. I am trying to use fseek() but I am not too sure how to use it or even if it is what I should be using. Any advise would be awesome!!

    Here is the two functions that I mentioned above:

    Code:
    void total_sales( )
    {
    double totsales[6] = {0};
    double avg, tot = 0;
    sales rec;
    
    int i;
    FILE *file;
    
    
    file = fopen("sales.dat", "rb");
    	if (file == NULL) { message("Error opening file"); return;}
    		
    		
    		printf("\nTotal Sales Report\n\n");
    		printf("\n\nRealtor\t\t\t\t\tTotal Sales ($)\n\n");
    	
    	
    while (fread(&rec, sizeof(rec), 1, file))
    	
    	totsales[rec.code] += rec.sale;
    	for (i = 1; i < 6; i++)
    	printf("%s\t\t%22.2lf\n", *(name + i), *(totsales + i));
    	
    	fclose(file);
    	getaverage(totsales, &tot, &avg);
    	
    	printf("\n\nAverage = %29.2lf	", avg); 
    	printf("\nTotal = %31.2lf", tot); 
    	printf("\n\nPress Enter to continue");
    	getchar( );
    }
    
    /*======================================================*/
    void agent_sales( )
    {
    int i, cd, c, recnum, num, status;
    double totsales[6] = {0};
    long spot, last, count;
    char ch;
    sales rec;
    
    FILE *file;
    
    file = fopen("sales.dat", "rb");
    	if (file == NULL) { message("Error opening file"); return;}
    	
    	cd = getcode( );
    	i = cd;
    	printf("\n\nSales by %s\n\n", *(name + cd));
    	printf("Date\t\t\t\t\t      Sale\n");
    
    	//fseek(file, 0L, SEEK_END); // get current record count from file, assign to c
    	//c = (int)ftell(file) / sizeof(rec);
    	
    	while (fread(&rec, sizeof(rec), 1, file))
    	{
    		totsales[rec.code] = rec.sale;
    
    	printf("%s\t\t%17.2lf\n", (rec.date ) , *(totsales + i));
    	
    	}
    	fclose(file);
    	
    	printf("\n\nPress Enter to continue");
    	getchar( );
    	
    
    
    }
    Last edited by leopardforest@g; 05-09-2007 at 01:04 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Stated and written as a C programming question: moved to C Programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by laserlight View Post
    Stated and written as a C programming question: moved to C Programming.
    I dont quite understand? Am I in the wrong forum?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You posted this in the C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by laserlight View Post
    You posted this in the C++ programming forum.
    Oh I did not realize. Thanks.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >	while (fread(&rec, sizeof(rec), 1, file))
    >	{
    >		totsales[rec.code] = rec.sale;
    >
    >	printf("%s\t\t%17.2lf\n", (rec.date ) , *(totsales + i));
    >	
    >	}
    Make totsales a double:
    Code:
    double totsales = 0;
    And change the above to:
    Code:
    	while (fread(&rec, sizeof(rec), 1, file))
    	{
    		if (rec.code == cd)
    		{
    			totsales += rec.sale;
    		}
    	
    	}
    	printf("total sales: %f\n", totsales);

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by swoopy View Post
    Code:
    >	while (fread(&rec, sizeof(rec), 1, file))
    >	{
    >		totsales[rec.code] = rec.sale;
    >
    >	printf("%s\t\t%17.2lf\n", (rec.date ) , *(totsales + i));
    >	
    >	}
    Make totsales a double:
    Code:
    double totsales = 0;
    And change the above to:
    Code:
    	while (fread(&rec, sizeof(rec), 1, file))
    	{
    		if (rec.code == cd)
    		{
    			totsales += rec.sale;
    		}
    	
    	}
    	printf("total sales: %f\n", totsales);
    Thanks, but thats not quite what I am looking for.

    So if the user inputs this:

    Code:
    Enter your Code : 1            (This number also corresponds with a name array that works)
    
    Date: 05/09/07
    
    Sale: $500
    I need to be able to take that information that is mixed among others just like it but assigned to different codes in my binary file and then ask for the code and display it like so:

    Code:
    Enter your code: 1
    
    Date                       Sale
    05/09/07               500
    05/10/07                 30
    etc....
    I dont know how to link the date and sale with the code entered. What I keep doing makes the all the dates entered for every code display. Any guidance would be great!! Thanks

  8. #8
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    Can you post the file you are working with? That would help.

    I dont know how to link the date and sale with the code entered. What I keep doing makes the all the dates entered for every code display.
    Try a linked list?

    EDIT: Yay, post #123 . . .

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Then add the appropriate code to do what you want within here:
    Code:
    		if (rec.code == cd)
    		{
    			//Do something
    		}
    If you want to print out the date and sales, add a printf() to do that.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Sorry for doubting you in your suggestion. I think i see what you are try to say. This is my most current code and it seems to be giving the results that I need. Does it look like what you were thinking? Thanks!

    Code:
    /*======================================================*/
    void agent_sales( )
    {
    int i, cd, c, recnum, num;
    double totsales = 0;
    //long spot, last, count;
    //char ch;
    sales rec;
    
    
    
    FILE *file;
    
    file = fopen("sales.dat", "rb");
    	if (file == NULL) { message("Error opening file"); return;}
    	
    	cd = getcode( );
    	i = cd;
    	printf("\n\nSales by %s\n\n", *(name + cd));
    	printf("Date\t\t\t\t\t      Sale\n");
    
    		
    	while (fread(&rec, sizeof(rec), 1, file))
    	{
    		if (rec.code == cd)
    		{
    		totsales += rec.sale;
    		printf("%s\t\t%17.2lf\n", rec.date , totsales);
    		}
    	}
    	fclose(file);
    	
    	printf("\n\nPress Enter to continue");
    	getchar( );
    	
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. question about fseek
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 11-07-2008, 01:45 PM
  3. fseek question
    By blondie.365 in forum C Programming
    Replies: 19
    Last Post: 10-20-2008, 04:30 PM
  4. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  5. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 02:03 AM