Hi,
Here's my code

#include<iostream>
#include<fstream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

int cIndex(char *x,char **featlist,int len)
{
	int i;
	for(i=0;i<len;i++)
	if(strcmp(x,featlist[i])==0)
	return i;
}	
	
int main(int argc,char *argv[])
{
	std::ifstream featFile(argv[1],ios::in);
	std::ifstream trainFile(argv[2],ios::in);
//	std::ofstream svmFile(argv[3],ios::out);
	char line[20000];
	char feat[100];
	char **featlist;
	int i=0,len=0,fl=0,j;
	char* x;
	int valnum;	
	while(featFile.good())
	{
		featFile.getline(feat,100,'\n');
		if(featFile.eof()) break;
		len++;
	}
	featFile.clear();
	featFile.seekg(0,ios::beg);
//	cout<<len<<endl;
	featlist=new char*[len];
	for (i = 0; i < len; ++i)
        featlist[i] = new char[100];
	i=0;
	while(featFile.good())
	{
		featFile.getline(feat,100,'\n');
		if(featFile.eof()) break;
		strcpy(featlist[i],feat);
		i++;
	}
	while(trainFile.good())
	{
		trainFile.getline(line,20000,'\n');
		x=strtok(line," ");
		valnum=cIndex(x,featlist,len);
		cout<<valnum<<";";		
		
	}
	trainFile.close();	
		
}

my featFile is of the form

yasuhiro
ali
mdbl
consum
dollar
unrest
protest
mile
mill
ventur
semiconductor
heller
authoris

and my trainFIle is of the form

acq 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 26:0 27:0 28:0 29:0 30:0 31:0 32:0 33:0 34:0
acq 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 26:0 27:0 28:0 29:0 30:0 31:0 32:0 33:0 34:0

so i basically have to read the training file and replace the first name with an array index...the array index is formed using the featFile...the call to cIndex keeps seg faulting after some iterations.

> The function cIndex isn't always returning a value:

int cIndex(char *x,char **featlist,int len)
{
	int i;
	for(i=0;i<len;i++)
            if(strcmp(x,featlist[i])==0)
	        return i;
        /* So what happens when featlist[i] and x aren't equal ? */
        /* You should add a return code to check whether they were equal or not */
        /* Let's say if they weren't equal you return -1 for example */
}

> Your code compiled and ran fine for me (what OS and compiler are you using?)

> Running your code gave me the following output: -7;-7; , is that correct? (I'm using the same file contents for featFile and trainFile as you provided)

> I can't seem to find any place in your code where you're deallocating the assigned memory to featlist , that's a serious memory leak :) ...

To release the allocated memory (of your featlist ):

for(int i = 0; i < len; i++)
     delete[]  ptr[i];

So, no more memory leaks :P

ok...It was the return value! thanks a lot! memory leaf fixed :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.