Thread: Writing to File Using a Function

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Writing to File Using a Function

    I globally declared a file. Then in my main I opened the file, then called a function to write, and then closed the file in my main again. When I am in my function, do I need to send it the file, or does it recognize that its open and ready to write to from my main?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    In reality, I don't think you should make the FILE * global, but if you do, then no, you don't have to send it to the function. Since it's global it can see it, but it won't recognize anything at all about the FILE * in question. If you call this function and try to read or write before opening the file, then disaster will probably strike.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    What would be the right syntax if I were to declare it in the main and send it in.

    Here is my guess:

    Function Declaration: void myFunction(FILE myFile)

    File declaration in main: FILE *file
    Function Call in the Main: myFunction(file)

    Function:

    void myFunction(myFile)
    [

    Writing to the file

    ]

    I am just making a guess here, I am not too sure.

    When I do it the global way, everything compiles and runs fine, until I go to make a fprintf statement in myFunction. Then it explodes.
    Last edited by Programmer3922; 08-02-2008 at 06:38 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You should pass a pointer to a file, not a FILE object itself. Other than that, it appears you might have the basic idea, although your syntax is way off.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    That is what I am unsure about is what the proper sytnax for sending and recieving files to functions are.

    I mean I know how it works with int, float, etc..

    Its just : void myFunction(int a)

    Main:
    number = 1;
    myFunction(number)

    void myFunction(int numberSendingIn)

    ...Function Operations...

    What is the appropriate way to code it as a file instead of an int, float, etc..

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, consider this for an int:

    Code:
    void myFunction(int i);
    This for an int *:

    Code:
    void myFunction(int *i);
    So for a FILE *:

    Code:
    void myFunction(FILE *f);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM