FileParser
v3.0.0
FileParser是一个.NET库,旨在逐条读取文本文件,将每行的内容保存到基本类型vars(int,double,double,string等)中。
Nullable 该项目天生有一个非常具体的目的:提供一种工具,可以使用该工具轻松地解析具有已知结构的文件,理想情况下是灵活且易于使用的C ++标准IO方法。
对于那些不明白我的意思的人,这是一个简单的用例(也是重新介绍):
给定以下input.txt 。
5 1.1 3.14159265 2.2265 5.5 10 fish一个简单的.cpp片段,如以下一个可以处理input.txt ,提供该文件作为标准输入源:
./myExecutable < input.txt > output.txt
# include < iostream >
# include < list >
# include < string >
int main ()
{
int _integer;
std::string _str;
std::list< double > _list;
double _auxdouble;
// Input start;
std::cin>>_integer;
for ( int i= 0 ; i<_integer; ++i)
{
std::cin>>_auxdouble;
_list. push_back (_auxdouble);
}
std::cin>>_str;
// Input end
// Data processing
// Output start
std::cout<<_integer<< " " ;
for ( const double & d : _list)
std::cout<<d<< " " ;
std::cout<<_str;
// Output end
return 0 ;
}似乎毫不费力地使用C ++处理这类简单的.txt文件,对吗?
好吧,使用c#的东西并不是那么直接,这就是为什么FileParser创建的原因:
using System ;
using System . Collections . Generic ;
using System . Globalization ;
using System . IO ;
using FileParser ;
namespace FileParserSample
{
class Program
{
static void Main ( string [ ] args )
{
var cultureInfo = new CultureInfo ( "en-US" ) ;
CultureInfo . DefaultThreadCurrentCulture = cultureInfo ;
List < double > listDouble = new List < double > ( ) ;
string str ;
// Input start
IParsedFile file = new ParsedFile ( "SimpleInput.txt" ) ;
IParsedLine firstLine = file . NextLine ( ) ;
int _integer = firstLine . NextElement < int > ( ) ;
for ( int i = 0 ; i < _integer ; ++ i )
listDouble . Add ( firstLine . NextElement < double > ( ) ) ;
str = firstLine . NextElement < string > ( ) ;
// Input end
// Data Processing
// Output start
StreamWriter writer = new StreamWriter ( ".. \ CSharpSimpleOutput.txt" ) ;
using ( writer )
{
writer . WriteLine ( _integer + " " + string . Join ( null , listDouble ) ) ;
}
// Output end
}
}
} 我已经尽力创建一个描述FileParser API的Wiki。
除Wiki外,使用的一些真实(自己的)项目是:
如果其他人碰巧使用Fileparser,我将很乐意接受建议并解决任何疑问。
只是打开一个问题:)