Advertisement

C++ 读文件 多

阅读量:

在编写邻接表的过程中,通过文件读取方式导入数据时,发现每次操作都会额外读入一行内容,经过长时间排查后才确定问题所在。

复制代码
    void Graphic::buildGraph(std::string fname)
    {
    	std::ifstream fin(fname);
    	fin>>this->num;
    	this->graph=new FirstNode[num];
    	while(!fin.eof())//<======这里就是出事的地方
    	{
    		int firstVex,vex,weight;
    		fin>>firstVex>>vex>>weight;
    		VexNode *newVex=new VexNode(vex,weight);
    		insert(firstVex,newVex);//插入顶点信息
    	}
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    

出现错误的原因在于使用 !fin.eof() 来判断文件是否读取完成,然而 fin.eof() 的作用是检测是否遇到了文件结束的标记。当读取

全部评论 (0)

还没有任何评论哟~