Thread: Saving to a file!

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    Saving to a file!

    hello! i was wonderin if any1 could help me with saving the program to a named file. i have a football league table, and i would like to save it so the league table is still the same when i load it! id like the user to enter the name of the file that it is going to be saved to! plz help!!! thanx!

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You need to open the file using fopen() and the "w" mode, then you can write to the file just like you write to the screen. When you're done, be sure to close the file with fclose(). It's that easy.
    Code:
    #include <stdio.h>
    
    
    int main( void ) {
      FILE *fp = fopen( "file.txt", "w" );
    
      if ( fp ) {
        fprintf( fp, "writing to the file\n" );
        fclose( fp );
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    ok thanx!! im just havin a little truble understandin how it works =( . i would like to have in the menu the option to "save table" and another option to "load table". so i would have somthing like:

    Code:
    int savetable():
    {
    
    
    
    }
    
    and 
    
    int loadtable():
    {
    
    
    
    }
    what would the code be in each of the above?? thanx!!

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I'm not going to write your program for you. Try writing some little test programs with the one I gave you as an example and figure out how it works. It really helps if you have a book or a good tutorial. I think cprogramming.com has a tutorial on working with files.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    ok thanx 4 the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM