1. Not finding help here? Sign up for a free 30min 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!

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;
    fp=fopen(pt,"w+");
    return fp;

    }

    char* getName()
    {
    char* pt= calloc(SIZE,sizeof(char));
    fgets(pt,SIZE,stdin);
    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");
    fflush(stdout);
    MyFile=file_open(getName());
    printf("Enter a sentence");
    fflush(stdout);
    gets(C);
    fprintf(MyFile,"%s",C);
    fclose(MyFile);
    }
     
  2. jcsd
  3. Oct 10, 2015 #2

    NascentOxygen

    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

    Mark44

    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

    Mark44

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Redefining the function fopen for files and getting the name
Loading...