File input out put


I am trying to manipulate a file of records and I get these two errors when I compile;

1. variable 'std::fstream a_file' has initializes
2. storage size of 'a_file' isn't known

Both of these errors are at the following line of code

fstream a_file("data.dat");

please help me

thamaeAsked:
Who is Participating?
 
Sys_ProgConnect With a Mentor Commented:
That's because
u have initially declared a function
WriteFile() with no parameters

Then u have defined a WriteFile(0 function with 2 parameters

And then u are using WriteFile() with  no parameters

By declaring initially, u tell the copiler that there si a function called WriteFile() with no parameters, henec the compiler does not give u error saying that undecalared identifier/function

But, the linker tries to resolve the call to Write() with no parameters, it cannot see the definiton of this function, since u have defined a WriteFile() with 2 parameter

Hence the Linker error

Amit
0
 
Sys_ProgCommented:
Post your code
also let us know your compiler and platform

Try this code at your side

#include <fstream>

int main(int argc, char* argv[])
{
   std::fstream f ( "data.dat" ) ;
   system("pause");
   return 0;
}


Amit
0
 
thamaeAuthor Commented:
Ok my code is here (even thogh it is huge) and I am using Dev-C++




#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
#include <iomanip.h>

#define structlength sizeof(rec); /*macro definitions*/

using namespace std;

/*Declaration of functions*/

void AddCustomer();    /*adds new customer to database*/
void PayAccount();    /*processes customer payments*/
void ViewAll();      /*view all customers in databese*/
void ViewOne();      /*view ano customer record*/
void ArrReport();    /*list all customers with arrears*/
void Menu();        /*displays user menu*/
void delay();
int OpenFile();
void CloseFile();
int WriteFile();
int ReadFile();
float Arrear();    /*returns arrears value*/
int quit();

/*Global declaration*/
 
fstream a_file("data.dat"); /*errors here*/

/*Structure Declaration*/

struct customer
{
 int mon;
 int day;
 int rate;
 int term;
 int year;
 int status;
 char id[30];
 char DOB[20];
 char nam[30];
 char snam[30];
 char town[30];
 char adres[30];
 char phone[15];
 float balance;
 float TotalPaid;
 float instalment;
 float LoanValue;
 float LoanAmount;
 
 }rec;
 
 /*FUNCTION TO DISPLAY MENU*/

int quit()
{
 //system("PAUSE");//
 return 0;
}
void delay()
{
 for(int i=0;i<100000;i++)
 {
  for(int j=0;j<10000;j++)
  {
   
  }
 
 }
}
int OpenFile()
{
 a_file.open("Data.dat",ios::in && ios::out);
 if(!a_file.good())
 {
  cout<<"SORRY,COULD NOT OPEN FILE\n:";
  return 0;
 }
 else
 {
  a_file.seekg(0,ios::end);
  long pos = a_file.tellg();
  int number = pos/sizeof(customer);
 }
}
void CloseFile()
{
 a_file.close();
}
int WriteFile(customer& rec,int cNum)
{
 long where = cNum*sizeof(customer);
 a_file.seekp(where,ios::beg);
 a_file.write(&rec, sizeof(customer));
 if(!a_file.good())
 {
  cout<<"SORRY, CANNOT WRITE TO FILE\N";
  return 0;
 }
}
int ReadFile(customer &rec,int cNum)
{
 long where = cNum*sizeof(customer);
 a_file.seekg(where,ios::beg);
 a_file.read(&rec,sizeof(customer));
 if(!a_file.good())
 {
  cout<<"SORRY, CANNOT READ THE FILE\n";
  return 0;
 }
}
void men()
{

 system("cls");
 int ch;
 cout<<"********************************MENU OPTIONS***********************************\n\n\n";
 cout<<"1.ADD NEW CUSTOMER\n";
 cout<<"2.ACCOUNT LOOK-UP\n";
 cout<<"3.ALL ACCOUNTS\n";
 cout<<"4.ACCOUNT PAYMENT\n";
 cout<<"5.ARREARS REPORT\n";
 cout<<"6.EXIT APPLICATION\n\n\n";
 
 cin>> ch;
 if(ch == 1)
 {
  AddCustomer();
 }
 else if(ch == 6)
 {
  quit();
 }
}
 /*A FUNCTION TO ADD A NEW CUSTOMER*/
 
void AddCustomer()
{
 
 OpenFile();
  system("cls");
  cout<<"***************************QUICK-MONEY DATABASE ENTRY***************************\n\n\n";
  cout<<"ID NUMBER :";
  cin>>rec.id;
  cout<<" \n";
  cout<<"NAME :";
  cin>>rec.nam;
  cout<<" \n";
  cout<<"SURNAME :";
  cin>>rec.snam;
  cout<<" \n";
  cout<<"LOAN AMOUNT :";
  cin>>rec.LoanAmount;
  cout<<" \n";
  cout<<"LOAN TERM: ";
  cin>>rec.term;
  cout<<" \n";
  cout<<"DOB :";
  cin>>rec.DOB;
  cout<<" \n";
  cout<<"TOWN :";
  cin>>rec.town;
  cout<<" \n";
  cout<<"VILLAGE (NO SPACES) :";
  cin>>rec.adres;
  cout<<" \n";
  cout<<"INTEREST RATE :";
  cin>>rec.rate;
  cout<<" \n";
  cout<<"PHONE :";
  cin>>rec.phone;
  cout<<" \n\n\n";
  rec.LoanValue = rec.LoanAmount+ (rec.LoanAmount*rec.rate)/100;
  rec.instalment = rec.LoanValue/rec.term;
  rec.balance = rec.LoanValue;
  rec.TotalPaid = 0.0;
  rec.status = 0;
 
 
 
   int Day(rec.day);
   int Month(rec.mon);
   int year(rec.year);
 
  cout<<"DO YOU WANT TO SAVE THIS RECORD (Y=1/N=0)?: ";
  int ch;
  cin>>ch;
  if(ch == 1)
  {
   WriteFile();
   CloseFile();
  }
  else
  {
   cout<<" \n";
   cout<<"RECORD NOT SAVED PLEASE WAIT WHILE LOADING MENU......"<<endl;
   delay();
  }
 
 
   men();
}
   
   
 
 
 
/*void PayAccount();
void ViewAll();
void ArrReport();
float Arrear(); */
 
void ViewOne()
{
 char ch;
 
 char idn[30];
 system("cls");
 OpenFile();
 cout<<" CUSTOMER ID: ";
 cin>>idn;
 
 while(a_file.read(&rec,sizeof(customer)) && strcmpi(idn,rec.id))
 {

   if(!strcmpi(idn,rec.id))
   {
    cout<<"******RECORD NOT FOUND*************\n";
    delay();
   }
   else
   {
    system("cls");
    cout<<"************SEARCH RESULTS***************\n\n\n";
    cout<<"CUSTOMER ID: "<<rec.id<<endl;
    cout<<"NAME: "<<rec.nam<<endl;
    cout<<"BALANCE: "<<rec.balance<<endl;
    cout<<"LOAN AMOUNT: "<<rec.LoanAmount<<endl;
    cout<<"LOAN VALUE: "<<rec.LoanValue<<endl;
    cout<<"LOAN TERM: "<<rec.term<<endl;
    cout<<"INTEREST : "<<rec.rate<<endl;
    cout<<"ARREAS :       "<<endl;
    cout<<"PHONE : "<<rec.phone<<endl;
    cout<<"VILLAGE : "<<rec.adres<<endl<<endl;
    cout<<"LOAN DATE: "<<rec.day<<"/"<<rec.mon<<"/"<<rec.year<<endl<<endl;
    cout<<"**************END OF RESULTS PRESS ANY KEY TO CONTINUE********\n\n";
    int k;
    cin>>k;
   }
   CloseFile();
  }
  men();
 
}
int main()
{
   men();
 
}


0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Sys_ProgCommented:
First include the foolowing files

#include <iostream>
#include <fstream>


Change the #include <iomanip.h> to #include <iomanip>

Then change
 a_file.open("Data.dat",ios::in && ios::out);
to
 a_file.open("Data.dat",ios::in | ios::out);

Amit
0
 
oumerCommented:
include <fstream> and <iostream>
0
 
Sys_ProgCommented:
Change strcmpi () to strcmp () as DevC++ does not have strcmpi()

Amit
0
 
thamaeAuthor Commented:
That has solved the problems but there is a new one Linker error and it reads "undefined reference to 'WriteFile'
0
 
Karl Heinz KremerCommented:
You have a function prototype for WriteFile() without a parameter, you've implemented it without a parameter, but you are calling it with parameters. This will not work. Ether call it withtout parameter, or implement it with parameters.
0
 
Sys_ProgCommented:
khkremer,

He has declared a function without any parameters
He has defined a function with 2 parameters
And he is using the function without parameters

Amit
0
 
thamaeAuthor Commented:
You are real experts guys I am very pleased with the way you handle questions. May you prosper in expertise.

Thamae
0
 
thamaeAuthor Commented:
My problems seem to be endless!

My program is exacutable now but no file of records is created. When I try to view records (thr'u menu option 2) the program fails to open the data file, why is this?
0
 
Sys_ProgCommented:
thamae,

Can u post ur comiling code so that we can have a look at it

Amit
0
 
thamaeAuthor Commented:
Ok Amit, there it goes;


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
#include <iomanip>
#include <iostream>
#include <fstream>

#define structlength sizeof(rec); /*macro definitions*/

using namespace std;

/*Declaration of functions*/

void AddCustomer();    /*adds new customer to database*/
void PayAccount();    /*processes customer payments*/
void ViewAll();      /*view all customers in databese*/
void ViewOne();      /*view ano customer record*/
void ArrReport();    /*list all customers with arrears*/
void Menu();        /*displays user menu*/
void delay();
int OpenFile();
void CloseFile();
int WriteFile();
int ReadFile();
float Arrear();    /*returns arrears value*/
int quit();

/*Global declaration*/
 
 fstream a_file("data.dat");

/*Structure Declaration*/

struct customer
{
 int mon;
 int day;
 int rate;
 int term;
 int year;
 int status;
 char id[30];
 char DOB[20];
 char nam[30];
 char snam[30];
 char town[30];
 char adres[30];
 char phone[15];
 float balance;
 float TotalPaid;
 float instalment;
 float LoanValue;
 float LoanAmount;
 
 }rec;
 
 /*FUNCTION TO DISPLAY MENU*/

int quit()
{
 //system("PAUSE");//
 return 0;
}
void delay()
{
 for(int i=0;i<100000;i++)
 {
  for(int j=0;j<10000;j++)
  {
   
  }
 
 }
}
int OpenFile()
{
 a_file.open("Data.dat",ios::in | ios::out );
 if(!a_file.good())
 {
  cout<<"SORRY,COULD NOT OPEN FILE\n:";
  return 0;
 }
 else
 {
  a_file.seekg(0,ios::end);
  long pos = a_file.tellg();
  int number = pos/sizeof(customer);
 }
}
void CloseFile()
{
 a_file.close();
}
//int WriteFile(customer& rec,int cNum)
int WriteFile()
{
 int cNum;
 long where = cNum*sizeof(customer);
 a_file.seekp(where,ios::beg);
 a_file.write((char*)&rec, sizeof(customer));
 if(!a_file.good())
 {
  cout<<"SORRY, CANNOT WRITE TO FILE\N";
  delay();
  return 0;
 }
}
int ReadFile(customer &rec,int cNum)
{
 long where = cNum*sizeof(customer);
 a_file.seekg(where,ios::beg);
 a_file.read((char*)&rec,sizeof(customer));
 if(!a_file.good())
 {
  cout<<"SORRY, CANNOT READ THE FILE\n";
  delay();
  return 0;
 }
}
void men()
{

 system("cls");
 int ch;
 cout<<"********************************MENU OPTIONS***********************************\n\n\n";
 cout<<"1.ADD NEW CUSTOMER\n";
 cout<<"2.ACCOUNT LOOK-UP\n";
 cout<<"3.ALL ACCOUNTS\n";
 cout<<"4.ACCOUNT PAYMENT\n";
 cout<<"5.ARREARS REPORT\n";
 cout<<"6.EXIT APPLICATION\n\n\n";
 
 cin>> ch;
 if(ch == 1)
 {
  AddCustomer();
 }
 else if(ch == 2)
 {
  ViewOne();
 }
 else if(ch == 6)
 {
  quit();
 }
}
 /*A FUNCTION TO ADD A NEW CUSTOMER*/
 
void AddCustomer()
{
 
 OpenFile();
  system("cls");
  cout<<"***************************QUICK-MONEY DATABASE ENTRY***************************\n\n\n";
  cout<<"ID NUMBER :";
  cin>>rec.id;
  cout<<" \n";
  cout<<"NAME :";
  cin>>rec.nam;
  cout<<" \n";
  cout<<"SURNAME :";
  cin>>rec.snam;
  cout<<" \n";
  cout<<"LOAN AMOUNT :";
  cin>>rec.LoanAmount;
  cout<<" \n";
  cout<<"LOAN TERM: ";
  cin>>rec.term;
  cout<<" \n";
  cout<<"DOB :";
  cin>>rec.DOB;
  cout<<" \n";
  cout<<"TOWN :";
  cin>>rec.town;
  cout<<" \n";
  cout<<"VILLAGE (NO SPACES) :";
  cin>>rec.adres;
  cout<<" \n";
  cout<<"INTEREST RATE :";
  cin>>rec.rate;
  cout<<" \n";
  cout<<"PHONE :";
  cin>>rec.phone;
  cout<<" \n\n\n";
  rec.LoanValue = rec.LoanAmount+ (rec.LoanAmount*rec.rate)/100;
  rec.instalment = rec.LoanValue/rec.term;
  rec.balance = rec.LoanValue;
  rec.TotalPaid = 0.0;
  rec.status = 0;
 
 
 
   int Day(rec.day);
   int Month(rec.mon);
   int year(rec.year);
 
  cout<<"DO YOU WANT TO SAVE THIS RECORD (Y=1/N=0)?: ";
  int ch;
  cin>>ch;
  if(ch == 1)
  {
   WriteFile();
   CloseFile();
  }
  else
  {
   cout<<" \n";
   cout<<"RECORD NOT SAVED PLEASE WAIT WHILE LOADING MENU......"<<endl;
   delay();
  }
 
 
   men();
}
   
   
 
 
 
/*void PayAccount();
void ViewAll();
void ArrReport();
float Arrear(); */
 
void ViewOne()
{
 char ch;
 
 char idn[30];
 system("cls");
 OpenFile();
 cout<<" CUSTOMER ID: ";
 cin>>idn;
 
 while(a_file.read((char*)&rec,sizeof(customer)) && strcmp(idn,rec.id))
 {

   if(!strcmpi(idn,rec.id))
   {
    cout<<"******RECORD NOT FOUND*************\n";
    delay();
   }
   else
   {
    system("cls");
    cout<<"************SEARCH RESULTS***************\n\n\n";
    cout<<"CUSTOMER ID: "<<rec.id<<endl;
    cout<<"NAME: "<<rec.nam<<endl;
    cout<<"BALANCE: "<<rec.balance<<endl;
    cout<<"LOAN AMOUNT: "<<rec.LoanAmount<<endl;
    cout<<"LOAN VALUE: "<<rec.LoanValue<<endl;
    cout<<"LOAN TERM: "<<rec.term<<endl;
    cout<<"INTEREST : "<<rec.rate<<endl;
    cout<<"ARREAS :       "<<endl;
    cout<<"PHONE : "<<rec.phone<<endl;
    cout<<"VILLAGE : "<<rec.adres<<endl<<endl;
    cout<<"LOAN DATE: "<<rec.day<<"/"<<rec.mon<<"/"<<rec.year<<endl<<endl;
    cout<<"**************END OF RESULTS PRESS ANY KEY TO CONTINUE********\n\n";
    int k;
    cin>>k;
   }
   CloseFile();
  }
  men();
 
}
int main()
{
   men();
 
}
0
 
Sys_ProgCommented:
You are opening the file in ios::in as well as ios::out
For this to work, the file should be existing first

Your write does not happen because the file does not open

So, either u should manually create the file blank OR what u can do is open the file in output mode & then close it at program startup.

Amit
0
 
thamaeAuthor Commented:
I tried to create the file blank (text fie)* manually but there is an open failure, what is the course. please bear with me this is my first introduction file handling.

* I changed "data.dat" to "data.txt".
0
 
Sys_ProgCommented:
Where did u create the file
I modified your program to open a file

open ( "c:\\data.txt", .......)  ;

Amit
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.