Solved

File input out put

Posted on 2004-04-16
16
414 Views
Last Modified: 2010-04-01

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

0
Comment
Question by:thamae
16 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840469
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
 

Author Comment

by:thamae
ID: 10840529
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840556
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
 
LVL 4

Expert Comment

by:oumer
ID: 10840571
include <fstream> and <iostream>
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840610
Change strcmpi () to strcmp () as DevC++ does not have strcmpi()

Amit
0
 

Author Comment

by:thamae
ID: 10840660
That has solved the problems but there is a new one Linker error and it reads "undefined reference to 'WriteFile'
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 125 total points
ID: 10840698
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10840704
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840749
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
 

Author Comment

by:thamae
ID: 10840847
You are real experts guys I am very pleased with the way you handle questions. May you prosper in expertise.

Thamae
0
 

Author Comment

by:thamae
ID: 10840889
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840902
thamae,

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

Amit
0
 

Author Comment

by:thamae
ID: 10841383
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10841531
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
 

Author Comment

by:thamae
ID: 10841716
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847655
Where did u create the file
I modified your program to open a file

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

Amit
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now