#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    1
    Rep Power
    0

    How to read inputs from a file


    Sudesh Patel
    123 SouthRidge Ave
    Garland TX 67045
    214-777-9999
    3-5-78
    CoWorker
    sudesh@ti.com

    Copy of data above and program below

    void main (void)
    {

    class Person
    {

    public:
    char first_name[30];
    char last_name[30];
    char address1[50];
    char address2[50];
    char address3[50];
    char city[50];
    char state[2];
    char phone[10];
    char Date[8];
    char relation[25];
    char email_address[60];

    public:
    friend istream& operator >> (istream& stream, Person& person);
    };

    istream& operator >> (istream& stream, Person& person)
    {
    return stream >> first_name >> last_name >> address1 >> address2 >> address3
    >> city >> state >> phone >> date >> relation >> email_address;
    }

    ifstream file; // declare an input file stream
    Person p;
    file.open("c:\\address.file.txt", ios::in); // open it
    while (file.good())
    {
    file >> p;

    // Fill the form or whatever

    }
    file.close();
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    7
    Rep Power
    0
    you could read in the data from your file as strings

    use
    Code:
    getline(infile, line);
    to read line by line where your variables are declared as:
    Code:
    ifstream infile("Data.txt", ios::in|ios::out); 
    string line;
    Hope that helps.
    Last edited by C++ Padua; July 9th, 2003 at 12:00 AM.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    Code looks pretty good, except for a few errors. I'd say you've pretty much got the right idea, except for:
    1. You cannot write functions within functions in C/C++. You'll need to rearrange the code a bit to fix this.
    2. Never declare void main(void). Some C++ compilers allow it, but this is a bad idea. It's better to declare as int main(void). Note that void main() is legal in C, but not C++. See http://homepages.tesco.net/~J.deBoyn...void-main.html for more.
    3. return stream >> first_name >> last_name ... should read
    return stream >> person.first_name >> person.last_name ...
    4. The class has a member variable called Date, but the friend function refers to it as date. Either fix the friend function or the class variable so that they are the same lettercase.
    5. Your input file has an extra field after the state (i.e.) the zip code. You'll need to add an extra member variable to your class and read it in as well, otherwise the member variables after the state will not get read in correctly.
    6. Some of your arrays are too small. Note that char arrays need to have one extra byte for the NULL character. Not doing so is asking for trouble. Hence, you need to change state[2] to state[3], phone[10] to [phone[13] etc.

    With all that said, here's your rewritten code. I took the liberty of also adding a print_stuff() method to your class, so that you can verify that it is reading things properly.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Person 
    { 
    
    public: 
      char first_name[30]; 
      char last_name[30]; 
      char address1[50]; 
      char address2[50]; 
      char address3[50]; 
      char city[50]; 
      char state[3]; 
      char zipcode[6];
      char phone[13]; 
      char date[9]; 
      char relation[25]; 
      char email_address[60]; 
    
    public: 
      friend istream& operator >> (istream& stream, Person& person); 
      void print_stuff(void);
    }; 
    
    istream& operator >> (istream& stream, Person& person) 
    { 
      return stream >> person.first_name 
                    >> person.last_name 
                    >> person.address1 
                    >> person.address2 
                    >> person.address3 
                    >> person.city 
                    >> person.state
                    >> person.zipcode
                    >> person.phone 
                    >> person.date 
                    >> person.relation 
                    >> person.email_address; 
    } 
    
    void Person::print_stuff(void) {
      cout << first_name << endl
           << last_name << endl
           << address1 << endl
           << address2 << endl
           << address3 << endl
           << city << endl
           << state << endl
           << zipcode << endl
           << phone << endl
           << date << endl
           << relation << endl
           << email_address << endl;
    }
    
    int main(void) {
      ifstream file; // declare an input file stream 
      Person p; 
      file.open("file.txt", ios::in); // open it 
      while (file.good()) 
        { 
          file >> p; 
          
          // Fill the form or whatever 
          
        } 
      file.close();
      p.print_stuff();
      return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo