#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep 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,

    Can you help me?

    thanks a lot




    I really needs your helps guys
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    No. fopen() returns a pointer to FILE. FILE is declared in <stdio.h>. You cannot return a pointer to FILE without using <stdio.h>.

    What do you want the prototype for your myfopen() function to be?

    Comments on this post

    • clifford agrees : ... unless you are too implementing your own FILE type - but that is less than clear here.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    myfopen(const char*, const char*)

    I know that i should start to do it with open() and write() or read() but i am not really good at this, and i dont know how to start.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    A prototype includes a return type :)

    something like
    Code:
    FILE *fopen(const char *, const char *);
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    thanks man
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by bdb
    A prototype includes a return type :)

    something like
    Code:
    FILE *fopen(const char *, const char *);
    yes exactly,

    it is a little comlicated because we will have the error cases, but for me it is just the beginning the problem,

    this function will return the address 0 in case of the error,

    and his jub is to open a stream andinitialize it
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    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().
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep 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*/
    {

    stream=(myFILE*)malloc(sizeof(myFILE));
    stream->index=open(name_file,O_RDONLY);


    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);
    }

    return stream;

    }

    Comments on this post

    • clifford disagrees : fail

IMN logo majestic logo threadwatch logo seochat tools logo