![]() |
#2
rjsp2011-11-05 09:11
![]() #include <iostream> #include <fstream> #include <string> using namespace std; const char* inputfilename = "D:\\sources\\cpp001\\input.txt"; const char* outputfilename = "D:\\sources\\cpp001\\output.txt"; const char* idmatch = "aaaa"; const char* dmmatch = "dm0"; // 改为 "" 或 0 意味着所有dm都拷贝 std::string string_trim( const std::string& str ); int main( void ) { ifstream input( inputfilename ); if( !input ) return -1; ofstream output( outputfilename ); if( !input ) return -2; string id, idraw; for( string line; getline(input,line); ) { size_t index = line.find('='); if( index!=string::npos && string_trim(line.substr(0,index))=="id" ) { id = string_trim( line.substr(index+1) ); idraw = line; } if( id != idmatch ) continue; if( dmmatch && *dmmatch ) { string dm = string_trim( line.substr(0,line.find_first_of(" \t\r\n")) ); if( dm == dmmatch ) { if( !idraw.empty() ) { output << idraw << '\n'; idraw.clear(); } output << line << '\n'; } } else { output << line << '\n'; } } return 0; } std::string string_trim( const std::string& str ) { size_t index1 = str.find_first_not_of(" \t\r\n"); if(index1==std::string::npos) return std::string(); size_t index2 = str.find_last_not_of(" \t\r\n"); return str.substr( index1, index2+1-index1 ); } 代码还可以简化些,因为你根本没交代 id=aaaa 是不是可能被写成 id = aaaa;同一个 id 下是不是会出现重复的 dm, 等等。 另外 id=aaaaa id=bbbb id=cccc id=dddd id=aaaa 是 5 种id,你不会不识数吧? |
大家好
我有一组很大很大的数据,急需分析,但是以前从来没用过c++,不知道哪位高手能够帮我一把。
数据是这样的:
有四个id,一直循环,每个id里面又有四个不能分类,dm0,1,2,3。数字就是数据。
id=aaaaa
dm0 1 5 -2 6 1 7 12 9 24 22 24 40 73 72 73...............................-20(还有很多)
dm1 0 5 -1 7 1 3 9 1 6 4 8 10 20 20 23 9 13 12 16 9 14..................
dm2 0 4 0 7 -1 4 8 -1 3 1 6 3 12 15 17 2 7 4 7 4 7 6 10 ..........
dm3 0 0 0 1 2 11 37 62 73 82 73 63 52 48 32 19 11 5 -3 -8 -11 -9 -8 -8 -14............
id=bbbb
dm0 1 5 -2 6 1 7 12 9 24 22 24 40 73 72 73...............................-20(还有很多)
dm1 0 5 -1 7 1 3 9 1 6 4 8 10 20 20 23 9 13 12 16 9 14..................
dm2 0 4 0 7 -1 4 8 -1 3 1 6 3 12 15 17 2 7 4 7 4 7 6 10 ..........
dm3 0 0 0 1 2 11 37 62 73 82 73 63 52 48 32 19 11 5 -3 -8 -11 -9 -8 -8 -14............
id=cccc
dm0 1 5 -2 6 1 7 12 9 24 22 24 40 73 72 73...............................-20(还有很多)
dm1 0 5 -1 7 1 3 9 1 6 4 8 10 20 20 23 9 13 12 16 9 14..................
dm2 0 4 0 7 -1 4 8 -1 3 1 6 3 12 15 17 2 7 4 7 4 7 6 10 ..........
dm3 0 0 0 1 2 11 37 62 73 82 73 63 52 48 32 19 11 5 -3 -8 -11 -9 -8 -8 -14............
id=dddd
dm0 1 5 -2 6 1 7 12 9 24 22 24 40 73 72 73...............................-20(还有很多)
dm1 0 5 -1 7 1 3 9 1 6 4 8 10 20 20 23 9 13 12 16 9 14..................
dm2 0 4 0 7 -1 4 8 -1 3 1 6 3 12 15 17 2 7 4 7 4 7 6 10 ..........
dm3 0 0 0 1 2 11 37 62 73 82 73 63 52 48 32 19 11 5 -3 -8 -11 -9 -8 -8 -14............
id=aaaa
dm0........
dm1...
dm2...
dm3....
id=bbbb
.
.
id=cccc
..
.
.
.
.
.
(一直循环id但是数据不同)
我现在要把其中的数据提取出来,比如我只要id=aaaa 的所有数据,把id=bbbb,cccc,dddd都不要了。和,我只要id=aaaa里dm0的所有数据,其他的都不要,然后存在另一个file里面。哪位高手能帮我解决一下,继续code。谢谢谢谢谢谢!不知道有没有表达清楚。。数据实在太强大了。。
我想要的就是
id=aaaa
dm0..
dm1..
dm2..
dm3...
id=aaaa
.
.
.
和
id=aaaa
dm0...
id=aaaa
dm0..
.
.