could I ask for advice? Please, could someone give an example of code, which deletes spaces from lines of the first text file and saves “new text without spaces” into the second file. I understand how it be probably working, but I can not write it, because i am beginner in programing. Thanks for any advice.
My codes:
//read csv file
void readCSV(istream &input, vector< vector<string> > &output)
{
string csvLine;
while(getline(input, csvLine) )
{
istringstream csvStream(csvLine);
vector<string> csvColumn;
string csvElement;
while(getline(csvStream, csvElement) )
{
csvColumn.push_back(csvElement);
}
output.push_back(csvColumn);
}
}
//save all from csv to txt
void saveToTxt()
{
fstream file("file.csv", ios::in);
ofstream outfile;
outfile.open("file.txt");
typedef vector< vector<string> > csvVector;
csvVector csvData;
readCSV(file, csvData);
for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
{
for(vector<string>::iterator j = i->begin(); j != i->end(); ++j)
{
outfile<<*j<<endl;
}
//code for deleting spaces, what i found, but i can't implement to above codes, coz my programming skill are not big
string s;
while (getline( cin, s ))
{
s.erase(
remove_if(
s.begin(),
s.end(),
ptr_fun <int, int> ( isspace )
),
s.end()
);
cout<<s<<endl;
I love solutions which won’t qualify as a result for a homework assignment. Below is how I would write code for the specification, partly because I genuinely think that this is how it is to be done and partly to give others a bit of interesting reading. It contains all the necessary hints to create a teacher-friendly solution, too:
If you can’t use a C++ 2011 compiler you’ll need to replace the lambda function by an actual function with the same signature.