Read file and extract text

May 4, 2010 at 10:20pm
Hi,

I am new to C++, I was wondering if someone could help me with this.

I have a file and it has 20 records. The format is

itemname id wt code coupon discount
ABC123Efg 234 232 171 -21 -3332324234
ABC123Efh 3 2 17121 -21231 -3332324234
...
XYZ123Efg 234 232 171 -21 -3332324234

From the user I get 2 arguments 'ABC' and 'efg'

For the given arguments, I need to retrieve its corresponding values (only the last 2 columns)

My idea: (kindly correct me if I am wrong)

open a file,
read a line,
Store them in a class (which has 6 members: itemname,id,code,wt,discount,coupon)
use method to extract 3 letters of itemname and compare it with user argument1 and repeat the same for user argument2.
If matches, fetch its discount and coupon values.

My doubt: I am not sure how to spilt the values from a line and store in class

Here is what I have done.

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

class temp
{
string itemName;
int itemId;
int itemCode;
int itemWt;
float coupon;
float discount;
}

int main () {
string line;
ifstream myfile ("item.txt");
// string text ="AL"; // search pattern
temp tempObj;

if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
// line.find(text);
cout<<line;
}
myfile.close();
}
else cout << "Unable to open file";
system("pause");
return 0;
}

Thanks.

May 4, 2010 at 11:50pm
Use the right tool for the job: Perl and regular expressions.
May 5, 2010 at 12:32am
nah... try one of the string functions... like find , get the position, read out everything that follows and convert it to the format of your desire:)... check the reference http://cplusplus.com/reference/string/string/find/
May 5, 2010 at 9:22am
Thanks PanGalactic for your suggestion, but I must use only C++

Hi Incubbus,

As my user args will be XYZ and Efg, I used substr to get XYZ and Efg.

XYZ123Efg 234 232 171 -21 -3332324234 (file format)


But I also need to get -21 and -3332324234 ( the first 4 columns will not have same number of characters in other records.

Could you suggest me as how to obtain those two column values..?

Thanks
May 5, 2010 at 11:18am
Since your file consists of only 20 entries, linear searching (i.e. checking every entry in the file for a match) is a good approach. Get itemname in a string using operator >> with your ifstream object. Then check if it matches the args given by the user. If not, use the ignore function of your ifstream object to get rid of the rest of the line and move on to the next. If you have a match then simply use operator >> to get the rest of the data into your variables.
May 5, 2010 at 6:45pm
Hey roshi,

Yea, I understand that. But each record has 6 columns, I have the value for the 1st column. I need to know how to get 5th and 6th column values...?

Thanks
May 5, 2010 at 9:03pm
You might want to consider this kind of approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <string>
#include <fstream>


class record
{
	std::string itemName;
	int itemId;
	int itemCode;
	int itemWt;
	float coupon;
	float discount;
};

int main()
{
	std::ifstream myfile("item.txt");

	while(myfile)
	{
		record rec; // crreate a blank record

		myfile >> rec.itemName;
		myfile >> rec.itemId;
		myfile >> rec.itemCode;
		myfile >> rec.itemWt;
		myfile >> rec.coupon;
		myfile >> rec.discount;

		if(myfile) // no errors?
		{
			// Do something with record
		}
	}

	return 0;
}
May 5, 2010 at 11:00pm
Thanks Galik. I am encountering some errors. Let me explain what I did...

XYZ123Efg 234 232 171 -21 -3332324234 (file format)

str userArg1 = "XYZ";
str userArg2 = "Efg";
// i use getline to read each line from the file.

itemXYZ = line.substr(0,userArg1.length()); // gave me XYZ
itemEfg = line.substr(6,userArg2.length()); // gave me Efg

if((userArg1==itemXYZ )&&(userArg2==itemEfg))
{
//print rec.discount , rec.coupon
//I need to send these 2 arguments to another function
myfile.close();
}

Also got an error saying out of range exception for substr.
I tried changing the starting position, but it still throws me that error.

I got another idea, was not sure how to implement it...
// convert string to string array
Eg. string line = "XYZ123Efg 234 232 171 -21 -3332324234"
string strArray[0] ="XYZ123Efg "
string strArray[1] = "234"

Any suggestion?
May 6, 2010 at 8:43am
// i use getline to read each line from the file.

Don't. Do it like I said and like Galik showed you. Use the >> operator of your ifstream object.
Last edited on May 6, 2010 at 2:39pm
May 6, 2010 at 2:30pm

Checking the first column:
1
2
3
4
5
6
7
8
9
10
	std::string arg1 = "XYZ";
	std::string arg2 = "Efg";

	if(rec.itemName.find(arg1) == 0
	&& rec.itemName.find(arg2) == rec.itemName.length() - arg2.length())
	{
		// Do something with record
		std::cout << rec.itemName << std::endl;
	}


I would not read the file line by line, but field by field using the >> operator.
Topic archived. No new replies allowed.