Pointer Error

Started by
1 comment, last by zackriggle 21 years, 3 months ago
Alright, this is REALLY pissing me off... Please tell me why this creates errors:
  
//

// int2hex takes a LONG and converts it to hex in ASCII

//

// mqarc - an archive

// mqarc.files - a vector with the members:

//  start, end, name

//

//

	char* buf;
	buf = new char [6];
	for(i = 0; i != mqarc.files.size(); i++)
	{
		// Get file size

		fbuf.open(mqarc.files[i].name);
		fbuf.seekg(0,ios::end); // Get position of last char (total length)


		// Enter vars....

		mqarc.files[i].start = last + 1;
		mqarc.files[i].end = mqarc.files[i].start + fbuf.tellg();
		last = mqarc.files[i].end;

		fbuf.close();

		// Write vars....

		fout << mqarc.files[i].name << endl;
			// Convert the decimal-length to a hex-length in ASCII and write

			strcpy(buf,int2hex(mqarc.files[i].start));
			fout << buf << endl;
			strcpy(buf,int2hex(mqarc.files[i].end));
			fout << buf << endl;
	};
	delete[] buf;
  
================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice --
Advertisement
What are the errors?

Before I couldn''t spell engineer, now I are one!
--Before I couldn't spell engineer, now I are one!
Hi ..

yes, what errors anyway

this is the internal x to ascii helper function from the ms-crt
if you have msvc++ installed look at VC98\CRT\SRC\XTOA.C

setting radix to 16 puts out HEX ..


   void xtoa (unsigned long val,   // value           char *buf,           // outputbuffer           unsigned radix,      // 10 decimal,2 binary, a.s.o           int is_neg)          // != 0 when negative value{        char *p;                /* pointer to traverse string */        char *firstdig;         /* pointer to first digit */        char temp;              /* temp char */        unsigned digval;        /* value of digit */        p = buf;        if (is_neg) {            /* negative, so output ''-'' and negate */            *p++ = ''-'';            val = (unsigned long)(-(long)val);        }        firstdig = p;       /* save pointer to first digit */        do {            digval = (unsigned) (val % radix);            val /= radix;       /* get next digit */            /* convert to ascii and store */            if (digval > 9)                *p++ = (char) (digval - 10 + ''a'');/* a letter */            else                *p++ = (char) (digval + ''0'');     /* a digit */        } while (val > 0);        /* We now have the digit of the number in the buffer, but in reverse           order.  Thus we reverse them now. */        *p-- = ''\0'';            /* terminate string; p points to last digit */        do {            temp = *p;            *p = *firstdig;            *firstdig = temp;   /* swap *p and *firstdig */            --p;            ++firstdig;         /* advance to next two digits */        } while (firstdig < p); /* repeat until halfway */}    


if you only want to convert long then just modify it a little bit to remove the unnecessary parts ..

or use _itoa()(ms) itoa() (non-ms)
take a look at your crt-docs

This topic is closed to new replies.

Advertisement