Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 12th, 2004, 11:44 AM
Authorized User
 
Join Date: Aug 2004
Location: Jasenie, , Slovakia.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading from external data file

Hi
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


well i tried to use atoi to change string variable to int but 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


//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
{
  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();
}


P.T.
__________________
P.T.
Reply With Quote
  #2 (permalink)  
Old December 12th, 2004, 02:14 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

atoi() takes a c-style string (array of char, null-terminated)

You can use the c_str() member function of an ifstream to feed it:

Code:
  //getline (accfile, ucty); //<=== mistake? where is and what is ucty
  getline (accfile, na); // my guess, since you use na in the next statements
  atoi(na.c_str());  // <=== c-style string
This may get you to the point of debugging, I hope.

Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old December 12th, 2004, 02:40 PM
Authorized User
 
Join Date: Aug 2004
Location: Jasenie, , Slovakia.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are right Dave, that

getline(accfile, ucty);

it's me mistake just trying quickly to translate from me mother tongue.
well me problem remains, still is reading only word "accounts" instead of "3" and mess-up data file(delete first line 3 accounts and make transaction)
Any other ideas??

Regards

P.T.
Reply With Quote
  #4 (permalink)  
Old December 12th, 2004, 04:45 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's how I would think about debuging the program.

First of all, since your program changes the "accounts.dat" file that means that you have to re-create it again every time you run the program. For the time being, make a different output file.

Now, as you read each thing into your program, show what the program actually sees by using cout.

For example:

Code:
accfile >> accs[1].accno;
  cout << "Debug: accs[1].accno = " << accs[1].accno<< endl;
  //getline (accfile, ucty);
  getline (accfile, na);
  cout << "Debug: na = " << na << endl;
  atoi(na.c_str());  
  cout <<"Debug: This was 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
      cout << "Top of loop: nRead = " << nRead << endl;
    accfile >> accs[nRead].accno >> accs[nRead].balance;

    cout << "Debug: accs[" << nRead << "].accno = " << accs[nRead].accno << endl;
    cout << "Debug: accs[" << nRead << "].balance = " << accs[nRead].balance << endl;
Get the point? Print out everything until you are sure what the program sees. Then you can delete or comment out the extra cout statements.

By the way, why did you do this for the very first read:
Code:
  accfile >> accs[1].accno;
How about this:

Code:
  atoi(na.c_str());
This calculates something, but doesn't store the value in a variable, so what's supposed to happen.

Regards,

Dave
Reply With Quote
  #5 (permalink)  
Old December 12th, 2004, 06:02 PM
Authorized User
 
Join Date: Aug 2004
Location: Jasenie, , Slovakia.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think we do not understand each other so I will try to explain what is this all about.

If we forget about this part
string na; //number of accounts
accfile >> accs[1].accno;
getline (accfile, ucty);
atoi(na);
cout <<"Reading from first line = " << na << endl;

Program open ex.file accounts.dat
Compare how many accounts there in case that I want to add new account ( this function doesn't exist yet)
Than I'm telling it to do withdraw from account number ...
It is searching dat file till find match of accounts numbers
Print out account number and current balance
Close dat file which stays in buffer
Open file for writing
And update

This is working with no problems at all

But I wanted to state on the first line of dat file how many accounts are there
This not happening because of this little piece of code
string na; //number of accounts
accfile >> accs[1].accno;
getline (accfile, na);
atoi(na);
cout <<"Reading from first line = " << na << endl;

where at begginnig am trying to reade first data accno "account number" and forget about rest of line. This should tell me that there are 3 accounts. However this code find out there is word account does Withdraw and overwrite file it's own way. Why ???

P.T.
Reply With Quote
  #6 (permalink)  
Old December 14th, 2004, 03:06 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I think I understand you OK:

your program doesn't work the way you think it should.

My suggestions were to get you to look at what your program actually does:

Code:
accfile >> accs[1].accno
If the first line of your data file has

Quote:
quote:
3 accounts
then this statements stores the number 3 in the account number field of account number 1. Why?????

Then you read the rest of the line

Code:
  getline (accfile, na);
This puts " accounts" into string na

Then this
Code:
  atoi(na);
Tries to calculate the integer value of the string, then does nothing with the result (The result is zero, by the way.)

Then you read the rest of the file. If each line has an account number and a name, you have read values into accs[0], accs[1], and accs[2]. If you put cout statements as I suggested, you can see whether the file data is what you think it is.

Now, after reading the accounts and performing a transaction, you close the file, then open the same file for writing and you write out the account information. Note that you did not write the first line "3 accounts". So the next time you try to use this as an input file, the program fails. (When you open a file for writing it destroys the previous contents of the file, so you must re-create the entire contents for it.)

Now, If I have misunderstood you, maybe you can tell me what you really mean.


Regards,

Dave
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
insert data param into xml from external file alexshiell XSLT 0 January 24th, 2006 01:47 PM
Reading ASCII data from text file. LordBeholder VB How-To 2 June 25th, 2004 05:50 PM
Read external data rajanikrishna Classic ASP Databases 0 May 6th, 2004 03:24 AM
Opening file external kaivanrijswijk VBScript 0 March 29th, 2004 05:43 PM
Reading string data from an input file Omega_st1 Visual C++ 0 October 15th, 2003 05:10 PM



All times are GMT -4. The time now is 10:47 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.