Discuss Help plz - fopen() in the C Programming forum on Dev Shed. Help plz - fopen() C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 6
Time spent in forums: 1 h 19 m 18 sec
Reputation Power: 0
Help plz - fopen()
Hello,
I have a problem here, I want to write a function called"myfopen()" instead of "fopen()"
for writing this function I must not use the <stdio.h> library,
Posts: 4,808
Time spent in forums: 1 Month 2 Days 17 h 43 m 11 sec
Reputation Power: 1800
Compare the semantics and behaviour of fopen() with open() - your version will need to implement that missing functionality. You will need corresponding read, write and close operations as well - otherwise opening the file itself would be pointless. If you intend to use stdio fread/fwrite etc, then you will need implementation specific details of your particular libraries implementation.
It is not really reasonable to ask for help with so little initial effort. Since there are many design decisions that need to be made by you, and the scope of the task is significant. From the information you have provided, we would not really know where to start either.
There are a number of C libraries available as sourcecode that you might look at for inspiration. Here's one example I trust that on its own it will not help you too much (not least because it is written in K&R C rather than ISO/ANSI C, which is why I am happy to post the link as assistance rather than a solution. Your implementation will depend upon the scope and context of your particular assignment.
Last edited by clifford : October 14th, 2012 at 07:18 AM.
Posts: 4,808
Time spent in forums: 1 Month 2 Days 17 h 43 m 11 sec
Reputation Power: 1800
Quote:
Originally Posted by hamed.samie
yes exactly,
You have entirely missed bdb's point. FILE is a data structure defined by stdio.h. If it is your intent not to use stdio.h, you will have to define your own structure. Probably best in thsi case not to call it FILE but rather MYFILE perhaps.
The structure will contain anything your implementation needs to function in the manner you require, but I suggest that it will at least contain the file descriptor or handle returned by open().
Posts: 6
Time spent in forums: 1 h 19 m 18 sec
Reputation Power: 0
No man i got it,
i already defined that :
typedef struct
{
int index;
int number_char_buff;
char buffer[BUFSIZE];
} myFILE;
an d i knew that i must use th e open() function instead of fopen()
but for the beginning i had problems i did sth but i am not sure that it is a good way or not, the problem is the time
this is it nt complete yet,
myFILE* myfopen(const char* name_file, const char* options)
{
char* adress_r=strchr(options,'r');
char* adress_w=strchr(options,'w');
char* adress_a=strchr(options,'a');
char* adress_t=strchr(options,'t');
myFILE *stream=NULL;
if ( adress_r== NULL && adress_w == NULL ) /* If r and w are not in options */
{
/*ERROOOOOOOOOOOOR !;*/
}
else if ( adress_r!= NULL && adress_w != NULL ) /* If r and w are in options*/
{} /*ERROOOOOOOOOOOOR !;*/
else if (adress_r != NULL) /* If just r is in options*/
{
if (stream->index <0)
{
fatal(0,"This file do not exist \n \0", 5);
}
}
else if ( adress_a != NULL && adress_t != NULL ) /* If a and t are in otions*/
{} /*ERROOOOOOOOOOOOR !;*/
else if ( adress_a == NULL && adress_t == NULL ) /* If just w is in otions*/
{
stream=(myFILE*)malloc(sizeof(myFILE));
stream->index=open(name_file,O_CREAT|O_WRONLY,S_IRUSR|S_IWUSR|S_IROTH);
}
else if( adress_a != NULL) /* If just w and a are in otions*/
{
stream=(myFILE*)malloc(sizeof(myFILE));
stream->index=open(name_file,O_CREAT|O_WRONLY|O_APPEND,S_IRUSR|S_IWUSR|S_IROTH);
}
else /* If just w and t are in otions*/
{
stream=(myFILE*)malloc(sizeof(myFILE));
stream->index=open(name_file,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR|S_IROTH);
}