Thread: Making a file with a name passed as a parameter

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Making a file with a name passed as a parameter

    Hi ,I have a method which makes a file by using the argument list as parameters.The only problem i have is that it does not work.
    Code:
    void openfile(struct file *p, const char * filename ){
        
        p->output=fopen(*filename,"w");
        if(p->output==NULL)
              fprintf(stderr,"out of memory\n");
        
    }
    
    
    
    int main(int argc, char** argv) {
        struct file*p=makeNewStruct();
        const char * f="hey";
        openfile(p,f);

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    p->output=fopen(*filename,"w");
    should be:
    p->output=fopen(filename,"w");

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by sand_man View Post
    p->output=fopen(*filename,"w");
    should be:
    p->output=fopen(filename,"w");
    I did change it but it still has errors.It builds fine but fails when i run it

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Please can anyone tell me what i am doing wrong

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It does help if you post a small and complete sample of code that illustrates your problem. You have posted a fragment, and some code you haven't bothered to show may be contributing to the symptoms.

    That said, things to check follow. Note that I am assuming, in several cases, that you are doing things in code that you have neglected to mention in your post.

    1) Is makeNewStruct() returning a valid object? If p is a dangling pointer (i.e. it points to something that has ceased to exist), or a NULL, then the assignment of p->output gives undefined behaviour.

    2) Have you #include'd <stdio.h>? A C compiler will assume fopen() returns int if you haven't. That causes your code to subtly exhibit undefined behaviour since there is a round-trip converstion from FILE * (inside fopen) to int, and back to FILE * (in the caller) - and that FILE * cannot be used safely (even comparing it with NULL can potentially be an invalid operation).

    3) Does the current working directory, when your program is run, exist? And is it writable? If not, the operating system cannot create the file, and fopen() will return NULL.

    4) Has other code in your program opened files, but neglected to close them? Most implementations have a distinct limit on the total number of files that a program may have open simultaneously. This can be an issue with some debuggers that keep files open when a program being debugged is terminated - the debugger itself gets confused.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    I have found the problem
    Last edited by sigur47; 05-05-2012 at 07:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-13-2012, 09:19 AM
  2. How passed as an input parameter?
    By Siaw Ys in forum C Programming
    Replies: 3
    Last Post: 12-03-2011, 06:06 AM
  3. Object passed to method as parameter
    By elodman in forum C# Programming
    Replies: 11
    Last Post: 09-22-2011, 07:59 PM
  4. Creating exe file with parameter
    By Lunaric in forum C Programming
    Replies: 3
    Last Post: 03-18-2004, 12:35 AM
  5. Comparing A Struct To A Passed Parameter
    By Anonymous in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2002, 11:38 PM