HELP!!程序无错可是不能打开文件

spinsight 2014-01-21 06:15:10
那位大侠帮帮忙啊!
我有个程序读入很多的文件350个左右,要在这些文件中着值相近的文件。

我用了两个循环,大循环依次读入文件k,读一个关一个的,然后判断该文件是不是以前读过了,小循环再打开一个不同于前面的文件j,判断该文件是否已经用过了,如果不是就读入该文件的内容,关掉该文件。然后对比文件k与文件j读出的值是否相近。以此寻找所有与文件k内容相近的文件。

编辑无错,也可以运行,但是只能打开前24个文件,后面的文件就说不能打开文件,实际上文件都存在。

但是如果我将判断文件k是否用过和文件j是否用过都注释掉就没有问题,所有文件能读了。

用peak这个数组纪录每个k循环中的文件对应的所有的值相近的j循环的文件。如果我将该数组的值全部固定为0时程序也没有问题,所有文件能读。

下面是我用来判断文件是否已经用过了。所有在j循环中文件内容与k循环的文件内容值相近的都不再用到k循环中。

for(int ii=0;ii<NF;ii++)
{
for(int s=0;s<DEG;s++)
{
int tmp=peak[ii][s];
if(kf==tmp)
{
cout<<"skip kf= "<<tmp<<"###############"<<endl;
flag=1;
break;
}
}
if(flag==1)break;
}


不知道我说清楚没啊。程序有些长,不知道需不需要贴啊。

哪位帮我看看吧,我自己琢磨了好久都没有结果啊。不知道怎么回事。 如果需要我可以把程序全部贴上。
谢谢!!!!!




~
~
...全文
243 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
spinsight 2014-01-23
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
mac下也支持system函数和列目录命令ls吧。
我太初级了啊。这些还不怎么懂啊。用system操作一般命令可以但是不知道怎么在命令里面加变量啊。难道用shell的$,还是用%d %s之类的。有空再google一下。 多谢回复!
spinsight 2014-01-23
  • 打赏
  • 举报
回复
引用 7 楼 ALNG 的回复:
手都改软。没有改变业务逻辑,起码我没有有意要改变,除了上贴说明的文件指针关闭动作被跳过的问题。也学会授权分权,不要一把抓太多,分而治之,不管是面向对象编程还是结构化编程都需要这样。

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstddef>
#include <string>
#include <cstring>
using namespace std;



const int buff_size=600; //length for one record in input file
const int NF=352;
const int DEG=10;//degeneration for one record in input file
const int LEN=30; //length for file name
const float Ref_c=213.8450108f;
const float Ref_n=86.1806244f;
const float Er_ca=0.33f;
const float Er_n=0.4f;
const float Er_cx=0.15f;
const int Num_check=24;

struct Worker
{
	float fq_ca[NF], fq_n[NF], fq_cx[NF];
	int peak[NF][DEG];
	char preassgname[NF][LEN];
	char csname[NF][LEN];
	
	char res_tp[DEG][DEG],atom[DEG][DEG]; 
	char struc[DEG][DEG];    
	int nline;	

	void Init();
	
	bool HasPeak(int file_index)const;
	// please find the right name for the function
	bool Process1(int kf, char str_k[]);
	
	void Process2(int kf, char str_k[]);
}worker;


int main(void)
{
	char str_k[DEG];

	worker.Init();	  
	
	for(int kf=0;kf<NF;kf++)
	{
		cout<<"kf===="<<kf<<endl;
		
		if(worker.HasPeak(kf) && worker.Process1(kf, str_k))
			worker.Process2(kf, str_k);
				
	}
   
}


void Worker::Init()
{
	char buff[buff_size];
	FILE *fp;
	
	for(int k=0;k<NF;k++)
	{
		sprintf(preassgname[k],"pre_inputa_%d.dat",k);
		sprintf(csname[k],"cs_%d.dat",k);
		fq_ca[k]=0;
		fq_n[k]=0;
		fq_cx[k]=0;
		
		for(int i=0;i<DEG;i++)
		{
		   peak[k][i]=-1; 
		}
	
		FILE *fcs;		
		if((fcs=fopen(worker.csname[k],"r")) == NULL )
		{	
			continue;		
		}
	
		int mm;
		fgets(buff,buff_size,fp);
		sscanf(worker.csname[k],"%*3s%d%*4s",&mm); //extract the numerical number 文件序号统一
		sscanf(buff,"%f %f %f",&worker.fq_ca[mm],&worker.fq_n[mm],&worker.fq_cx[mm]);//get the chemical shift
		
		fclose(fp);
	
	}
}

// return true if 
// one of peak[file_index][0] to peak[file_index[DEG-1] is equal to kf
// 
bool Worker::HasPeak(int kf)const
{
	for(int ii=0;ii<NF;ii++)
	{
		for(int s=0;s<DEG;s++)
		{
			if(kf==peak[ii][s])
			{
				cout<<"skip kf= "<<kf<<"###############"<<endl;
				return true;
			}
		}
	}
	return false;
}

// return false if the kf'th file cannot be open
//
bool Worker::Process1(int kf, char str_k[])
{
	FILE * fp;
	int nrow=0, mm;
	char buff[buff_size];
	float tmp_ca, tmp_n, tmp_cx, file_flag;
	
	if((fp=fopen(worker.preassgname[kf],"r")) == NULL )//open the file for possi assign 
	{
		// you know fp is NULL, what's the point the output a NULL(fin_k) in your original program
		cout<<"can not open file for k "<<kf<<" preassgname="<<worker.preassgname[kf]<<endl;
		return false;
	}
	
	
	while((fgets(buff,buff_size,fp)!=NULL)) 
	{
		nrow++;
		switch(nrow){
		case 1:
			sscanf(buff,"%d",&nline);//the line number should be nline+1
			break;
		case 2:
			sscanf(buff,"%*s %*s %f %f %f",&tmp_ca,&tmp_n,&tmp_cx);
			
			file_flag=(worker.fq_ca[kf]-tmp_ca)*(worker.fq_n[kf]-tmp_n)*(worker.fq_cx[kf]-tmp_cx);
			
			if(fabs(file_flag)>0.001) 
			{
				cout<<"chemical shift file "<<csname[kf]<<" doesn't agree with the possible assignment file"
				    <<worker.preassgname[kf]<<endl;
				exit(-1);
			}
			break;
		case 3:
			mm=nrow-4;			
			sscanf(buff,"%s %s %s %*s %*s %*s %*d",res_tp[mm],struc[mm],atom[mm]); 
		}
	}//end while
	fclose(fp);
	strcpy(str_k,atom[0]);
	
	return true;
}

void Worker::Process2(int kf, char str_k[])
{
	FILE *fp;
	int nline2, ndis;
	char buff[buff_size];
	char com_res_tp[DEG][DEG],com_atom[DEG][DEG],com_struc[DEG][DEG];
			
	char *at1_2,*at2_2,*at3_2;
	
	for(int mr=0; mr<=(nline-3); mr++)
	{

		char *at1,*at2,*at3;
		int id_atom=strcmp(worker.atom[mr],str_k);
		if(id_atom!=0)continue;
		at1=strtok(worker.atom[mr],"-");
		at2=strtok(NULL,"-");
		at3=strtok(NULL,"-");

		for(int jf=(kf+1);jf<NF;jf++)
		{
			int flag_j=0;
			
			int id2_ca;
			int nrow2=0;
			float tmp_ca2=0;
			float tmp_n2=0;
			float tmp_cx2=0;
			

			if((fp=fopen(worker.preassgname[jf],"r")) == NULL )
			{
			
			continue;
			}

			for(int ik=0;ik<NF;ik++)
			{
			    
				for(int ss=0;ss<DEG;ss++)
				{
				
					int tmpp=worker.peak[ik][ss];
					
					if(jf==tmpp)
					{
						
						 flag_j=1;
						 break;
					}
				}
				if(flag_j==1)break;
			}
			if(flag_j==1)continue;
			
			while((fgets(buff,buff_size,fp)!=NULL))
			{
				nrow2++;
				if(nrow2==1)sscanf(buff,"%d",&nline2);
				
				
				if(nrow2==2)
				{
					sscanf(buff,"%*s %*s %f %f %f",&tmp_ca2,&tmp_n2,&tmp_cx2);
					float file_flag2=(worker.fq_ca[jf]-tmp_ca2)*(worker.fq_n[jf]-tmp_n2)*(worker.fq_cx[jf]-tmp_cx2);
					
					if(fabs(file_flag2)>0.001) 
					{
					cout<<"chemical shift file "<<worker.csname[jf]<<" doesn't agree with the possible assignment file\
					"<<worker.preassgname[jf]<<endl;
					exit(-1);
					}
				}

				if(nrow2>3)
				{
				int mm=nrow2-4;
				
				sscanf(buff,"%s %s %s %*s %*s %*s %*d",com_res_tp[mm],com_struc[mm],com_atom[mm]);  
				
				}
			}//end while

			fclose(fp);
			
			if((fabs(worker.fq_ca[kf]-worker.fq_ca[jf])<Er_ca)&&(fabs(worker.fq_n[kf]-worker.fq_n[jf])<Er_n))
			{
				for(int nr=0;nr<=(nline2-3);nr++)
				{
					
					at1_2=strtok(com_atom[nr],"-");
					at2_2=strtok(NULL,"-");
					at3_2=strtok(NULL,"-");
					id2_ca=strcmp(at3_2,"CA");
					
					int dif_atom=strcmp(at3_2,at3);
					int dif_res=strcmp(com_res_tp[nr],worker.res_tp[mr]);	
					if(id2_ca!=0)
					{
						if((dif_atom!=0)&&(dif_res==0))   
						{
							worker.peak[kf][ndis]=jf;
							ndis++;
							
							cout<<"jf="<<jf<<" different cx shows up !!!!!!!!!!!!!!!!!!!!!"<<endl;
							
							break;
						}
						if((dif_atom==0)&&(dif_res==0)&&(fabs(worker.fq_cx[kf]-worker.fq_cx[jf])<Er_cx)) 
						
						{
						
						cout<<"same worker.peak shows up at jf= "<<jf<<" kf="<<kf<<" !!!!!!!!!!!!!!!!!!!!!!!"<<endl;
						break;
						}//end if


					}	//end if id2
					else
					{
					
						if((dif_atom!=0)&&(dif_res==0)) 
						
						{
						
							cout<<"same worker.peak CANCA shows up at jf= "<<jf<<" kf="<<kf<<" !!!!!!!!!!!!!!!!!!!!!!!"<<endl;
							break;
						
						}//end if
					
					}//end else
					
				}
			}//end if

		}//end j loop
	}

}
谢谢大侠的改动。好好学习拜读一下。
spinsight 2014-01-23
  • 打赏
  • 举报
回复
引用 6 楼 ALNG 的回复:

		if((fin_k=fopen(global.preassgname[kf],"r")) == NULL )//open the file for possi assign 
		{
			cout<<"can not open file for k"<<"fin_k="<<fin_k<<" preassgname="<<global.preassgname[kf]<<endl;
			continue;
		}
		for(int ii=0;ii<NF;ii++)
		{
			for(int s=0;s<DEG;s++)
			{
				int tmp=global.peak[ii][s];
				if(kf==tmp)
				{
					cout<<"skip kf= "<<tmp<<"###############"<<endl;
					flag=1;
					break;
				}
			}
			if(flag==1)break;
		}
		if(flag==1)continue;
这里有问题, continue之前应该关闭前面打开的文件,这样导致那个文件句柄在关闭之前又被赋了新的值,而旧的文件句柄永久丢失,其对应的文件处于打开状态,下一次再打开就会失败。后面还有没有别的错误还不知道。
真是非常感谢,其实昨天我偶尔将那个判断文件是否用过的循环放到fopen前发现居然可以work了,然后就想可能是这个问题。文件打开了没关闭就跳过了。 今天就看到你的回复了,真是好厉害啊。我都试了几个星期了啊才蒙对,汗。非常感谢啊。
赵4老师 2014-01-22
  • 打赏
  • 举报
回复
mac下也支持system函数和列目录命令ls吧。
孩皮妞野 2014-01-22
  • 打赏
  • 举报
回复
手都改软。没有改变业务逻辑,起码我没有有意要改变,除了上贴说明的文件指针关闭动作被跳过的问题。也学会授权分权,不要一把抓太多,分而治之,不管是面向对象编程还是结构化编程都需要这样。

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstddef>
#include <string>
#include <cstring>
using namespace std;



const int buff_size=600; //length for one record in input file
const int NF=352;
const int DEG=10;//degeneration for one record in input file
const int LEN=30; //length for file name
const float Ref_c=213.8450108f;
const float Ref_n=86.1806244f;
const float Er_ca=0.33f;
const float Er_n=0.4f;
const float Er_cx=0.15f;
const int Num_check=24;

struct Worker
{
	float fq_ca[NF], fq_n[NF], fq_cx[NF];
	int peak[NF][DEG];
	char preassgname[NF][LEN];
	char csname[NF][LEN];
	
	char res_tp[DEG][DEG],atom[DEG][DEG]; 
	char struc[DEG][DEG];    
	int nline;	

	void Init();
	
	bool HasPeak(int file_index)const;
	// please find the right name for the function
	bool Process1(int kf, char str_k[]);
	
	void Process2(int kf, char str_k[]);
}worker;


int main(void)
{
	char str_k[DEG];

	worker.Init();	  
	
	for(int kf=0;kf<NF;kf++)
	{
		cout<<"kf===="<<kf<<endl;
		
		if(worker.HasPeak(kf) && worker.Process1(kf, str_k))
			worker.Process2(kf, str_k);
				
	}
   
}


void Worker::Init()
{
	char buff[buff_size];
	FILE *fp;
	
	for(int k=0;k<NF;k++)
	{
		sprintf(preassgname[k],"pre_inputa_%d.dat",k);
		sprintf(csname[k],"cs_%d.dat",k);
		fq_ca[k]=0;
		fq_n[k]=0;
		fq_cx[k]=0;
		
		for(int i=0;i<DEG;i++)
		{
		   peak[k][i]=-1; 
		}
	
		FILE *fcs;		
		if((fcs=fopen(worker.csname[k],"r")) == NULL )
		{	
			continue;		
		}
	
		int mm;
		fgets(buff,buff_size,fp);
		sscanf(worker.csname[k],"%*3s%d%*4s",&mm); //extract the numerical number 文件序号统一
		sscanf(buff,"%f %f %f",&worker.fq_ca[mm],&worker.fq_n[mm],&worker.fq_cx[mm]);//get the chemical shift
		
		fclose(fp);
	
	}
}

// return true if 
// one of peak[file_index][0] to peak[file_index[DEG-1] is equal to kf
// 
bool Worker::HasPeak(int kf)const
{
	for(int ii=0;ii<NF;ii++)
	{
		for(int s=0;s<DEG;s++)
		{
			if(kf==peak[ii][s])
			{
				cout<<"skip kf= "<<kf<<"###############"<<endl;
				return true;
			}
		}
	}
	return false;
}

// return false if the kf'th file cannot be open
//
bool Worker::Process1(int kf, char str_k[])
{
	FILE * fp;
	int nrow=0, mm;
	char buff[buff_size];
	float tmp_ca, tmp_n, tmp_cx, file_flag;
	
	if((fp=fopen(worker.preassgname[kf],"r")) == NULL )//open the file for possi assign 
	{
		// you know fp is NULL, what's the point the output a NULL(fin_k) in your original program
		cout<<"can not open file for k "<<kf<<" preassgname="<<worker.preassgname[kf]<<endl;
		return false;
	}
	
	
	while((fgets(buff,buff_size,fp)!=NULL)) 
	{
		nrow++;
		switch(nrow){
		case 1:
			sscanf(buff,"%d",&nline);//the line number should be nline+1
			break;
		case 2:
			sscanf(buff,"%*s %*s %f %f %f",&tmp_ca,&tmp_n,&tmp_cx);
			
			file_flag=(worker.fq_ca[kf]-tmp_ca)*(worker.fq_n[kf]-tmp_n)*(worker.fq_cx[kf]-tmp_cx);
			
			if(fabs(file_flag)>0.001) 
			{
				cout<<"chemical shift file "<<csname[kf]<<" doesn't agree with the possible assignment file"
				    <<worker.preassgname[kf]<<endl;
				exit(-1);
			}
			break;
		case 3:
			mm=nrow-4;			
			sscanf(buff,"%s %s %s %*s %*s %*s %*d",res_tp[mm],struc[mm],atom[mm]); 
		}
	}//end while
	fclose(fp);
	strcpy(str_k,atom[0]);
	
	return true;
}

void Worker::Process2(int kf, char str_k[])
{
	FILE *fp;
	int nline2, ndis;
	char buff[buff_size];
	char com_res_tp[DEG][DEG],com_atom[DEG][DEG],com_struc[DEG][DEG];
			
	char *at1_2,*at2_2,*at3_2;
	
	for(int mr=0; mr<=(nline-3); mr++)
	{

		char *at1,*at2,*at3;
		int id_atom=strcmp(worker.atom[mr],str_k);
		if(id_atom!=0)continue;
		at1=strtok(worker.atom[mr],"-");
		at2=strtok(NULL,"-");
		at3=strtok(NULL,"-");

		for(int jf=(kf+1);jf<NF;jf++)
		{
			int flag_j=0;
			
			int id2_ca;
			int nrow2=0;
			float tmp_ca2=0;
			float tmp_n2=0;
			float tmp_cx2=0;
			

			if((fp=fopen(worker.preassgname[jf],"r")) == NULL )
			{
			
			continue;
			}

			for(int ik=0;ik<NF;ik++)
			{
			    
				for(int ss=0;ss<DEG;ss++)
				{
				
					int tmpp=worker.peak[ik][ss];
					
					if(jf==tmpp)
					{
						
						 flag_j=1;
						 break;
					}
				}
				if(flag_j==1)break;
			}
			if(flag_j==1)continue;
			
			while((fgets(buff,buff_size,fp)!=NULL))
			{
				nrow2++;
				if(nrow2==1)sscanf(buff,"%d",&nline2);
				
				
				if(nrow2==2)
				{
					sscanf(buff,"%*s %*s %f %f %f",&tmp_ca2,&tmp_n2,&tmp_cx2);
					float file_flag2=(worker.fq_ca[jf]-tmp_ca2)*(worker.fq_n[jf]-tmp_n2)*(worker.fq_cx[jf]-tmp_cx2);
					
					if(fabs(file_flag2)>0.001) 
					{
					cout<<"chemical shift file "<<worker.csname[jf]<<" doesn't agree with the possible assignment file\
					"<<worker.preassgname[jf]<<endl;
					exit(-1);
					}
				}

				if(nrow2>3)
				{
				int mm=nrow2-4;
				
				sscanf(buff,"%s %s %s %*s %*s %*s %*d",com_res_tp[mm],com_struc[mm],com_atom[mm]);  
				
				}
			}//end while

			fclose(fp);
			
			if((fabs(worker.fq_ca[kf]-worker.fq_ca[jf])<Er_ca)&&(fabs(worker.fq_n[kf]-worker.fq_n[jf])<Er_n))
			{
				for(int nr=0;nr<=(nline2-3);nr++)
				{
					
					at1_2=strtok(com_atom[nr],"-");
					at2_2=strtok(NULL,"-");
					at3_2=strtok(NULL,"-");
					id2_ca=strcmp(at3_2,"CA");
					
					int dif_atom=strcmp(at3_2,at3);
					int dif_res=strcmp(com_res_tp[nr],worker.res_tp[mr]);	
					if(id2_ca!=0)
					{
						if((dif_atom!=0)&&(dif_res==0))   
						{
							worker.peak[kf][ndis]=jf;
							ndis++;
							
							cout<<"jf="<<jf<<" different cx shows up !!!!!!!!!!!!!!!!!!!!!"<<endl;
							
							break;
						}
						if((dif_atom==0)&&(dif_res==0)&&(fabs(worker.fq_cx[kf]-worker.fq_cx[jf])<Er_cx)) 
						
						{
						
						cout<<"same worker.peak shows up at jf= "<<jf<<" kf="<<kf<<" !!!!!!!!!!!!!!!!!!!!!!!"<<endl;
						break;
						}//end if


					}	//end if id2
					else
					{
					
						if((dif_atom!=0)&&(dif_res==0)) 
						
						{
						
							cout<<"same worker.peak CANCA shows up at jf= "<<jf<<" kf="<<kf<<" !!!!!!!!!!!!!!!!!!!!!!!"<<endl;
							break;
						
						}//end if
					
					}//end else
					
				}
			}//end if

		}//end j loop
	}
}
孩皮妞野 2014-01-22
  • 打赏
  • 举报
回复

		if((fin_k=fopen(global.preassgname[kf],"r")) == NULL )//open the file for possi assign 
		{
			cout<<"can not open file for k"<<"fin_k="<<fin_k<<" preassgname="<<global.preassgname[kf]<<endl;
			continue;
		}
		for(int ii=0;ii<NF;ii++)
		{
			for(int s=0;s<DEG;s++)
			{
				int tmp=global.peak[ii][s];
				if(kf==tmp)
				{
					cout<<"skip kf= "<<tmp<<"###############"<<endl;
					flag=1;
					break;
				}
			}
			if(flag==1)break;
		}
		if(flag==1)continue;
这里有问题, continue之前应该关闭前面打开的文件,这样导致那个文件句柄在关闭之前又被赋了新的值,而旧的文件句柄永久丢失,其对应的文件处于打开状态,下一次再打开就会失败。后面还有没有别的错误还不知道。
spinsight 2014-01-21
  • 打赏
  • 举报
回复
我还是把程序框架贴一下吧。我第一次编这么长的程序。 #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <cstddef> #include <string> #include <cstring> using namespace std; const int buff_size=600; //length for one record in input file const int NF=352; const int DEG=10;//degeneration for one record in input file const int LEN=30; //length for file name const float Ref_c=213.8450108; const float Ref_n=86.1806244; const float Er_ca=0.33; const float Er_n=0.4; const float Er_cx=0.15; const int Num_check=24; int main(void) { FILE *fin_k,*fcs,*fin_j; char *oneline; float *fq_ca,*fq_n,*fq_cx; int **peak; char **preassgname,**csname; int nline,nline2; oneline=new char[buff_size]; fq_ca=new float[NF]; fq_n=new float[NF]; fq_cx=new float[NF]; peak=new int *[NF]; preassgname=new char *[NF]; csname=new char *[NF]; for(int k=0;k<NF;k++) { peak[k]=new int[DEG]; preassgname[k]=new char[LEN]; csname[k]=new char[LEN]; sprintf(preassgname[k],"pre_inputa_%d.dat",k); sprintf(csname[k],"cs_%d.dat",k); fq_ca[k]=0; fq_n[k]=0; fq_cx[k]=0; for(int i=0;i<DEG;i++) { peak[k][i]=-1; } } ////////////// for(int j=0;j<NF;j++) { int mm; if((fcs=fopen(csname[j],"r")) == NULL ) { continue; } fgets(oneline,buff_size,fcs); sscanf(csname[j],"%*3s%d%*4s",&mm); //extract the numerical number 文件序号统一 sscanf(oneline,"%f %f %f",&fq_ca[mm],&fq_n[mm],&fq_cx[mm]);//get the chemical shift fclose(fcs); } ////////// for(int kf=0;kf<NF;kf++) { cout<<"kf===="<<kf<<endl; int flag=0; int nrow=0; int ndis=0; float tmp_ca,tmp_n,tmp_cx; char **res_tp,**atom; char **struc; char *str_k; atom=new char* [DEG]; res_tp=new char *[DEG]; struc=new char *[DEG]; str_k=new char[DEG]; for(int aa=0;aa<DEG;aa++) { res_tp[aa]=new char[DEG]; atom[aa]=new char[DEG]; struc[aa]=new char[DEG]; } if((fin_k=fopen(preassgname[kf],"r")) == NULL )//open the file for possi assign { cout<<"can not open file for k"<<"fin_k="<<fin_k<<" preassgname="<<preassgname[kf]<<endl; continue; } for(int ii=0;ii<NF;ii++) { for(int s=0;s<DEG;s++) { int tmp=peak[ii][s]; if(kf==tmp) { cout<<"skip kf= "<<tmp<<"###############"<<endl; flag=1; break; } } if(flag==1)break; } if(flag==1)continue; while((fgets(oneline,buff_size,fin_k)!=NULL)) { nrow++; if(nrow==1)sscanf(oneline,"%d",&nline);//the line number should be nline+1 if(nrow==2) { sscanf(oneline,"%*s %*s %f %f %f",&tmp_ca,&tmp_n,&tmp_cx); float file_flag=(fq_ca[kf]-tmp_ca)*(fq_n[kf]-tmp_n)*(fq_cx[kf]-tmp_cx); if(fabs(file_flag)>0.001) { cout<<"chemical shift file "<<csname[kf]<<" doesn't agree with the possible assignment file\ "<<preassgname[kf]<<endl; exit(-1); } } if(nrow>3) { int mm=nrow-4; sscanf(oneline,"%s %s %s %*s %*s %*s %*d",res_tp[mm],struc[mm],atom[mm]); } }//end while fclose(fin_k); strcpy(str_k,atom[0]); for(int mr=0;mr<=(nline-3);mr++) { char *at1,*at2,*at3; int id_atom=strcmp(atom[mr],str_k); if(id_atom!=0)continue; at1=strtok(atom[mr],"-"); at2=strtok(NULL,"-"); at3=strtok(NULL,"-"); for(int jf=(kf+1);jf<NF;jf++) { int flag_j=0; char **com_res_tp,**com_atom,**com_struc; char *at1_2,*at2_2,*at3_2; int id2_ca; int nrow2=0; float tmp_ca2=0; float tmp_n2=0; float tmp_cx2=0; com_res_tp=new char*[DEG]; com_atom=new char*[DEG]; com_struc=new char*[DEG]; for(int s=0;s<DEG;s++) { com_res_tp[s]=new char[DEG]; com_atom[s]=new char[DEG]; com_struc[s]=new char[DEG]; } if((fin_j=fopen(preassgname[jf],"r")) == NULL ) { continue; } for(int ik=0;ik<NF;ik++) { for(int ss=0;ss<DEG;ss++) { int tmpp=peak[ik][ss]; if(jf==tmpp) { flag_j=1; break; } } if(flag_j==1)break; } if(flag_j==1)continue; while((fgets(oneline,buff_size,fin_j)!=NULL)) { nrow2++; if(nrow2==1)sscanf(oneline,"%d",&nline2); if(nrow2==2) { sscanf(oneline,"%*s %*s %f %f %f",&tmp_ca2,&tmp_n2,&tmp_cx2); float file_flag2=(fq_ca[jf]-tmp_ca2)*(fq_n[jf]-tmp_n2)*(fq_cx[jf]-tmp_cx2); if(fabs(file_flag2)>0.001) { cout<<"chemical shift file "<<csname[jf]<<" doesn't agree with the possible assignment file\ "<<preassgname[jf]<<endl; exit(-1); } } if(nrow2>3) { int mm=nrow2-4; sscanf(oneline,"%s %s %s %*s %*s %*s %*d",com_res_tp[mm],com_struc[mm],com_atom[mm]); } }//end while fclose(fin_j); if((fabs(fq_ca[kf]-fq_ca[jf])<Er_ca)&&(fabs(fq_n[kf]-fq_n[jf])<Er_n)) { for(int nr=0;nr<=(nline2-3);nr++) { at1_2=strtok(com_atom[nr],"-"); at2_2=strtok(NULL,"-"); at3_2=strtok(NULL,"-"); id2_ca=strcmp(at3_2,"CA"); int dif_atom=strcmp(at3_2,at3); int dif_res=strcmp(com_res_tp[nr],res_tp[mr]); if(id2_ca!=0) { if((dif_atom!=0)&&(dif_res==0)) { peak[kf][ndis]=jf; ndis++; cout<<"jf="<<jf<<" different cx shows up !!!!!!!!!!!!!!!!!!!!!"<<endl; break; } if((dif_atom==0)&&(dif_res==0)&&(fabs(fq_cx[kf]-fq_cx[jf])<Er_cx)) { cout<<"same peak shows up at jf= "<<jf<<" kf="<<kf<<" !!!!!!!!!!!!!!!!!!!!!!!"<<endl; break; }//end if } //end if id2 else { if((dif_atom!=0)&&(dif_res==0)) { cout<<"same peak CANCA shows up at jf= "<<jf<<" kf="<<kf<<" !!!!!!!!!!!!!!!!!!!!!!!"<<endl; break; }//end if }//end else } }//end if for(int t=0;t<DEG;t++) { delete []com_atom[t]; delete []com_res_tp[t]; delete []com_struc[t]; } delete []com_atom; delete []com_res_tp; delete []com_struc; }//end j loop } for(int n=0;n<DEG;n++) { delete []res_tp[n]; delete []atom[n]; delete []struc[n]; } delete []res_tp; delete []atom; delete []struc; delete []str_k; } ///////////////////free the global variables for(int k=0;k<NF;k++) { delete []preassgname[k]; delete []csname[k]; delete []peak[k]; } delete []csname; delete []preassgname; delete []peak; delete []fq_ca; delete []fq_cx; delete []fq_n; delete []oneline; } 好心人帮我看看啊。多谢!!!!!
spinsight 2014-01-21
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
多谢回复! 我也曾想过用系统命令读文件的,但是我的文件名是个变量,随着循环变的。不知道怎么在mac系统实现啊。 能不能具体指导一下?
spinsight 2014-01-21
  • 打赏
  • 举报
回复
引用 1 楼 ALNG 的回复:
两两比较是350*349/2中组合,循环这样写 [code=c] const char * file_names[350]; // 初始化文件数组 for(int i=0; i<349; ++i) for(int j=i+1; j<350; ++j) compare_files(file_name[i], file_name[j]); [/code=c] 这样你根本不需要记住一个文件是否已经处理过了。改用这种思路,也许前面的bug就自动消除了。
我是这么写的,但因为被挑选出来的j不能再用到i循环中,所有要一个判断,而且被挑选出来的作为i文件相近值的j文件也不能再作为第i+1个文件的候选项。所以我就在j循环也作了一个判断。
孩皮妞野 2014-01-21
  • 打赏
  • 举报
回复
两两比较是350*349/2中组合,循环这样写 [code=c] const char * file_names[350]; // 初始化文件数组 for(int i=0; i<349; ++i) for(int j=i+1; j<350; ++j) compare_files(file_name[i], file_name[j]); [/code=c] 这样你根本不需要记住一个文件是否已经处理过了。改用这种思路,也许前面的bug就自动消除了。
赵4老师 2014-01-21
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧