Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reuse ifstream infile 1

Status
Not open for further replies.

tmcneil

Technical User
Nov 17, 2000
294
US
I've declared ifstream infile in a class and use it in a method to open a txt file for reading. Once I am finished reading data from it and parsing it into two different files, I then close it in that same method. However, I cannout reuse it to open a different file in another method. My file check just states that the file does not exist for opening. Well, it does, since I just created it in the same directory. Is it possible to clear out ifstream infile from its association to the 1st file it opened. Since I don't want to introduce another variable to open another file. I like to reuse my code as mush a possible to keep it clean. Any ideas?

In the meantime, I have been using another variable for opening a file, but I ultimately want to reuse te infile.

Todd
 
That should work. I'll try it.

Thanks!

Todd
 
Also, I was wondering if you knew anything about arrays. I have a class declared called, employee_record:
Code:
class employee_record
{  
    public:
        char  full_name[24];
	    float weekly_sales;
        char  department;  
        float bonus_amount;
	    float gross;
	    float total_taxes;
	    float net_wages;
}; //end employee_record class

I have a 2nd class that include an array and some methods.
Code:
class employee_ary
{                              
   private:
	  class employee_record employees [50];
	  int valid_count;
      //int count;
	  ifstream infile;
	  ofstream out_good_file;
      ofstream out_error_file;

   public:
	  employee_ary (void);
      void process_input_file (void);
	  int validate_dept (char ch);
      int validate_sales (float sales);
      void error_report (void);
      void valid_report (void);
      //void comp_bonus (void);
      //void comp_total_wages (void);
      //void comp_taxes (void);
      //void comp_net_wages (void);
      void print_records (char ch);
      //void alphabetize (void);
      //void write_to_binary_file (void);
      ~employee_ary (void);
}; //end employee_ary class

The declared array, class employee_record employees [50];, will be sued to store information from a file that I am reading from. What I am having trouble with is the corerct syntax to populate the full_name portion of the array with the concattenate value of lname and fname.
Code:
void employee_ary::valid_report(void)
{   
    char dept;
    float weekly_sales;
    char fname[10], lname[13];
    valid_count = 0;

    //open infile for reading
    infile.open ("good_file.txt", ios::in);
    if (infile.fail()) //see if file opened successfully
    {
        cout << "File, good_file.txt, could not be opened!\n\n";
        infile.clear ();
	    system ("pause");   //so the message can be read
	    exit (1);  // abort the program
	}

	infile >> fname >> lname >> weekly_sales >> dept;
    while (!infile.eof())
    {
        
        strcat(lname, " ");  //concattenate a space with lname
        strcat(lname, fname);  //concattenate a fname with lname and space
        cout << lname << endl;
        //strcpy(employees[valid_count], lname);          //store lname read in array
        employees[valid_count] = lname;       //store lname read in array
        //strcpy(employees[valid_count], weekly_sales);   //store weekly_sales read in array
        //strcpy(employees[valid_count], dept);           //store dept read in array

        valid_count++;

        //read next record
        infile >> fname >> lname >> weekly_sales >> dept;
    }

    cout << endl << DASHES << endl << endl;
    cout << "\t\t\t     Number of Valid Records = " << valid_count <<  "." << endl;

    infile.close ();
    infile.clear ();
} //end valid_report

How do I properly store the lname to the full_name portion of the employees array? I also have to add the dept and sales info read from the file.

Thanks,
Todd
 
Never mind, I figure it out after a few more tries.
Code:
//copy lname read to full_name
strcpy(employees[valid_count].full_name, lname);
//append space after last name stored
strcat(employees[valid_count].full_name, " ");
//append first name after space
strcat(employees[valid_count].full_name, fname);
//store weekly_sales to weekly_sales
employees[valid_count].weekly_sales = weekly_sales;
//store dept read in array, but use lowercase value
employees[valid_count].department = (tolower(dept));

Todd
 
> char full_name[24];
Why not use
[tt]std::string full_name;[/tt]

Then you get a nice safe string with no buffer overflow and the ability to do something like
Code:
employees[valid_count].full_name = lname + " " + fname;

--
 
salem,

That's a great suggestion, but I handed in my assignment already. It took me like 3 full days of coding and documentation. We have to write a top-down design document for every program we right and have to have to document all our programs. That part takes longer than writing the code. However, thanks for the help everyone. We'll see what kind of grade that I get.

Todd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top