All pastes #21462 Raw Edit

Something

public text v1 · immutable
#21462 ·published 2005-08-29 02:00 UTC
rendered paste body
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

// define the structure here
struct transaction {
  int id;
};


int read_file();
int write_record(struct transaction& t);
int read_record(FILE* fp);
int enter_record();


int main()
{
 // put loop here
 
 return 0;   
}

int read_file()
{
 FILE* fp = fopen("expenses.dat", "rb");
 if(!fp) return -1;

 while(!feof(fp))
 {
  int err = 0;
  if(err = read_record(fp)) {
     cout << "error!" << endl;
     return err;
    }
 }
 return 0;
}

int write_record(struct transaction& t)
{
  FILE *fp = fopen ("expenses.dat","ba+");
  if (fp!=NULL) {
    write(fp, t, sizeof(struct transaction));
    fclose(fp);
  }
  return 0;
    
}

int read_record(FILE* fp)
{
 struct transaction t;
 int err = fread(fp, &t, sizeof(t));

 if(err) return err;

//print all shit here
 cout << t.transactionID << endl;

 return 0;
}

int enter_record() {
struct transaction t;
cout << "Enter this: ";
cin >> t.transactionID;
// repeat

// whgen done, write tihs record
write_record(t);
}