Thread: How to delete saved data in a binary file on c Programming

  1. #1
    Registered User
    Join Date
    Apr 2017

    How to delete saved data in a binary file on c Programming

    Hi. I am creating a binary file that saves up to 5 data and everytime a new data saves the old (last) saved data will be deleted. I am having problem creating the c program to delete the last saved data.. this is what I have so far..I need help

    #include <stdio.h>
    // Symbolic constants
    #define BIN_FILE "data.bin"
    #define NUM_VALUES 5  // number of values to read from binary file
    // For computed values
    #define NUM 5 // number of computed values
    // The following are indexes into the array
    // containing computed values
    #define VIX 0
    #define CoIX 1
    #define QIX 2
    #define tIX 3
    #define CIIX 4
    // function prototypes
    void STOREDDATA(FILE *, double *);
    Function: main
    Description: Opens a binary file and calls STOREDDATA to save the initial concentration of the reactor volume,the
                concentration of the inlet flow, the volumetric flow rate of the streams and the total run time of the process values into the file
    void main(void)
        // Variable declarations
        FILE *fp;  // File pointer for binary file
        double outputVals[NUM];
        // Open the file
        fp = fopen(BIN_FILE, "rb");
        if(fp == NULL)
           printf("Cannot open binary file %s\n", BIN_FILE);
            STOREDDATA(fp, outputVals);
                   // Display the results
            printf("Analysis of file data gives: \n");
            printf("    initial concentration of the reactor volume: %.3f\n", outputVals[CoIX]);
            printf("    volume of the reactor: %.3f\n", outputVals[VIX]);
            printf("    concentration of the inlet flow: %.3f\n", outputVals[CIIX]);
            printf("    the volumetric flow rate of the streams: %.3f\n", outputVals[QIX]);
            printf("    total run time: %.3f\n", outputVals[tIX]);
    Function: STOREDDATA
        fp - file pointer for input binary file
        outPt - pointer to array for storing compute values.
    Description: Reads all data values into an array and scans the data
                 within the array to compute 5 outputs
    void STOREDDATA(FILE *fp, double *outPt)
        // Declarations
        double Co; //initial concentration of the reactor volume
        double V;//volume of the reactor
        double CI; // concentration of the inlet flow
        double Q; //volumetric flow rate of the streams
        double t; //total run time
        double val; // value read from the file
        double values[NUM_VALUES];  // Cannot read more than MAX_NUM_VALUES
        int ix;    // Indexes into array and counts number of values read
        // Read in values into the array
        rewind(fp); // start at the beginning of the file
        fread(values, sizeof(double),NUM_VALUES,fp);

  2. #2
    Registered User
    Join Date
    May 2010
    Where are you trying to write anything to your file?

    By the way main() should be defined to return an int, not a void.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Edit and Delete struct records that are saved in file?
    By Supremezzy in forum C Programming
    Replies: 2
    Last Post: 05-04-2016, 10:31 AM
  2. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  3. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  4. Reconstructing a vector saved in a binary file..
    By Shamino in forum C++ Programming
    Replies: 10
    Last Post: 02-02-2012, 07:13 PM

Tags for this Thread