2011-04-25 84 views
1

我遇到了問題,從文件讀取字符串,然後從文件中讀取雙精度。我的教授建議我在每條輸入行之後放置一個特殊的getline,但它沒有奏效,我推斷這是該程序的問題。有更簡單的方法來參加雙打比賽嗎?輸入文件的閱讀字符串和雙打

例子是:

John Smith 
019283729102380 
300.00 
2000.00 
Andrew Lopez 
293481012100121 
400.00 
1500.00 

代碼讀取:

while(! infile.eof()) 
{ 
getline(infile,accname[count],'\n'); 
getline(infile, refuse, '\n'); 
getline(infile,accnum[count],'\n'); 
getline(infile, refuse, '\n'); 
infile>>currbal[count]; 
getline(infile, refuse, '\n'); 
infile>>credlim[count]; 
getline(infile, refuse, '\n'); 
count++; 
} 
+1

什麼沒有工作?你給了什麼投入,發生了什麼? – 2011-04-25 21:55:24

+0

看的IStream ::忽略 – sehe 2011-04-25 21:56:02

+0

@Oli,這真的只是凍結的程序,然後我評論了「拒絕」和infiles屬於雙打,和它跑了(同時輸出垃圾) – Sam 2011-04-25 22:01:41

回答

2

編輯:更新響應於OP的澄清。

根據您提供的樣品,這應該工作:

while(1) { 
    std::string s1; 
    if(!std::getline(infile, s1)) 
     break; 
    std::string s2; 
    if(!std::getline(infile, s2)) 
     break; 
    double d1, d2; 
    if(!(infile >> d1 >> d2)) 
     break; 
    accname[count] = s1; 
    accnum[count] = s2; 
    currball[count] = d1; 
    credlim[count] = d2; 
    count++; 
} 

這段代碼會像輸入工作:

Adam Sandler 
0112233 
5 100 
Ben Stein 
989898 
100000000 
1 

但它不會像輸入工作:

Adam Sandler 

0112233 

5 100 

Ben Stein 

989898 

100000000 

1 
0

我想解析的多條記錄時,羅布·亞當斯的,反應了一個小錯誤。首次提取d2之後,示例輸入流將包含\nBen Stein\n989898\n100000000\n1,因此到std::getline()下一呼叫將提取一個空字符串,而不是Ben Stein。所以似乎有必要在每個while循環迭代結束時使用一個空字符串。

我認爲下面的代碼示例提供了一個修復:

#include <iostream> 
#include <sstream> 

int main(int argc, char** argv) { 
    using std::stringstream; 
    stringstream infile(stringstream::in | stringstream::out); 

    infile << "John Smith\n"; 
    infile << "019283729102380\n"; 
    infile << "300.00\n"; 
    infile << "2000\n"; 
    infile << "Andrew Lopez\n"; 
    infile << "293481012100121\n"; 
    infile << "400.00\n"; 
    infile << "1500.00\n"; 

    std::string account_name; 
    std::string account_number; 
    double current_balance; 
    double credit_limit; 

    int count = 0; 

    while (std::getline(infile, account_name) && 
     std::getline(infile, account_number) >> current_balance >> 
     credit_limit) { 

    std::cout << account_name << std::endl; 
    std::cout << account_number << std::endl; 
    std::cout << current_balance << std::endl; 
    std::cout << credit_limit << std::endl; 

    /* 
    accname[count] = account_name; 
    accnum[count] = account_number; 
    currball[count] = current_balance; 
    credlim[count] = credit_limit; 
    */ 

    count++; 

    // Consume the leading newline character, which seprates the credit limit 
    // from the next account name, when infile contains multiple records. 
    // 
    // For example, after consuming the record for John Smith, the stream will 
    // contain: "\nAndrew Lopez\n293481012100121\n400.00\n1500.00\n". If you 
    // don't consume the leading newline character, calling std::getline() to 
    // parse the next account name will extract an empty string. 
    std::string blank; 
    std::getline(infile, blank); 
    } 

    return 0; 
} 
0
string str; 

while(!infile.eof()) 
{ 
    getline(infile, accname[count]);// you do not need '\n' it will always read to 
        // end of the line 

    getline(infile, accnum[count]); 

    getline(infile, str); 
    currbal[count] = atof(str.c_str()); 

    getline(infile, str); 
    credlim[count] = atof(str.c_str()); 

    count++; 
} 

\* 
for the file 
John Smith 
019283729102380 
300.00 
2000.00 
Andrew Lopez 
293481012100121 
400.00 
1500.00 
*\