Redefining the function fopen for files and getting the name

In summary: If you're just trying to learn how to write functions, then leave the standard ones alone for now and just write your own.In summary, the conversation discusses how to redefine a function in C, specifically the function fopen. The code provided shows an attempt at redefining the function, but it does not work. The potential use of malloc to allocate memory for FILE* is mentioned. It is also mentioned that redefining a standard function is not good programming practice and it is suggested to figure out how to use the function correctly instead. The conversation ends with a reminder to use code tags when posting code.
  • #1
TheMathNoob
189
4

Homework Statement


How can you redefine a function in c. In this case I am trying to redefine fopen but it doesn't work because in the main the file doesn't change

Homework Equations



#include "InputLib.h"

FILE* file_open(char* pt)
{
FILE* fp = malloc(sizeof(FILE*)); // I tried this code without malloc and it doesn't work. I assume that this way would be better because I am allocating memory so fp won't be lost;
fp=fopen(pt,"w+");
return fp;[/B]
}

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_ */


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);
}
 
Physics news on Phys.org
  • #2
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.
 
  • #3
@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.
 
  • #4
NascentOxygen said:
Do you really need to redefine a standard function? It doesn't sound like good programming practice to me.
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.
 

Related to Redefining the function fopen for files and getting the name

What is the purpose of redefining the function fopen for files?

The purpose of redefining the function fopen for files is to customize the behavior of the function to better suit the needs of the programmer. This can include adding new features, improving performance, or fixing bugs.

How does redefining the function fopen for files affect the way file names are handled?

Redefining the function fopen for files can allow for more flexibility in how file names are handled. This can include supporting different file name formats, allowing for longer file names, or handling special characters in file names.

Can the function fopen for files be redefined for specific file types?

Yes, the function fopen for files can be redefined for specific file types. This can be useful for creating specialized functions for handling specific file types, such as audio files or image files.

Are there any potential drawbacks to redefining the function fopen for files?

There can be potential drawbacks to redefining the function fopen for files. If not done carefully, it can introduce bugs or cause compatibility issues with existing code. It is important to thoroughly test any changes made to the function.

Can the function fopen for files be redefined multiple times?

Yes, the function fopen for files can be redefined multiple times. This can be useful for making incremental changes or for different parts of the code that may require different behaviors for the function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
972
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top