void cd(char* name)//切换目录 { int fileInodeNum; PtrInode fileInode=(PtrInode)malloc(inodeSize);// if(strcmp(name,"..")==0)//返回上一级目录 { fseek(fp,superBlockSize+nowDir*inodeSize,SEEK_SET);//?? fread(fileInode,inodeSize,1,fp);//?? nowDir=fileInode->iparent; char temp[80]=""; int cnt=0; for(int i=strlen(path)-1;i>=0;i--) { cnt++; if(path[i]=='\\') break; } strncpy(temp,path,strlen(path)-cnt);//?? strcpy(path,temp); } else if(strcmp(name,'\\'==0))//返回根目录 { fseek(fp,superBlockSize,SEEK_SET); fread(fileInode,inodeSize,1,fp); nowDir=fileInode->iparent; strcpy(path,""); } else { char* subName; subName=strtok(name,"\\");//分隔 while(subName!=NULL) { fileInodeNum=findInodeNum(subName,1); if(fileInodeNum==-1) { cout<<"目录不存在!"<<endl; break; } else { nowDir=fileInodeNum; strcat(path,"\\");//连接字符串 strcat(path,subName); } subName=strtok(NULL,"\\"); } } free(fileInode); }改bug
时间: 2024-03-10 08:46:35 浏览: 66
在第12行,if语句中的判断条件应该改为`if(strcmp(name,"..")==0)`,将单引号改为双引号。
在第14行,`char temp[80]`的声明应该移到if语句外部,否则会导致变量作用域错误。
在第19行,`strncmp(temp,path,strlen(path)-cnt)`的第三个参数应该改为`cnt`,表示截取path的末尾cnt个字符。
修改后的代码如下:
```
void cd(char* name) //切换目录
{
int fileInodeNum;
PtrInode fileInode=(PtrInode)malloc(inodeSize);
if(strcmp(name,"..")==0) //返回上一级目录
{
fseek(fp,superBlockSize+nowDir*inodeSize,SEEK_SET);
fread(fileInode,inodeSize,1,fp);
nowDir=fileInode->iparent;
char temp[80];
int cnt=0;
for(int i=strlen(path)-1;i>=0;i--)
{
cnt++;
if(path[i]=='\\')
break;
}
strncpy(temp,path,strlen(path)-cnt);
temp[strlen(path)-cnt]='\0';
strcpy(path,temp);
}
else if(strcmp(name,"\\")==0) //返回根目录
{
fseek(fp,superBlockSize,SEEK_SET);
fread(fileInode,inodeSize,1,fp);
nowDir=fileInode->iparent;
strcpy(path,"");
}
else
{
char* subName;
subName=strtok(name,"\\");
while(subName!=NULL)
{
fileInodeNum=findInodeNum(subName,1);
if(fileInodeNum==-1)
{
cout<<"目录不存在!"<<endl;
break;
}
else
{
nowDir=fileInodeNum;
strcat(path,"\\");
strcat(path,subName);
}
subName=strtok(NULL,"\\");
}
}
free(fileInode);
}
```
阅读全文