2011-04-25 46 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 
*\