我正在尝试将逗号分隔文件中的数据读入字符串。此外,我希望从字符串中删除多余的空格。
我已经设法实施了一个有效的解决方案,但我感兴趣的是,如果能够更有效地完成这项工作。我的主要目标是使用默认字符串大小(std::string lName( 100, 0 );
)删除临时字符串初始化,因为文件中的数据长度可变。
另外,如果你有一些建设性的建议我会很感激。
我正在使用MS Visual Studio 2008。
以下是 SSCCE 示例:
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
// helper function for removing extra spaces
void removeSpace( std::string &str )
{
str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
}
int main()
{
//========== let us construct a test file =====================//
//===== format is Last Name, First Name, Gender, Color, Birth Date =======//
std::ofstream os;
os.open( "test.txt" );
// first row
os << " Smith ," << " John ," << " Male , "
<< " Green , " << " 6 / 7 / 1960 \n";
// second row
os << " Mortensen ," << " Mike ," << " Male , "
<< " Red , " << "5/5/ 1975 \n";
// third row
os << " Johnson ," << " Ann ," << " Female , "
<< " Blue , " << " 4/ 4 /1985 \n";
os.close();
// now let us read data from it
std::ifstream g;
g.open( "test.txt" );
if( g.is_open() )
{
while( !g.eof() )
{
// temporary strings
std::string lName( 100, 0 );
std::string fName( 100, 0 );
std::string gen( 100, 0 );
std::string clr( 100, 0 );
std::string date( 100, 0 );
// get data from file
g.getline( &lName[0], 100, ',' );
g.getline( &fName[0], 100, ',' );
g.getline( &gen[0], 100, ',' );
g.getline( &clr[0], 100, ',' );
g.getline( &date[0], 100 );
// remove extra spaces from strings
removeSpace( lName );
removeSpace( fName );
removeSpace( gen );
removeSpace( clr );
removeSpace( date );
// display the result
std::cout << lName.c_str()
<< ' ' << fName.c_str()
<< ' ' << gen.c_str()
<< ' ' << clr.c_str()
<< ' ' << date.c_str()
<< std::endl;
//cleanup
lName.clear();
fName.clear();
gen.clear();
clr.clear();
date.clear();
}
g.close();
}
// since our SSCCE example is done, let us delete the test file
if( 0 != std::remove( "test.txt" ) )
std::cout << "Couldn't delete test file!\n\n";
else
std::cout << "Successfully deleted test file!\n\n";
return 0;
}
根据会员 WhozCraig 的建议,我得以改进。为简洁起见,我只会发布while
循环:
while( !g.eof() )
{
// temporary strings
std::string line;
if( ! std::getline( g, line ) )
break;
std::istringstream iss(line);
while( iss )
{
std::string str;
if ( ! std::getline( iss, str, ',' ) )
break;
// remove excess spaces
removeSpace( str );
// output the result
std:: cout << str.c_str() << ' ';
}
std::cout << std::endl;
}
答案 0 :(得分:0)
对于琐碎的逗号分隔(与真实的CSV格式相反,这远远超出原始问题的范围),明智地使用std::getline
和std::istringstream
可能会让你失望,特别是在逐行资格方面。我冒昧地fix the while-conditon as well,
以下完全修改的示例。祝你好运。 (并且+1为你使用remove-erase-idiom来剥离你的空间。)
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
// helper function for removing extra spaces
void removeSpace( std::string &str )
{
str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
}
int main()
{
//===== format is Last Name, First Name, Gender, Color, Birth Date =======//
std::ofstream os;
os.open( "test.txt" );
// first row
os << " Smith ," << " John ," << " Male , "
<< " Green , " << " 6 / 7 / 1960 \n";
// second row
os << " Mortensen ," << " Mike ," << " Male , "
<< " Red , " << "5/5/ 1975 \n";
// third row
os << " Johnson ," << " Ann ," << " Female , "
<< " Blue , " << " 4/ 4 /1985 \n";
os.close();
// now let us read data from it
std::ifstream g;
g.open( "test.txt" );
if( g.is_open() )
{
std::string line;
while( std::getline(g, line) )
{
std::istringstream iss(line);
std::string lName, fName, gen, clr, date;
if (std::getline(iss, lName, ',') &&
std::getline(iss, fName, ',') &&
std::getline(iss, gen, ',') &&
std::getline(iss, clr, ',') &&
std::getline(iss, date))
{
// remove extra spaces from strings
removeSpace( lName );
removeSpace( fName );
removeSpace( gen );
removeSpace( clr );
removeSpace( date );
// display the result
std::cout << lName
<< ' ' << fName
<< ' ' << gen
<< ' ' << clr
<< ' ' << date << '\n';
}
}
g.close();
}
// since our SSCCE example is done, let us delete the test file
if( 0 != std::remove( "test.txt" ) )
std::cout << "Couldn't delete test file!\n\n";
else
std::cout << "Successfully deleted test file!\n\n";
return 0;
}
<强>输出强>
Smith John Male Green 6/7/1960
Mortensen Mike Male Red 5/5/1975
Johnson Ann Female Blue 4/4/1985