我可以征求意见吗?请有人给出一个代码示例,它从第一个文本文件的行中删除空格并将“没有空格的新文本”保存到第二个文件中。我了解它可能是如何工作的,但我不会写它,因为我是编程的初学者。感谢您的任何建议。我的代码:
//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;