You don't open it like a file, you treat it like an address in memory -- assign a pointer to it, and use it like that.
Here's a file I did, based on some code fragments I found. This enumerates the DOS LPT ports, (somewhat) detects the mode the port is in, and then gives you addresses of the data, status, and control ports. You can use outportb to write, and inportb to read. This is C++, as it uses streams, but it could very easily be converted to C just by changing the I/O. I hope this sample program helps.
Code:
unsigned long DATA = 0;
unsigned long CONTROL = 0;
unsigned long STATUS = 0;
void initPort(void);
void main(int, char**)
{
clrscr();
unsigned long addrLPT[3];
addrLPT[0] = *(unsigned long far *)0x00000408L; //get LPT1 address
addrLPT[1]= *(unsigned long far *)0x0000040AL; //get LPT2 address
addrLPT[2] = *(unsigned long far *)0x0000040CL; //get LPT3 address
unsigned int index, isPort = 0;
unsigned char usePort = 255;
cout << "Ports available for use:" << endl;
for (index = 0; index < 3; index++)
{
if (addrLPT[index] != 0){
cout << (index + 1) << ": LPT" << (index + 1) << endl;
isPort = 1;
}
}
if (isPort == 0)
{
cout << "No parallel ports found! Program terminating" << endl;
exit(1);
}
isPort = 0;
while(isPort == 0){
cout << "Please type the number (1-3) of the valid port you wish to test and press Enter: ";
cin >> usePort;
cout << endl;
if (usePort <= '3' && usePort >= '1')
{
if (addrLPT[(unsigned int)(usePort - '1')] != 0){
cout << "Port LPT" << usePort << " has been selected" << endl;
isPort = 1;
}
else cout << "That port is not available, please try a valid port." << endl;
}
else cout << "Bad input '" << usePort <<"', enter an integer between 1 and 3" << endl;
}
DATA = addrLPT[(unsigned int)(usePort - '1')];
cout << "Data port is at: " << hex << DATA << "h" << endl;
STATUS = DATA + 1;
CONTROL = DATA + 2;
cout << "Status port is at: " << hex << STATUS << "h" << endl;
cout << "Control port is at: " << hex << CONTROL << "h" << endl;
initPort();
}
void initPort(void){
//try to detect ECP port
unsigned char ECR = inportb(DATA + 0x402);
if ((ECR & 0x03) == 1){
outportb(DATA + 0x402,0x34);
if(inportb(DATA + 0x402) == 0x35){
cout << "ECP port detected" << endl;
cout << "ECP register at: " << hex << (DATA + 0x402) << "h" << endl;
}
else cout << "SPP/EPP port detected" <<endl;
outportb(DATA + 0x402, ECR); // restore original value
}
else cout << "SPP/EPP port detected" << endl;
}