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,我將很樂意接受建議並解決任何疑問。
只是打開一個問題:)