What's new

Some qs...

Doomulation

?????????????????????????
Ok, just wondering some things. Anyway,

1) Is there a good API to get the cdrom drive?
2) Is there a way to convert char to char*? As in the following illustrated sample:

PHP:
char drive = 'D';
char* temp = new char[3];
while (true)
{
  strcpy(drive,temp); // Compile error: cannot convert char to char*
  strcpy((char*)drive,temp); // No compile error, but data is lost
  strcpy((char*)&drive,temp); // Would this work?
  strcat(":\\",temp);
  drive++;
}

3) While I'm at it...has anyone experienced that the compiler/linker hangs sometimes and the stop build command fails?
 

aprentice

Moderator
Doomulation said:
Ok, just wondering some things. Anyway,

1) Is there a good API to get the cdrom drive?
2) Is there a way to convert char to char*? As in the following illustrated sample:

There is api to determine if the drive is a cdrom, but you cannot get detailed information on it, you need aspi for that.

Here is some code from one of my projects, darkengine, hope it helps.

PHP:
bitmask=GetLogicalDrives();
for(i=2;i<26;i++) //Start at C
{
      bit=bitmask>>i&0x1;
      if(bit==1) //drive present
      {
	sprintf(letter,"%c:\\",drive);
	if(GetDriveType(letter)!=DRIVE_CDROM) //if not cdrom
		drives[++b]=drive;
       }
	drive++;
}
drives[++b]='\0';
 

Teamz

J'aime tes seins
you mixed up the arguments of strcpy

the first argument is the destination and the 2nd argument is the string that you want to copy
 

euphoria

Emutalk Member
Doomulation said:
Ok, just wondering some things. Anyway,

2) Is there a way to convert char to char*? As in the following illustrated sample:

Code:
char drive = 'D';
char* temp = new char[3];
while (true)
{
  strcpy(drive,temp); // Compile error: cannot convert char to char*
  strcpy((char*)drive,temp); // No compile error, but data is lost
  strcpy((char*)&drive,temp); // Would this work?
  strcat(":\\",temp);
  drive++;
}
as teamz said you've mixed the destination and the source in the strcpy() funtion. The last one is correct, but you could also do it like this since you're only accessing one char:
temp[0] = drive;


3) While I'm at it...has anyone experienced that the compiler/linker hangs sometimes and the stop build command fails?
Atleast using DJGPP on win2000 the ntvdm.exe fails like 10% of the time compilation/linking. But VC++ 6.0 SP3 hasn't failed on me yet :plain:
 

ingonab

New member
Doomulation said:
2) Is there a way to convert char to char*? As in the following illustrated sample:
Answering only the question you asked and not pointing out any other problems with that code... :happy:

As the two other posters already mentioned, the arguments for strcpy() are back to front. :) You'll find that pretty much all the basic ANSI C functions put their parameters in the (destination, source) order.

Another, more serious, problem has to do with the assumption that you only need to convert from char to char*. The simple answer to the question is "&drive" (you don't need the typecast). BUT, doing this for an argument to strcpy() is wrong. The function strcpy() doesn't just want a char*, it wants a char* that points to a array of chars with the last element being 0x00 (a null terminating string, another words). &drive is just one char with no terminator, so strcpy() will overflow the array bounds while searching for the end of the string.

The safe answer is, you need a temporally buffer:

PHP:
char drive = 'D';
char buffer[2];

buffer[0] = drive;
buffer[1] = '\\0';
But in your example, you can just do:
PHP:
sprintf(temp, "%c:\\\\", drive);
Note: 'temp' needs to be at least FOUR chars in size (not three), to cater for the null terminator.


3) While I'm at it...has anyone experienced that the compiler/linker hangs sometimes and the stop build command fails?

Sometimes. Don't know what causes it though. I assume it's just MS's shoddy old IDE. (VC++ 6.0)
 
OP
Doomulation

Doomulation

?????????????????????????
Teamz: yeah i know i always mess them up...i'm used to it being the other way around. But it always go find when writing the code, so that's no real problem...

aprentice: those streams and shit used in situations like this is too complicated for me... :p i can't see what they do, really. I knew about the getdriveinfo api, though.

ingonab:
PHP:
char drive = 'D';
char buffer[2];

buffer[0] = drive;
buffer[1] = '\0';
Oh yeah, friggin' smart solution, i didn't think of that... :p

Sometimes. Don't know what causes it though. I assume it's just MS's shoddy old IDE. (VC++ 6.0)
Wonder if upgrading to a service pack would help...?
 

Top