Thread: Small Function problem. Would appreciate any help given. <3

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    16

    Question Small Function problem. Would appreciate any help given. <3

    Hey there. I have a file function that is not working correctly and am a bit stumped by it. I'd appreciate any help.

    Function prototype:
    Code:
    void SaveFile(student list[], FILE *database);
    Function definition:
    Code:
    void SaveFile(student list[], FILE *database)
    {
    database = fopen("database.txt","w"); //Here I am trying to connect to the file and have it create a file
    SaveFile(list, database); //then the program prints the list to the file
    fclose(database);//and then closes the file
    }
    Function call:
    Code:
    SaveFile(list, &database);
    Every time I set up the function call the program will not execute properly. It terminates and errors half way through the execution. Help? Thank you.

    And I do have the File defined.

    Code:
    FILE* database;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you are calling the function from itself, infinitely many times. Of course that's not going to work. Did you intend to call a different function on that second line?

    (Also, if you have "FILE* database", then "&database" is not of type FILE*, but rather FILE**.)

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    The function call is where I am stuck.

    I am a beginner in intro to c, I know what I want to do, just trying to get it to do what I want it to do is what is not working out for me. ):

    How would I function call the file definition if &database is wrong in SaveFile(list, &database);

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    SaveFile(list, database);

    "database" is already a pointer, so you don't need to use the address of operator (&).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remove the & from the call's parameter.

    Ack! I've been Ninja-posted by a bear lover!!

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    Still isn't executing though. ):

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you still have a huge problem with the recursion. You need to call a different function inside SaveFile, not the same one.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is the function still calling itself, infinitely?

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    I guess it is by accident. The 2nd line is suppose to print the list to the file, how would I fix that?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use one of the file printing functions, like fprintf, or fwrite, to write the list to the file.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    Is it possible to write a function to the file?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course not. (You can write what the function returns....)

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    See.. I'm stumped because throughout my code I am altering the list. [adding to it, deleting to it, clearing it, ect.]

    And I need to be able to save the current list, at whatever time it maybe, to the file.

    Which would be my display function.

    However, would writing what my display function returns into my file work?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What your display function returns is almost certainly what your list is, so yes.

    EDIT: Typing too fast. What your display function displays is almost certainly what your list is.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    16
    The function is void. ]: I'm so confused right now. I can get the file to open and close and create itself but simply don't know how to get display to write into it.

    Code:
     void Display(student list[], int num)
     {
    	 int i;
    	 for(i=0;i<num;i++)
    	 {
    		 printf("\nName:%s", list[i].name);
    		 printf("\nGPA:%lf", list[i].gpa);
    		 printf("\nID: %d\n", list[i].id);
    	 }
     }
    Especially since it is void.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM