9 Replies - 7804 Views - Last Post: 04 September 2013 - 06:17 AM Rate Topic: -----

#1 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

read file into dynamic array

Posted 18 April 2010 - 04:26 PM

Hi all,
I'm trying to read a txt file that looks like this:

john alan 01 31 1985 5.95

into a dynamic array. The console output is displaying the memory address instead of the actual data thats being stored. Any help would be greatly appreciated.

#include <iostream>
#include <fstream>												
#include <iomanip>												
#include <string>												
using namespace std;	 

struct EmployeeData
{
	string firstName;
	string lastName;
	int month;
	int day;
	int year;
	double hourlyPayRate;
};

int openFileTest(ifstream& fin, int& numberOfLines);
void loadEmployeeData(ifstream& fin, EmployeeData*, unsigned int);
void displayEmployeeData(EmployeeData*, unsigned int);

/*****************************************************************
	main() function definition
*****************************************************************/

int main()
{
	int numberOfLines = 0;
	ifstream fin;
	openFileTest(fin, numberOfLines);
	EmployeeData* pEmployeeData = new EmployeeData[numberOfLines];
	loadEmployeeData(fin, pEmployeeData, numberOfLines);
	displayEmployeeData(pEmployeeData, numberOfLines);
	delete [] pEmployeeData;
	cout << endl;
	system("pause");
	return 0;							
}

int openFileTest(ifstream& fin, int& numberOfLines)
{
	string temp;
	fin.open("employees.dat");
	if(fin.fail())
	{
		cout << "Error, could not open the file." << endl;
		fin.clear();
		return -1;
	}
	else
	{
		while(getline(fin, temp))
		{
			numberOfLines++;
		}
	}
	fin.close();
	return numberOfLines;
}

void loadEmployeeData(ifstream& fin, EmployeeData* pEmployeeData, unsigned int numberOfLines)
{
	unsigned int i = 0;
	while (!fin.eof())
	{
		fin >> pEmployeeData[i].firstName;
		fin >> pEmployeeData[i].lastName;
		fin >> pEmployeeData[i].month;
		fin >> pEmployeeData[i].day;
		fin >> pEmployeeData[i].year;
		fin >> pEmployeeData[i].hourlyPayRate;
		i++;
	}
	return;
}

void displayEmployeeData(EmployeeData* pEmployeeData, unsigned int numberOfLines)
{
	unsigned int i = 0;
	cout << "<------------------------------------------------------------------------------>\n";
	cout << "\t\tThe employee data file contains the following:\n\n";
	cout.setf(ios_base::left, ios_base::adjustfield);
	cout.setf(ios_base::showpos);
	cout.setf(ios_base::showpoint);
	cout << "First Name";
	cout.width(16);
	cout << "\tLast Name";
	cout.width(16);
	cout << "Birthdate";
	cout.width(16);
	cout << "Pay Rate";
	while (i < numberOfLines)
	{
		cout << setw(2) << pEmployeeData[i].firstName;
		cout << setw(2) << pEmployeeData[i].lastName;
		cout << setw(2) << pEmployeeData[i].month;
		cout << setw(2) << pEmployeeData[i].day;
		cout << setw(2) << pEmployeeData[i].year;
		cout << setw(2) << pEmployeeData[i].hourlyPayRate;
		i++;
	}
	return;
}


This post has been edited by cyfuzhi: 18 April 2010 - 04:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: read file into dynamic array

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: read file into dynamic array

Posted 18 April 2010 - 05:21 PM

The main problem is that when you count the lines in the file you close the file.

So when you try to copy data to the structure all of the extractions fail because the file is not open.

I've moved the file open and close to main():

using namespace std; 

struct EmployeeData
{
	string firstName;
	string lastName;
	int month;
	int day;
	int year;
	double hourlyPayRate;
};

int openFileTest(ifstream&);

void loadEmployeeData(ifstream&, EmployeeData*);

void displayEmployeeData(EmployeeData*, int);

int main()
{
	int numberOfLines = 0;

	ifstream fin;

	fin.open("employees.dat");
	if(!fin.good())
	{
		cout << "Error, could not open the file." << endl;
		fin.clear();
		return -1;
	}

	numberOfLines = openFileTest(fin);

	fin.clear(); // read to EOF, reset error bits

	fin.seekg(ios::beg); // seek back to beginning of file
	
	EmployeeData* pEmployeeData = new EmployeeData[numberOfLines];

	loadEmployeeData(fin, pEmployeeData);

	displayEmployeeData(pEmployeeData, numberOfLines);

	delete [] pEmployeeData;

	fin.close(); // close file

	cout << endl;

	system("pause");
	return 0;
}
int openFileTest(ifstream& fin)
{
	string temp;
	int numberOfLines = 0;

	while(getline(fin, temp))
	{
		numberOfLines++;
	}

	return numberOfLines;
}

void loadEmployeeData(ifstream& fin, EmployeeData* pEmployeeData)
{
	while (fin 
		>> pEmployeeData->firstName
		>> pEmployeeData->lastName
		>> pEmployeeData->month
		>> pEmployeeData->day
		>> pEmployeeData->year
		>> pEmployeeData->hourlyPayRate
		)
	{
		pEmployeeData++;
	}

	return;
}

void displayEmployeeData(EmployeeData* pEmployeeData, int numberOfLines)
{
	cout << "<------------------------------------------------------------------------------>\n";
	cout << "\t\tThe employee data file contains the following:\n\n";
	cout.setf(ios_base::left, ios_base::adjustfield);
	cout.setf(ios_base::showpos);
	cout.setf(ios_base::showpoint);
	cout << "First Name";
	cout.width(16);
	cout << "\tLast Name";
	cout.width(16);
	cout << "Birthdate";
	cout.width(16);
	cout << "Pay Rate" << endl;

	for (int i = 0; i < numberOfLines; i++)
	{
		cout << setw(16) << pEmployeeData[i].firstName;
		cout << setw(14) << pEmployeeData[i].lastName;
		cout << setw(2)  << pEmployeeData[i].month;
		cout << setw(2)  << pEmployeeData[i].day;
		cout << setw(11) << pEmployeeData[i].year;
		cout << setw(16) << pEmployeeData[i].hourlyPayRate;
	}

	return;
}



Note that since the file has been read to EOF, the eofbit is set. Before any other IO operation on the file will succeed, the eofbit needs to be reset. Also since you want to read the file again, you need to seek back to the beginning of the file.

Output with the above code:

<------------------------------------------------------------------------------>

                The employee data file contains the following:

First Name      Last Name      Birthdate       Pay Rate
john            alan          +1+31+1985      +5.95000
Press any key to continue . . .


Was This Post Helpful? 1
  • +
  • -

#3 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: read file into dynamic array

Posted 18 April 2010 - 10:57 PM

Thank you n8wxs!

If there were multiple lines in the txt file, what iomanip function could be used to display everything in the correct columns?
Was This Post Helpful? 0
  • +
  • -

#4 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: read file into dynamic array

Posted 18 April 2010 - 11:33 PM

ok, just had to add a endl and left manipulator.

Is there a manipulator for displaying leading zeros? such as 01 would be displayed instead of 1 for the month of January?
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: read file into dynamic array

Posted 19 April 2010 - 03:33 AM

View Postcyfuzhi, on 19 April 2010 - 03:33 PM, said:

Is there a manipulator for displaying leading zeros? such as 01 would be displayed instead of 1 for the month of January?


This is one way to do it
http://www.cplusplus...lators/setfill/
Was This Post Helpful? 0
  • +
  • -

#6 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: read file into dynamic array

Posted 19 April 2010 - 08:18 PM

this is the output i'm getting when i'm using setfill ('0') in this function.

void displayEmployeeData(EmployeeData* pEmployeeData, unsigned int numberOfLines)
{
	cout << "<------------------------------------------------------------------------------>\n";
	cout << "\t\tThe employee data file contains the following:\n\n";
	cout.setf(ios_base::left, ios_base::adjustfield);
	cout.setf(ios_base::showpoint);
	cout << "First Name";
	cout.width(16);
	cout << "\tLast Name";
	cout.width(16);
	cout << "BirthDate";
	cout.width(16);
	cout << "Pay Rate" << endl;

	for (unsigned int i = 0; i < numberOfLines; i++)
	{
		cout << setw(16) << left  << pEmployeeData[i].firstName;
		cout << setw(14) << left  << pEmployeeData[i].lastName;
		if (pEmployeeData[i].month < MAX_NUMBER)
		{
			cout << setfill ('0') << setw(2) << right << pEmployeeData[i].month << "/";
		}
		else 
			cout << setw(2) << right << pEmployeeData[i].month << "/";
		if (pEmployeeData[i].month < MAX_NUMBER)
		{
			cout << setfill ('0') << setw(2) << right << pEmployeeData[i].day << "/";
		}
		else 
			cout << setw(2) << right << pEmployeeData[i].day << "/";
		cout << setw(2)  << right << pEmployeeData[i].day << "/";
		cout << setw(4)  << right << pEmployeeData[i].year;
		cout << setw(15) << right << setprecision(3) << pEmployeeData[i].hourlyPayRate;
		cout << endl;
	}
	return;
}



<------------------------------------------------------------------------------>

The employee data file contains the following:

First Name Last Name BirthDate Pay Rate
John Smith 01/23/23/1985000000000005.00
John000000000000Smith00000000012/23/23/1985000000000005.00
John000000000000Smith00000000011/23/23/1985000000000005.00
John000000000000Smith00000000011/23/23/1985000000000005.00

Press any key to continue . . .

Any idea why 0's are being filled everywhere else?
Was This Post Helpful? 0
  • +
  • -

#7 traurick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-September 13

Re: read file into dynamic array

Posted 03 September 2013 - 08:07 AM

Hello,

Found this article due to the same problem. I want to read from a text file string and numbers. I used more or less the structure found here. I just started with C++, so I am sorry.
The output delivers -6.27744e+066 instead of the strings and numbers.

Here the code.

include <iostream>
#include <fstream>                                               
#include <iomanip>                                               
#include <string>   

using namespace std;    
	 
struct FluidProperties
	{
	 string Component;    
	 double MolarMass;
	 double CriticalPressure;	    
	 double CriticalTemperature;
	 double CriticalZ;
	 double AccentricFactor;
	 double BoilingTemperature;
	 double GaseousDensity;
	 double LiquidDensity;
    
	};

int openFileTest(ifstream&);

void loadFluidProperties(ifstream&, FluidProperties*);
void displayFluidProperties(FluidProperties*, int);

int main ()
{
	double test=0;
	int numberOfLines = 0;
	ifstream fin;
	fin.open("properties.txt");
	if(!fin.good())
	{
       cout << "Error, could not open the file." << endl;       
	   fin.clear();
	   return -1;
	}
	numberOfLines = openFileTest(fin);
	fin.clear();
	fin.seekg(ios::beg);

	FluidProperties* pFluidProperties = new FluidProperties[numberOfLines];
	loadFluidProperties(fin, pFluidProperties);
	displayFluidProperties(pFluidProperties, numberOfLines);
	delete [] pFluidProperties;
	fin.close();

	
	cout<<numberOfLines<<endl;
	
	system("pause");

	return 0; 
}

int openFileTest(ifstream& fin)
	{
		string temp, dummyLine;
		int numberOfLines = 0;
		getline(fin, dummyLine);
		getline(fin, dummyLine);
		getline(fin, dummyLine);
		while(getline(fin,temp))
			{ 
				numberOfLines++;
			}
		return numberOfLines;
	}

void loadFluidProperties(ifstream& fin, FluidProperties* pFluidProperties)
{
	while (fin
		>>pFluidProperties->Component
		>>pFluidProperties->MolarMass
		>>pFluidProperties->CriticalPressure
		>>pFluidProperties->CriticalTemperature
		>>pFluidProperties->CriticalZ
		>>pFluidProperties->BoilingTemperature
		>>pFluidProperties->GaseousDensity	
		>>pFluidProperties->LiquidDensity
		)
	{
		 pFluidProperties++;

	}

	return ;
}

void displayFluidProperties(FluidProperties* pFluidProperties, int numberOfLines)
{
	
	cout<<"Test"<<endl;
	for (int i = 0; i < numberOfLines; i++)
    {
        cout <<  pFluidProperties[1].Component;
        cout <<  pFluidProperties[1].MolarMass;
        cout <<  pFluidProperties[i].CriticalPressure;
	    cout <<  pFluidProperties[i].CriticalTemperature;
        cout <<  pFluidProperties[i].CriticalZ;
		cout <<  pFluidProperties[i].BoilingTemperature;
	    cout <<  pFluidProperties[i].GaseousDensity;
		cout <<  pFluidProperties[i].LiquidDensity;
	    }
	 
	    return;
}



I use this for learning, Thank you.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: read file into dynamic array

Posted 03 September 2013 - 11:02 AM

for (int i = 0; i < numberOfLines; i++)
{
    cout <<  pFluidProperties[1].Component;
    cout <<  pFluidProperties[1].MolarMass;
    cout <<  pFluidProperties[i].CriticalPressure;


One thing is you need to look closely at what's in those []s there.

You should also be putting some sort of formatting in there, like an <<endl after each element.
Was This Post Helpful? 0
  • +
  • -

#9 traurick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-September 13

Re: read file into dynamic array

Posted 03 September 2013 - 10:46 PM

View PostJackOfAllTrades, on 03 September 2013 - 11:02 AM, said:

for (int i = 0; i < numberOfLines; i++)
{
    cout <<  pFluidProperties[1].Component;
    cout <<  pFluidProperties[1].MolarMass;
    cout <<  pFluidProperties[i].CriticalPressure;


One thing is you need to look closely at what's in those []s there.

You should also be putting some sort of formatting in there, like an <<endl after each element.



thank you for quick reply. I know that the output format is not good but anyway i should get correct numbers or ?

void loadFluidProperties(ifstream& fin, FluidProperties* pFluidProperties)
{
	while (fin
		>>	pFluidProperties->Component
		>>	pFluidProperties->MolarMass
		>>	pFluidProperties->CriticalPressure
		>>	pFluidProperties->CriticalTemperature
		>>	pFluidProperties->CriticalZ
		>>	pFluidProperties->BoilingTemperature
		>>	pFluidProperties->GaseousDensity	
		>>	pFluidProperties->LiquidDensity
		)
	{
		 pFluidProperties++;

	}

	return ;
}


I am not sure if this part of the code works, its an elegant solution and i want to use it, if it works.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: read file into dynamic array

Posted 04 September 2013 - 06:17 AM

Quote

I am not sure if this part of the code works, its an elegant solution and i want to use it, if it works.

Then I suggest you test the program and see if it works. If you don't understand the program you "found" I suggest you consider writing your own solution so you understand what is happening.

And please don't resurrect such old topics, open your own topic and link to the original.


Jim

This post has been edited by jimblumberg: 04 September 2013 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1