[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


open

Opens a file and returns it's id.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <fcntl.h>
int open(const char *path, int oflag);
Parameters:

Parameters for this facility are:

path  
char *  
The file path as a string  
oflag  
int  
The open mode  

Remarks:

The function open() opens a file for system level input and output. and is used with the UNIX style functions read() and write().

Legal file opening modes with open():

Mode
Description
O_RDWR  
Open the file for both read and write  
O_RDONLY  
Open the file for read only  
O_WRONLY  
Open the file for write only  
O_APPEND  
Open the file at the end of file for appending  
O_CREAT  
Create the file if it doesn't exist  
O_EXCL  
Do not create the file if the file already exists.  
O_TRUNC  
Truncate the file after opening it.  
O_NRESOLVE  
Don't resolve any aliases.  
O_ALIAS  
Open alias file (if the file is an alias).  
O_RSRC  
Open the resource fork  
O_BINARY  
Open the file in binary mode (default is text mode).  

Return:

open() returns the file id as an integer value.

See Also:

"close"

"lseek"

"read"

"write".

Example of open() usage::
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>


#define SIZE FILENAME_MAX
#define MAX 1024

char fname[SIZE] = "DonQ.txt";


int main(void)
{ 
	int fdes;
	char temp[MAX];
	char *Don = "In a certain corner of la Mancha, the name of\n\
which I do not choose to remember,...";
	char *Quixote = "there lived\none of those country\
 gentlemen, who adorn their\nhalls with rusty lance\
 and worm-eaten targets.";

	/* NULL terminate temp array for printf */
	memset(temp, '\0', MAX);  

	/* open a file */
	if((fdes = open(fname, O_RDWR | O_CREAT ))== -1)
	{
		perror("Error ");
		printf("Can not open %s", fname);
		exit( EXIT_FAILURE);
	}
		
	/* write to a file  */
	if( write(fdes, Don, strlen(Don)) == -1)
	{
		printf("%s Write Error\n", fname);
		exit( EXIT_FAILURE );
	}  	 

	/*	move back to over write ... characters */ 
	if( lseek( fdes, -3L, SEEK_CUR ) == -1L)
	{
		printf("Seek Error");
		exit( EXIT_FAILURE );
	}  
	
	/* write to a file  */
	if( write(fdes, Quixote, strlen(Quixote)) == -1)
	{
		printf("Write Error");
		exit( EXIT_FAILURE );
	}  	 
		
	/* move to beginning of file for read */
	if( lseek( fdes, 0L, SEEK_SET ) == -1L)
	{
		printf("Seek Error");
		exit( EXIT_FAILURE );
	}  
	
	/* read the file  */
	if( read( fdes, temp, MAX ) == 0)
	{
		printf("Read Error");
		exit( EXIT_FAILURE);
	}
	
	/* close the file */ 
	if(close(fdes))
	{
		printf("File Closing Error");
		exit( EXIT_FAILURE );
	}

	 puts(temp);  

	return 0;
}

In a certain corner of la Mancha, the name of
which I do not choose to remember,there lived
one of those country gentlemen, who adorn their
halls with rusty lance and worm-eaten targets.


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000