+ -
当前位置:首页 → 问答吧 → 十分紧急 大神 求助啊

十分紧急 大神 求助啊

时间:2011-12-28

来源:互联网

这是data.txt中的内容
aaaa,bbbb,cccc,10.25,10
cccc,dddd,eeee,12.33,12
然后
ifstream fin;
fin.open("data.txt");
if(fin.is_open()==false)cout<<"error"<<endl; 

while()//主要是这里写什么才能使其读完这两行后退出while


getline(fin,aa1.time1,','); 
getline(fin,aa1.name,','); 
getline(fin,aa1.kind,','); 
fin>>aa1.price; 
fin.ignore(1,'\n');
fin>>aa1.number; 
fin.ignore(100,'\n');

Product p1(aa1.time1,aa1.name,aa1.price,aa1.kind,aa1.number);
a.p.push_back(p1);
}
fin.close();

作者: fireylee   发布时间: 2011-12-28

C/C++ code

static int nCount = 1while(nCount < 3)//主要是这里写什么才能使其读完这两行后退出while
{
nCount++;

}



我一般用这种计数的方式

作者: wtbike   发布时间: 2011-12-28

设个计数器就行了噻

作者: peng_weida   发布时间: 2011-12-28

但是用这种方式的话 如果我再程序运行过程中增加一行数据时就会读取不了了,因为这个是固定的
引用 1 楼 wtbike 的回复:

C/C++ code

static int nCount = 1;
while(nCount < 3)//主要是这里写什么才能使其读完这两行后退出while
{
nCount++;

}



我一般用这种计数的方式

作者: fireylee   发布时间: 2011-12-28

引用 3 楼 fireylee 的回复:

但是用这种方式的话 如果我再程序运行过程中增加一行数据时就会读取不了了,因为这个是固定的引用 1 楼 wtbike 的回复:

C/C++ code

static int nCount = 1;
while(nCount < 3)//主要是这里写什么才能使其读完这两行后退出while
{
nCount++;

}



我一般用这种计数的方式

你是说下次运行的时候不能再读取了吗?
你可以在nCount == 2 的时候再把nCount 置为1,这样不就可以再读一次了。

作者: wtbike   发布时间: 2011-12-28

C/C++ code
while(!fin.eof())

这个?

作者: c_losed   发布时间: 2011-12-28

不愧是大神啊 ,就是它
引用 5 楼 c_losed 的回复:

C/C++ code
while(!fin.eof())

这个?

作者: fireylee   发布时间: 2011-12-28

不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
whlie (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。

作者: zhao4zhong1   发布时间: 2011-12-28