| | |
Reading from external data file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi
my name is Peter and I'm looking for somebody who can help me to understand how to read from external file without "destroing" existing inforamtion inside.
whole staff is based on book example which i'm trying to extend.
I have a programm which is working with database of 3 three accounts.
There is no problem to do operation on it(Deposit, Withdraw) and update data file. However if I insert there on start extra line which state how many accounts are there everything gets funy
data file
3 accounts
123 100.0
840 123.5
666 823.9
first number is account number second is current balance
and here is part of code which is looking for information on first line "3"
ifstream accfile;
accfile.open("accounts.dat");
int na; //number of accounts
accfile >> accs[1].accno;
cout << accfile << endl;
getline ( accfile, na);
cout <<"Number of accounts = " << na << endl;
give me error : no matching function for call to `getline (ifstream &, int &)'
if I declare na as string
cout brings "Numbers of accounts = accounts"
this does erease first line
my name is Peter and I'm looking for somebody who can help me to understand how to read from external file without "destroing" existing inforamtion inside.
whole staff is based on book example which i'm trying to extend.
I have a programm which is working with database of 3 three accounts.
There is no problem to do operation on it(Deposit, Withdraw) and update data file. However if I insert there on start extra line which state how many accounts are there everything gets funy
data file
3 accounts
123 100.0
840 123.5
666 823.9
first number is account number second is current balance
and here is part of code which is looking for information on first line "3"
ifstream accfile;
accfile.open("accounts.dat");
int na; //number of accounts
accfile >> accs[1].accno;
cout << accfile << endl;
getline ( accfile, na);
cout <<"Number of accounts = " << na << endl;
give me error : no matching function for call to `getline (ifstream &, int &)'
if I declare na as string
cout brings "Numbers of accounts = accounts"
this does erease first line
well still doesnt like it, it's saying
49: cannot convert `na' from type `string' to type `const char *'
I tried create another variable type integer and than convert string but than there message
cannot convert `na' from type `string' to type `const char *'
Anything else I can do with it???
Here is code as is look
data file
3 accounts
123 100.5
840 123.43
666 8273.06
program
49: cannot convert `na' from type `string' to type `const char *'
I tried create another variable type integer and than convert string but than there message
cannot convert `na' from type `string' to type `const char *'
Anything else I can do with it???
Here is code as is look
data file
3 accounts
123 100.5
840 123.43
666 8273.06
program
//account11.cc based on notes from tutorial Peter
#include<iostream.h>
#include<fstream.h>
#include<string>
const int ACC_MAX = 10;
enum TransactionType {Deposit, Withdraw};
struct Account //grouping account number and balance under Account{
int accno;
double balance;
};
struct Transaction //grouping transaction type, account number and amount of money moved{
TransactionType type;
int accno;
double amount;
};
void process(Transaction t, Account& a) //if deposit add money, if withdraw take out money[/RIGHT]{
switch(t.type)
{
case Deposit:
a.balance += t.amount;
break;
case Withdraw:
a.balance -= t.amount;
break;
}
}
void main()
{
Account accs[ACC_MAX];
int nRead = 0;
//open the data file
ifstream accfile;
accfile.open("accounts.dat");
string na; //number of accounts
accfile >> accs[1].accno;
getline (accfile, ucty);
atoi(na);
cout <<"Reading from first line = " << na << endl;
do //this does counting of accounts but I want to replace with stuff above
{ //where reading from first line says how many accounts are there
accfile >> accs[nRead].accno >> accs[nRead].balance;
if(accfile)
{
nRead++;
}
} while (accfile && nRead < ACC_MAX);
cout << nRead <<" accounts read\n";
Transaction trans = {Withdraw, 123, 50.0}; //simple transaction WITHDRAW from account 123
int a=0;
for (int i =0; i < nRead; i++)
{
if (accs[i].accno == trans.accno)
{
process (trans, accs[i]);
a = i;
}
}
//check the balance //print out up-date of account
cout <<"Account number: " <<accs[a].accno << endl;
cout <<"Balance is: £" << accs[a].balance << endl;
accfile.close();
ofstream oaccfile; //save any changes made
oaccfile.open("accounts.dat");
for (int i = 0; i < nRead; i++)
{
oaccfile << accs[i].accno << " " << accs[i].balance << endl;
}
oaccfile.close();
}![]() |
Similar Threads
- Reading data from file and storing in an array using both calloc and sscanf (C)
- Reading data to a file (Java)
- reading a specific data in .txt file to put in specific tag in xml in vb.net (VB.NET)
- reading column data (Python)
- importing data from file into multi-dimensional array (C)
- Reading a giant data file (C++)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Question:: reading from an input stream.
- Next Thread: Division with remainder
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






