Read lines from file based on first character of each line

If the first character of a line is 1, I need to read into variables f

Page 1 of 1

2 Replies - 1796 Views - Last Post: 31 January 2010 - 12:52 PM Rate Topic: -----

#1 muffintheman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-December 09

Read lines from file based on first character of each line

Posted 30 January 2010 - 03:17 PM

Hello,

I am new to C++, and I need to read from a file like this:

2 Jeffery Johnson A001 1400
1 Jeffery Smith B002 3 15
2 Jessica Smith A303 2200
2 Jeremy Suing X001 50
1 Sally Smith B001 9 14
2 John Johnson B010 1450
1 Sarah Smith B200 5 25
1 John Doe A678 4 10
1 Tad Jacobs X222 7 13
2 Ella Roberts B501 2400
1 Ellen Smith A501 3 40
2 Bobby Smith B555 240

If the first character is a 1, then reading the line should be something like this:

ifstream filein;
filein >> employeetype >> firstname >> lastname >> employeeID >> hoursperday >> hourlyrate;



but if the first character is a 2, then reading the line should be something like this:

filein >> employeetype >> firstname >> lastname >> employeeID >> monthlypay;



I used variables in this example such that it is easy to understand what they are referring to. Also, as you can see, there is no order of 1's and 2's. I realize that if I didn't have to differentiate between the different employee types, I could do something like this:

while(filein >> something1 >> something2 >> something3 >> something4 >> something5)
{
	   use somethings.
}



Thanks for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Read lines from file based on first character of each line

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,166
  • Joined: 14-September 07

Re: Read lines from file based on first character of each line

Posted 30 January 2010 - 03:34 PM

Something like this:

ifstream file;

//open file, etc..
int flag;

//set up a loop
   file >> flag;
   if (flag == 1) 
	  //do it one way
   else if (flag == 2)
	  //do it the other way


Was This Post Helpful? 1
  • +
  • -

#3 muffintheman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-December 09

Re: Read lines from file based on first character of each line

Posted 31 January 2010 - 12:52 PM

Fantastic! Thank you much, KYA! My code ended up looking like this:

if(fin)
	{
		int flag;

		while(fin >> flag)
		{
			if(flag==1)
			{
				fin >> tempfname >> templname >> tempempID >> temphrsday >> temphrlyrate;
				// Prints out to check
				cout << flag <<" "<< tempfname <<" "<< templname <<" "<< tempempID <<" "<< temphrsday <<" "<< temphrlyrate << endl;
			}
			else if(flag==2)
			{
				fin >> tempfname >> templname >> tempempID >> tempmpay;
								// Prints out to check
				cout << flag <<" "<< tempfname <<" "<< templname <<" "<< tempempID <<" "<< tempmpay << endl;
			}
		}
	}



It works! Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1