从文件C ++中提取特定信息

时间:2015-06-22 23:26:06

标签: file store ifstream getline

我正在尝试使用c ++读取文件,但我只想存储某些内容。这就是我的文件的样子

stufff
stuff
more stuff
    .
    .
    .
data_I_want 32 34 45
data_I_want 52 22 34
stuff again
    .
    .
    .
end of file

到目前为止,我已编写此代码,但输出始终为0.

ifstream file;
file.open("stuff.txt");
string line;
double v;
if(file.is_open()){
    while(getline(file,line) && line.compare("data_I_want")){
            file>>v;
            cout<<v<<endl;
    }
    file.close();
}

1 个答案:

答案 0 :(得分:0)

行是data_I_want 32 34 45,而不仅仅是data_I_want。您可能想要查找以data_I_want

开头的行

以下是你如何使用字符串的get substring(取自here):

                                                                        activity                  | timestamp    | source   | source_elapsed
--------------------------------------------------------------------------------------------------+--------------+----------+----------------
                                                                               execute_cql3_query | 09:00:11,930 | 10.1.1.5 |              0
 Parsing select * from datachannel_6min where channel_id = 1042 order by time_start desc limit 1; | 09:00:11,930 | 10.1.1.5 |            102
                                                                              Preparing statement | 09:00:11,930 | 10.1.1.5 |            233
                                             Executing single-partition query on datachannel_6min | 09:00:11,931 | 10.1.1.5 |           1135
                                                                     Acquiring sstable references | 09:00:11,931 | 10.1.1.5 |           1163
                                                                      Merging memtable tombstones | 09:00:11,931 | 10.1.1.5 |           1185
                                                                  Key cache hit for sstable 14912 | 09:00:11,931 | 10.1.1.5 |           1223
                                                Seeking to partition indexed section in data file | 09:00:11,931 | 10.1.1.5 |           1230
                                                                  Key cache hit for sstable 14823 | 09:00:11,984 | 10.1.1.5 |          53805
                                                Seeking to partition indexed section in data file | 09:00:11,984 | 10.1.1.5 |          53851
                                                                  Key cache hit for sstable 14786 | 09:00:12,059 | 10.1.1.5 |         129027
                                                Seeking to partition indexed section in data file | 09:00:12,059 | 10.1.1.5 |         129060
                                                                  Key cache hit for sstable 14749 | 09:00:12,241 | 10.1.1.5 |         311521
                                                Seeking to partition indexed section in data file | 09:00:12,241 | 10.1.1.5 |         311558
                                                                  Key cache hit for sstable 14714 | 09:00:12,242 | 10.1.1.5 |         311843
                                                Seeking to partition indexed section in data file | 09:00:12,242 | 10.1.1.5 |         311849
                                           Partition index with 0 entries found for sstable 14913 | 09:00:12,242 | 10.1.1.5 |         312153
                                                Seeking to partition indexed section in data file | 09:00:12,242 | 10.1.1.5 |         312159
                                           Partition index with 0 entries found for sstable 14914 | 09:00:12,354 | 10.1.1.5 |         423820
                                                Seeking to partition indexed section in data file | 09:00:12,354 | 10.1.1.5 |         423849
                                           Partition index with 0 entries found for sstable 14916 | 09:00:12,354 | 10.1.1.5 |         424455
                                                Seeking to partition indexed section in data file | 09:00:12,354 | 10.1.1.5 |         424463
                                           Partition index with 0 entries found for sstable 14915 | 09:00:12,420 | 10.1.1.5 |         490468
                                                Seeking to partition indexed section in data file | 09:00:12,420 | 10.1.1.5 |         490501
                                           Partition index with 0 entries found for sstable 14917 | 09:00:12,492 | 10.1.1.5 |         561711
                                                Seeking to partition indexed section in data file | 09:00:12,492 | 10.1.1.5 |         561748
                                         Partition index with 146 entries found for sstable 14918 | 09:00:12,696 | 10.1.1.5 |         766248
                                                Seeking to partition indexed section in data file | 09:00:12,696 | 10.1.1.5 |         766306
                       Skipped 0/11 non-slice-intersecting sstables, included 0 due to tombstones | 09:00:12,696 | 10.1.1.5 |         766323
                                                      Merging data from memtables and 11 sstables | 09:00:12,696 | 10.1.1.5 |         766329
                                                               Read 2 live and 0 tombstoned cells | 09:00:12,773 | 10.1.1.5 |         842632
                                                                                 Request complete | 09:00:12,773 | 10.1.1.5 |         843350



                                                                        activity                  | timestamp    | source   | source_elapsed
--------------------------------------------------------------------------------------------------+--------------+----------+----------------
                                                                               execute_cql3_query | 09:05:46,255 | 10.1.1.4 |              0
                                                                  Message received from /10.1.1.4 | 09:05:46,250 | 10.1.1.5 |             21
                                             Executing single-partition query on datachannel_6min | 09:05:46,250 | 10.1.1.5 |            520
                                                                     Acquiring sstable references | 09:05:46,250 | 10.1.1.5 |            593
                                                                      Merging memtable tombstones | 09:05:46,250 | 10.1.1.5 |            609
                                                       Bloom filter allows skipping sstable 14912 | 09:05:46,250 | 10.1.1.5 |            630
                                                       Bloom filter allows skipping sstable 14823 | 09:05:46,250 | 10.1.1.5 |            641
                                                       Bloom filter allows skipping sstable 14786 | 09:05:46,250 | 10.1.1.5 |            647
                                                       Bloom filter allows skipping sstable 14749 | 09:05:46,250 | 10.1.1.5 |            654
                                                       Bloom filter allows skipping sstable 14714 | 09:05:46,251 | 10.1.1.5 |            757
                                                       Bloom filter allows skipping sstable 14913 | 09:05:46,251 | 10.1.1.5 |            763
                                                       Bloom filter allows skipping sstable 14914 | 09:05:46,251 | 10.1.1.5 |            770
                                                       Bloom filter allows skipping sstable 14916 | 09:05:46,251 | 10.1.1.5 |            776
                                                       Bloom filter allows skipping sstable 14915 | 09:05:46,251 | 10.1.1.5 |            783
                                                       Bloom filter allows skipping sstable 14917 | 09:05:46,251 | 10.1.1.5 |            789
 Parsing select * from datachannel_6min where channel_id = 1036 order by time_start desc limit 1; | 09:05:46,255 | 10.1.1.4 |            103
                                                                              Preparing statement | 09:05:46,255 | 10.1.1.4 |            223
                                                                     Sending message to /10.1.1.5 | 09:05:46,256 | 10.1.1.4 |            673
                                          Partition index with 17 entries found for sstable 14918 | 09:05:46,534 | 10.1.1.5 |         283815
                                                Seeking to partition indexed section in data file | 09:05:46,534 | 10.1.1.5 |         283851
                       Skipped 0/11 non-slice-intersecting sstables, included 0 due to tombstones | 09:05:46,534 | 10.1.1.5 |         283867
                                                       Merging data from memtables and 1 sstables | 09:05:46,534 | 10.1.1.5 |         283873
                                                               Read 2 live and 0 tombstoned cells | 09:05:46,571 | 10.1.1.5 |         321319
                                                                  Enqueuing response to /10.1.1.4 | 09:05:46,571 | 10.1.1.5 |         321439
                                                                     Sending message to /10.1.1.4 | 09:05:46,571 | 10.1.1.5 |         321613
                                                                  Message received from /10.1.1.5 | 09:05:46,579 | 10.1.1.4 |         323621
                                                               Processing response from /10.1.1.5 | 09:05:46,579 | 10.1.1.4 |         323730                                                               
                                                                                 Request complete | 09:05:46,579 | 10.1.1.4 |         324458



                                                                        activity                  | timestamp    | source   | source_elapsed
--------------------------------------------------------------------------------------------------+--------------+----------+----------------
                                                                               execute_cql3_query | 05:39:12,430 | 10.1.1.4 |              0
 Parsing select * from datachannel_6min where channel_id = 1030 order by time_start desc limit 1; | 05:39:12,430 | 10.1.1.4 |            164
                                                                              Preparing statement | 05:39:12,430 | 10.1.1.4 |            310
                                                                     Sending message to /10.1.1.6 | 05:39:12,431 | 10.1.1.4 |            829
                                                                  Message received from /10.1.1.4 | 05:39:12,432 | 10.1.1.6 |             19
                                             Executing single-partition query on datachannel_6min | 05:39:12,433 | 10.1.1.6 |            719
                                                                     Acquiring sstable references | 05:39:12,433 | 10.1.1.6 |            742
                                                                      Merging memtable tombstones | 05:39:12,433 | 10.1.1.6 |            769
                                                        Bloom filter allows skipping sstable 1476 | 05:39:12,433 | 10.1.1.6 |            830
                                            Partition index with 0 entries found for sstable 1475 | 05:39:12,433 | 10.1.1.6 |            904
                                                Seeking to partition indexed section in data file | 05:39:12,433 | 10.1.1.6 |            919
                                            Partition index with 2 entries found for sstable 1346 | 05:39:12,434 | 10.1.1.6 |           1403
                                                Seeking to partition indexed section in data file | 05:39:12,434 | 10.1.1.6 |           1425
                                            Partition index with 2 entries found for sstable 1472 | 05:39:12,434 | 10.1.1.6 |           1511
                                                Seeking to partition indexed section in data file | 05:39:12,434 | 10.1.1.6 |           1522
                                             Partition index with 0 entries found for sstable 586 | 05:39:12,434 | 10.1.1.6 |           1567
                                                Seeking to partition indexed section in data file | 05:39:12,434 | 10.1.1.6 |           1578
                                             Partition index with 146 entries found for sstable 5 | 05:39:12,434 | 10.1.1.6 |           2132
                                                Seeking to partition indexed section in data file | 05:39:12,434 | 10.1.1.6 |           2152
                        Skipped 0/6 non-slice-intersecting sstables, included 0 due to tombstones | 05:39:12,434 | 10.1.1.6 |           2177
                                                       Merging data from memtables and 5 sstables | 05:39:12,434 | 10.1.1.6 |           2192
                                                               Read 2 live and 0 tombstoned cells | 05:39:13,106 | 10.1.1.6 |         673858
                                                                  Enqueuing response to /10.1.1.4 | 05:39:13,106 | 10.1.1.6 |         674163
                                                                     Sending message to /10.1.1.4 | 05:39:13,107 | 10.1.1.6 |         674329
                                                                  Message received from /10.1.1.6 | 05:39:13,107 | 10.1.1.4 |         676882
                                                               Processing response from /10.1.1.6 | 05:39:13,107 | 10.1.1.4 |         677118
                                                                                 Request complete | 05:39:13,107 | 10.1.1.4 |         677344



                                                                        activity                  | timestamp    | source   | source_elapsed
--------------------------------------------------------------------------------------------------+--------------+----------+----------------
                                                                               execute_cql3_query | 05:40:41,322 | 10.1.1.4 |              0
 Parsing select * from datachannel_6min where channel_id = 1028 order by time_start desc limit 1; | 05:40:41,322 | 10.1.1.4 |            104
                                                                              Preparing statement | 05:40:41,322 | 10.1.1.4 |            257
                                                                     Sending message to /10.1.1.5 | 05:40:41,322 | 10.1.1.4 |            569
                                                                  Message received from /10.1.1.4 | 05:40:41,324 | 10.1.1.5 |              9
                                             Executing single-partition query on datachannel_6min | 05:40:41,324 | 10.1.1.5 |            401
                                                                     Acquiring sstable references | 05:40:41,324 | 10.1.1.5 |            410
                                                                      Merging memtable tombstones | 05:40:41,324 | 10.1.1.5 |            427
                                                       Bloom filter allows skipping sstable 15658 | 05:40:41,324 | 10.1.1.5 |            451
                                                       Bloom filter allows skipping sstable 15666 | 05:40:41,324 | 10.1.1.5 |            476
                                                       Bloom filter allows skipping sstable 15892 | 05:40:41,324 | 10.1.1.5 |            489
                                                       Bloom filter allows skipping sstable 15749 | 05:40:41,324 | 10.1.1.5 |            503
                                                       Bloom filter allows skipping sstable 15874 | 05:40:41,324 | 10.1.1.5 |            514
                                                       Bloom filter allows skipping sstable 15682 | 05:40:41,324 | 10.1.1.5 |            523
                                          Partition index with 14 entries found for sstable 14918 | 05:40:42,152 | 10.1.1.5 |         828365
                                                Seeking to partition indexed section in data file | 05:40:42,152 | 10.1.1.5 |         828406
                        Skipped 0/7 non-slice-intersecting sstables, included 0 due to tombstones | 05:40:42,152 | 10.1.1.5 |         828422
                                                       Merging data from memtables and 1 sstables | 05:40:42,152 | 10.1.1.5 |         828427
                                                               Read 2 live and 0 tombstoned cells | 05:40:42,300 | 10.1.1.5 |         976825
                                                                  Enqueuing response to /10.1.1.4 | 05:40:42,301 | 10.1.1.5 |         976984
                                                                  Message received from /10.1.1.5 | 05:40:42,301 | 10.1.1.4 |         978829
                                                                     Sending message to /10.1.1.4 | 05:40:42,301 | 10.1.1.5 |         977105
                                                               Processing response from /10.1.1.5 | 05:40:42,301 | 10.1.1.4 |         979018
                                                                                 Request complete | 05:40:42,301 | 10.1.1.4 |         979239