Thread: save data held in memory into the file

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    save data held in memory into the file

    /** save * Obtain file name from user and save locations and depths for all data
    * currently in memory. If the file exists, append the data, otherwise
    * create a new file.
    * Data is saved in format:
    * Line 1: Pit Location - A name that may contain spaces.
    * Line 2: Pit Depth - Depth of pit in meters (2 decimal places)
    *
    * @param location array of strings containing the pit locations
    * @param depth array of doubles containing the pit depths
    * @param numdata integer

    insert
    Code:
     int save_data()
     { 
       double depth[MAX_SIZE] =  depth(toBottom);
      printf("\n Enter name of file to enter pit data to:\n");
       gets (filename);
       if((fileptr= fopen(filename, "a"))==NULL)
    			   {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
      
      size_t fwrite(
       const void *location, size_t MAX_SIZE, size_t i, FILE *fileptr);
      
       
      fclose (fileptr);
      return i;
      }
    how should i write depth and location such that when printed they look like below:
    eldorado 01 -3.14
    also when i try to compile i get a error
    called object'depth' is not a function

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
     int save_data()
     { 
       double depth[MAX_SIZE] =  depth(toBottom);
      printf("\n Enter name of file to enter pit data to:\n");
       gets (filename);
       if((fileptr= fopen(filename, "a"))==NULL)
    			   {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
      
      size_t fwrite(
       const void *location, size_t MAX_SIZE, size_t i, FILE *fileptr);
      
       
      fclose (fileptr);
      return i;
      }
    Probably because the object that goes by teh name "depth" is not function, yet you are trying to use it as such.

    And to answer your question, and raise one of my own, why use fwrite? Just use fprintf().

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, FIX YOUR INDENTATION!
    When I first looked at your function, I thought abort() was the last line in your function.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good point.

    Code:
    int save_data()
    { 
      double depth[MAX_SIZE] =  depth(toBottom);
      printf("\n Enter name of file to enter pit data to:\n");
      gets (filename);
      if((fileptr= fopen(filename, "a"))==NULL)
      {
        printf("Error opening %s for writing. Program terminated.", filename);
        abort();
      }
      
      size_t fwrite(const void *location, size_t MAX_SIZE, size_t i, FILE *fileptr);
      
       
      fclose (fileptr);
      return i;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM