2012-08-08 95 views
0

这里是我的主要的一些部分:从文本文件输入,显示不正确,为什么?

int main() { 
    Inventory Master; 
    bool flag; 

    Customer Bob("Bob", "CreditCard.txt"); 
    Customer Chris("Chris", "CreditCard.txt"); 
} 

这里是我的方法:

Customer::Customer(string n, string fileName) { 
    name = n; 
    ifstream Credit; 

    Credit.open(fileName.c_str(), ios::in); 

    while(Credit.good() && !Credit.eof()) { 
    Credit >> card >> balance >> ws; 
    cout << card <<"\t" << balance << endl; 

    } 


CreditCard _CC(int card, double balance); 
} 

这里是我的“CreditCard.txt文件:

12345 15.00 
32564 20.00 

我想要的方式要显示的信息有分配给Bob的第一行“12345 15.00”和分配给Chris的第2行,并且如果我创建新客户的新实例或对象,就这样做等等。目前实施它是继续分配“12345 15.00和32564 20.00”给鲍勃和克里斯。如果有人能够告诉我如何指示文本文件的某些行,以便鲍勃被分配到第1行,克里斯到第2行,并且当我将其添加到文本文件中时,更多的客户到其他行,我可以感谢帮助。

+0

尝试逐字读取文件,直到找到特殊字符,然后停止阅读。将这些字符分配给一个变量,然后将变量分配给Bob或Chris。您可以使用<或|管他呢。此外,不要忘记关闭文件;) – 2012-08-08 22:45:51

+0

嗯好吧,我看到它背后的想法。但是,你能否稍微向我展示一个例子,因为我已经将文本输入到卡片和平衡中了。我怎么能把它们分配给字符。我对这一点还很陌生,非常了解。 – tensuka 2012-08-08 22:51:58

+0

我在下面的答案中添加了一个例子。检查一下,它应该定义工作,但你必须在第一行成功读取后退出。 – 2012-08-08 22:58:06

回答

0

一切都发生在构造函数中。因此,如您所写,您的代码表示:虽然流处于良好状态,而不是文件结尾(关键点),请写入此处。

好吧,如果你仔细考虑一下,直到文件末尾达到Customer的每个实例为止。这不是你想要的。我可能会建议将该名称添加为每条记录的数据文件中的第一个字段。然后,您可以在文件中搜索正确的记录,假设您确保所有名称都是唯一定义的,然后按字符串将数据拉出。这样它不会每次都从头到尾阅读。我添加了“鲍勃”作为第一行的第一个字段,“克里斯”添加到第2行,并制作了string name = "Chris";。所以......

#include <string> 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main() 
{ 
    string tempStr; 
    string name = "Chris"; 
    ifstream Credit; 

    Credit.open("Info.txt", ios::in); 

    while(Credit.good() && !Credit.eof()) 
    { 
     getline(Credit, tempStr, ' ');//Reads the first records name field 
     cout << tempStr << endl; 
     if(name.compare(tempStr) == 0)//Compares the "name" to the field. 
     {       //If true they are the same 
      //Proceed to do reading and assignments with additional getline statements 
      cout << "Chris was matched the second time around!"; 
      Credit.setstate(ios::eofbit, true);//***Sets eof to true 
     } 
     else 
     { 
      Credit.ignore(50, '\n'); 
      //That should put Credit in the proper position to read the next name 
     } 
    } 

}

你这样做会导致问题的方式。如果你知道记录在文件中的位置,唯一可行的方法是。如果你有五条记录呢?当你到达第三个时,你将不得不ignore,或类似的,在你正在工作之前的所有领域。此外,人们可以很方便地从数据文件中读取打印。为每条记录提供标签(名称)的另一个原因。另外,你显然是using namespace std;,所以我也这样做了,但是它被折磨了。

+0

@Aj Munot:如果事实证明这是最有用的答案;表现出对投票的热爱,甚至更好地接受它。我的代表可以使用它! – ChiefTwoPencils 2012-08-09 03:15:16

+0

哈哈没问题,但我能够弄清楚如何做到这一点之前,我看到这anwser没有使用任何人的建议在这里给出。我仍然感谢你的帮助和反馈,所以我会为你投票,因为它是最丰富的,我从你的例子中学到了一些东西。 – tensuka 2012-08-09 03:34:16

0

istream.getline()http://www.cplusplus.com/reference/iostream/istream/getline/可能是你的答案。一次只读一行。

一点点在这里例如: http://www.cplusplus.com/forum/beginner/27799/

从我的旧homerworks的一个小例子:你在做鲍勃和克里斯

ifstream fin(fileName); 
char buffer[256]; 

int count = 0; 

if (fin.is_open()) 
{ 
    while (!fin.eof()) 
    { 
     fin.getline(buffer, 256); 
    } 
} 
+0

或者,使用字符串的自由函数getline()可能更易于使用。 HTTP:// WWW。cplusplus.com/reference/string/getline/ – jwismar 2012-08-08 22:59:04

+0

你是对的。这是一个古老的家庭作业,所以我们的老师总是希望我们使用字符数组而不是字符串呵呵。 – 2012-08-08 23:02:23