Redefining the function fopen for files and getting the name

  • Thread starter Thread starter TheMathNoob
  • Start date Start date
  • Tags Tags
    files Function
Click For Summary

Discussion Overview

The discussion revolves around the attempt to redefine the standard C function fopen for file handling. Participants explore the implications of redefining standard functions, the approach taken in the provided code, and the potential pitfalls of such practices. The scope includes programming practices and technical challenges related to function redefinition in C.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant presents a code snippet attempting to redefine fopen, expressing concerns about memory allocation and the persistence of the file pointer.
  • Another participant questions the necessity of redefining a standard function, suggesting it may not be good programming practice.
  • A further comment emphasizes that redefining standard library functions is a bad idea and encourages understanding the misuse of the original function instead.
  • There is a reminder for the original poster to use code tags for better readability, indicating a concern for proper formatting in discussions.

Areas of Agreement / Disagreement

Participants generally express disagreement regarding the practice of redefining standard functions, with some advocating against it while others focus on the technical aspects of the provided code. The discussion remains unresolved regarding the necessity and implications of such redefinitions.

Contextual Notes

Participants highlight potential issues with memory management and function usage without resolving the specific technical challenges presented in the code. There is an indication of unresolved questions about the correct usage of fopen and the rationale behind redefining it.

TheMathNoob
Messages
189
Reaction score
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
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.
 
@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.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K