1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Redefining the function fopen for files and getting the name

  1. Oct 10, 2015 #1
    1. The problem statement, all variables and given/known data
    How can you redefine a function in c. In this case I am trying to redefine fopen but it doesnt work because in the main the file doesnt change

    2. Relevant equations

    #include "InputLib.h"

    FILE* file_open(char* pt)
    FILE* fp = malloc(sizeof(FILE*)); // I tried this code without malloc and it doesnt work. I assume that this way would be better because I am allocating memory so fp wont be lost;
    return fp;


    char* getName()
    char* pt= calloc(SIZE,sizeof(char));
    return pt;

    #ifndef INPUTLIB_H_
    #define INPUTLIB_H_
    #include "IntList.h"
    char* getName();
    FILE* file_open(char infile[]);
    const int SIZE=50;
    #endif /* INPUTLIB_H_ */

    3. The attempt at a solution

    #include "IntList.h"

    int main(void)
    char C[80];
    FILE* MyFile;
    //printf("enter the name of the file");
    printf("enter the name of the file");
    printf("Enter a sentence");
  2. jcsd
  3. Oct 10, 2015 #2


    User Avatar

    Staff: Mentor

    Do you really need to redefine a standard function? It doesn't sound like good programming practice to me.
    I know that doesn't answer your question, though.
  4. Oct 10, 2015 #3


    Staff: Mentor

    @TheMathNoob, yesterday several people, including myself, asked you multiple times to use code tags around your code. I have closed this thread in an effort to get you to start doing this. If you don't know how, look at the thread you posted yesterday.
  5. Oct 10, 2015 #4


    Staff: Mentor

    I'll go further -- it's a really bad idea. If the standard library function isn't working right for you, don't rewrite the function -- figure out how you are misusing it.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook