void query::load_query(constchar* filename){
string lines;
int count = 0;
ifstream file (filename);
//READ OPERATION--ONE EXECUTION ONLY
if(file.is_open())
{
while(! file.eof() )
{
getline (file,lines);
//MAKE ALL STRING LOWERCASE
transform(lines.begin(), lines.end(), lines.begin(), (int(*)(int)) tolower);
//CONVERT STRING TO CHAR*
char * clines = newchar [lines.length()+1];
strcpy (clines, lines.c_str());
//COUNTING + COPYING OPERATION
cut_string x(clines);
vector<string> flds = x.split();
for (int k = 0; k < flds.size(); k++) {
cout<<flds[i]<<endl;
count++;
};
}
file.close();
}
total = count;
};
the 'flds' on the code above has vector <string> data type, i was able to output it using cout but i don't know how to copy its value to another vector <string>...whenever i tried to do that using my own way, the compiled program ended up crashing...do you know how to solve this? thx before :)
You need to compile and see what is happening every now and then, because I don't think your data structures will work the way you think they are.
You are also making some elementary mistakes with file handling -- which is understandable, because file handling is no where near as simple as people think it is.
1 2 3 4 5 6 7 8 9 10 11 12 13
ifstream file(filename);
if (file)
{
string line;
while (getline(file, line))
{
transform(line.begin(), line.end(), (int(*)(int))tolower);
// split each line into fields here...
// I'm not sure how you are doing this, so please tell me more.
}
}
As noted above, I'm not sure what the goals of this assignment are. How is each line split into fields? On whitespace? Or by some other criteria?
Are you trying to get the total number of fields in the file? Or average per line? Or what?
@sharma
i'm sorry i was wrong, it should be k, not i :)
@duoas
actually i use alternative version of strtok() (i found the code here, its name is splitstring.cpp), and i use it to split only the words (without comma, space, etc) on every line that is being read...the vector <string> flds here can only temporarily store one line for each process, so i have to copy the contents of it to another vector <string>, and the main problem here is i am unable to copy it because the size of flds depends on the line in the text file that is being read, and i want to store the whole splitted text into my new vector <string>....i have tried to use resize() for the new vector <string> but it still keep making the program crash...
i also don't know how to copy vector from certain elements..
hope this can explain anything, i'm sorry for my bad english..