5 Replies - 233 Views - Last Post: 04 July 2013 - 05:20 PM Rate Topic: -----

#1 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 210
  • Joined: 06-July 12

How to read numbers from a file and load them into an object?

Posted 04 July 2013 - 03:53 PM

Okay, my problem is I'm not sure how to read numbers (all ints) from a file then load them into my object. Then how do I write the sorted numbers in that array to an output file? Also is my decending bubble sort correct and is my see_all function correct?

//numlist.h File

#ifndef NUMLIST_H
#define NUMLIST_H
#include <iostream>
#include <cstdlib>
#include <cmath>

class NumList{
    public:
        static const size_t CAPACITY = 100;

        // Default constructor
        NumList();

        // Precondition: none
        // Postcondition: num is placed at the end of the list,
        //               if there is sufficient Capacity
        void insert(int num);
        size_t size()const {return used;}

        //Precondition: istream is a stream that has already been connected to
        //              a successfully opened file
        // Postcondition: integers from the file are loaded into the object
        void load_from_file(std::istream& ins);

        // Preconditon: ostream is connected to a successfully open file
        // Postconditon numbers in the object are written to the file
        void save_to_file(std::ostream& outs);
        void b_sort();
        int get_item(size_t index)const;
        void see_all()const;
    private:
        int data[CAPACITY];
        size_t used;
};

#endif



//numlist.cc File

#include <fstream>
#include "numlist.h"
using namespace std;

// Constructor
NumList::NumList(){
        used = 0;
}

void NumList::insert(int num){
        if(used < CAPACITY){
                data[used] = num;
                used++;
        }
        else{
                cout<<"Error. List capacity has been reached.\n";
        }
}

void NumList::load_from_file(istream& ins){
        ifstream ifs;
        int tmp;
        ifs.open("data");

        while(ins >> tmp) //I don't understand how to read from a file and load them to an object?
           insert(tmp);
}


void NumList::save_to_file(ostream& outs){
        for(int i = 0; i < used - 1; i++) //I don't understand how to write the array numbers to a file i think this is                                   
                                         // correct?
           outs << used << endl;
}
void NumList::see_all()const{
        if(used == 0)
            cout<<"Empty list.\n";
        else
            for(int i = 0; i < used; i++)
                cout << data[i] << endl;
}

int NumList::get_item(size_t index)const{
        if(index < used)
                return data[index];
        else
                return -1;
}

void NumList::b_sort(){
    bool done = false;
    int j;
    int tmp;
    while(!done){
        done = false;
        for(j = used - 1; j >= 0; j--){  //is my decending bubble sort correct?
            if(data[j] < data[j-1]){
                done = false;
                tmp = data[j];
                data[j] = data[j-1];
                data[j - 1] = tmp;
            }
            else
                done = true;
        }
    }
}



//main.cc File

#include <iostream>
#include <fstream>
#include "numlist.h"
using namespace std;

int main(){
     NumList num1;
     int tmp;
     size_t pos;
     ifstream ifs;
     string filename;

     cout << "Please enter the name of your number file:  ";
     cin >> filename;

     ifs.open(filename.c_str());
     if(ifs.fail()){
        cout << "Input file failed to open.\n";
        return -1;
     }

     num1.load_from_file(ifs);
     ifs.close();
     num1.see_all();

     cout << "Please enter three numbers to add to the list:  ";
     cin >> tmp;

     num1.insert(tmp); //1
     cin >> tmp;

     num1.insert(tmp); //2
     cin >> tmp;

     num1.insert(tmp); //3
     num1.b_sort();
     num1.see_all();

     pos = filename.find(".");
     filename.insert(pos, "sorted");

     ofstream ofs(filename.c_str());
     num1.save_to_file(ofs);
     ofs.close();

return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to read numbers from a file and load them into an object?

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: How to read numbers from a file and load them into an object?

Posted 04 July 2013 - 04:31 PM

Let's start with reading a file ...

See this example:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

const string FNAME = "myContacts.txt";
/*
John Doe
100 Main Street
Chicago, IL 60661

Mary Sue
555 New Avenue
New York, NY 57755

*/


struct Contact
{
   string name;
   string add1;
   string add2;
   friend istream& operator >> ( istream& is, Contact& c )
   {
      string dummy;
      getline( is, c.name );
      getline( is, c.add1 );
      getline( is, c.add2 );
      getline( is, dummy );
      return is;
   }
   friend ostream& operator << ( ostream& os, const Contact& c )
   {
       os << c.name << endl;
       os << c.add1 << endl;
       os << c.add2 << endl;
       return os;
   }
} ;


int main()
{
    vector< Contact > v;
    ifstream fin( FNAME.c_str() );
    if( fin )
    {
       Contact c;
       while( fin >> c  )
       {
          v.push_back( c );
       }
       fin.close();
    }
    // Ok ... you NOW have your vector full of all the contacts in the file
    // show it ...
    vector< Contact >::const_iterator it;
    cout << "Top ...\n";
    for( it = v.begin(); it != v.end(); ++ it  )
    {
        cout << *it;
        cout << endl;
    }
    
    cout << "Press 'Enter' to continue/exit ..." << flush;
    string dummy;
    getline( cin, dummy );
}

Was This Post Helpful? 0
  • +
  • -

#3 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 210
  • Joined: 06-July 12

Re: How to read numbers from a file and load them into an object?

Posted 04 July 2013 - 04:44 PM

I'm sorry I don't know what half the stuff you have in your code is like iterator, vector, push_back, *it. All I'm trying to do is read some numbers from a file and insert them into my an array and then write them to an output file.
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: How to read numbers from a file and load them into an object?

Posted 04 July 2013 - 04:58 PM

what is you file structure to look like ...

please supply an example

Edit: Oh ... I see it is just a list of int's (separated by ws <white-space> ) ...

That you wish to read into an array (of pre-fixed max size???)

???

This post has been edited by David W: 04 July 2013 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 210
  • Joined: 06-July 12

Re: How to read numbers from a file and load them into an object?

Posted 04 July 2013 - 05:07 PM

View PostDavid W, on 04 July 2013 - 04:58 PM, said:

what is you file structure to look like ...

please supply an example

Edit: Oh ... I see it is just a list of int's (separated by ws <white-space> ) ...

That you wish to read into an array (of pre-fixed max size???)

???


yes
Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: How to read numbers from a file and load them into an object?

Posted 04 July 2013 - 05:20 PM

View Postjordannn15, on 04 July 2013 - 08:07 PM, said:

...yes



const char* IN_FILE = "in_ints.txt";
const int MAX_SIZE = 100;

// main //

ifstream fin( INFILE );
if( fin )
{
   int ary[MAX_SIZE]; // get memory to hold int's
   int i = 0;
   while( i < MAX_SIZE && fin >> ary[i] ) ++i;
   fin.close();

   // do stuff with int's in ary ...

}
else cout << "\nThere was a problem opening file " << IN_FILE << endl;



This post has been edited by David W: 04 July 2013 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1