Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone spot the NULL pointer assignment

Status
Not open for further replies.

AndyDoyle

Programmer
Jan 31, 2003
5
US
Code:
/*502796P4 - Final Report*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
//#define COMPUTEACH
#define UPDATED &quot;502796nf.dat&quot;
#define MAST_SIZE sizeof(struct MASTER_REC)

void printHeaders(void);

FILE *fp,*u_ptr;

struct MASTER_REC{
	char cust_code[6];
	char cust_name[21];
	char cust_addr[61];
	float cust_bal;
	long credit_limit;
	int year,month,day;
	};

#ifdef COMPUTEACH
	#define PRINTER &quot;PRN&quot;
	#define MAXLINES 30
	#include &quot;printer.h&quot;
#else
	#define PRINTER &quot;Final Report.dat&quot;
	#define MAXLINES 50
#endif

void main()
{
	int over=0,recs=0,lines=0;
	float TotalVal=0,TotalBal=0;
	struct MASTER_REC cust;
	double value=0;

	#ifdef COMPUTEACH
		reset(fp);
		set_graph(fp);
	#endif

	/*Open disk file (New master file)*/
	if ((u_ptr = fopen(UPDATED,&quot;rb&quot;)) == NULL)
	{
		fprintf(stdout,&quot;\nError in opening new master&quot;);
		exit(1);
	}

	/*Open printer file (This is for The report)*/
	if ((fp = fopen(PRINTER,&quot;w&quot;)) == NULL)
	{
		fprintf(stdout,&quot;\nError in opening printer file&quot;);
		exit(1);
	}

	printHeaders();

	fread(&cust,MAST_SIZE,1,u_ptr);
	recs++;

	while (!feof(u_ptr))
	{
		if (cust.cust_bal > cust.credit_limit)
				value = cust.cust_bal - cust.credit_limit;
		else
				value = 0;

		if (value > 0)
			{
				fprintf(fp,&quot;\n  %s&quot;,cust.cust_code);
				fprintf(fp,&quot;        %10.2f&quot;,value);
				TotalVal = TotalVal + value; //Show limit value
				over++;
			}

		lines++;

		if (lines>MAXLINES)
		{
			fputc('\f\n', fp);
			lines=0;
			printHeaders();
		}
		recs++;

		TotalBal = TotalBal + cust.cust_bal;

		fread(&cust,MAST_SIZE,1,u_ptr);
	}

	fprintf(fp,&quot;\n\nRecords processed > %d\n&quot;, recs);
	fprintf(fp,&quot;Records over Credit limit > %d\n&quot;, over);
	fprintf(fp,&quot;Excess Credit > %.2f\n&quot;,TotalVal);
	fprintf(fp,&quot;Grand total balance > %.2f\n&quot;,TotalBal);

	fclose(fp);
	fclose(u_ptr);
}

void printHeaders()
{
	#ifdef COMPUTEACH
		bold (fp,ON);
	#endif
	fprintf(fp,&quot;\n%s %s\t\tZenith Paints&quot;,__TIME__,__DATE__);
	fprintf(fp,&quot;\n\t\t       Customers who exceed credit limit\n&quot;);
	fprintf(fp,&quot; Customer       Excess Balance\n&quot;);
	fprintf(fp,&quot;   Code\n&quot;);
	#ifdef COMPUTEACH
		bold (fp,OFF);
	#endif
}
 
>> fprintf(fp,&quot;\n %s&quot;,cust.cust_code);

since your using fread() to read the entire block into your &quot;cust&quot; structure there is no null terminator at the end of any of your char array members. Therefore if you pass them as &quot;char*&quot; to any string functions that operate off the null terminator your application looks like the desert in Iraq, KABOOM!

Maybe that's it. Maybe not i didn't set it all up and run it. But of course you did so by now you already know this yes?

-pete

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top