C语言文件反向路径查询代码始终返回‘File not found’求助
C语言代码调试:始终返回“File not found”问题排查
我写了一段C语言代码,目标是返回指定文件的反向路径,但无论输入什么内容,代码始终输出“File not found!”。
原代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct TipoNo { char info; } TipoNo; typedef struct TipoArv { char name[100]; struct TipoArv* son; struct TipoArv* brother; struct TipoArv* dad; } TipoArv; // create TipoArv* cria(char c[]) { TipoArv* novo = (TipoArv*)malloc(sizeof(TipoArv)); strcpy(novo->name, c); novo->son = NULL; novo->brother = NULL; novo->dad = NULL; return novo; } // initialize void inicializa(TipoArv* a) { char t[1025] = "\\root"; TipoArv* raiz = cria(t); a->son = raiz; a->brother = NULL; a->dad = NULL; } // insert void insere(TipoArv* dad, TipoArv* subarv) { subarv->brother = dad->son; dad->son = subarv; subarv->dad = dad; } // remove void remover(TipoArv* raiz, char* arquivo) { if (raiz == NULL) return; TipoArv* atual = raiz->son; TipoArv* anterior = NULL; while (atual != NULL) { if (strcmp(atual->name, arquivo) == 0) { if (anterior == NULL) { raiz->son = atual->brother; } else { anterior->brother = atual->brother; } free(atual); return; } anterior = atual; atual = atual->brother; } atual = raiz->son; while (atual != NULL) { remover(atual, arquivo); atual = atual->brother; } } // search TipoArv* buscar(TipoArv* raiz, char name[]) { if (raiz == NULL) return NULL; if (strcmp(raiz->name, name) == 0) { return raiz; } TipoArv* atual = raiz->son; while (atual != NULL) { TipoArv* encontrado = buscar(atual, name); if (encontrado != NULL) { return encontrado; } atual = atual->brother; } return NULL; } // print int imprimir_caminho(TipoArv* raiz, char arquivo[]) { if (raiz == NULL) return 0; if (strcmp(raiz->name, arquivo) == 0) { if (raiz->son != NULL) { return imprimir_caminho(raiz->son, arquivo); } else { printf("%s", raiz->name); TipoArv* atual = raiz->dad; while (atual != NULL) { printf(" -> %s", atual->name); atual = atual->dad; } printf("\n"); return 1; } } TipoArv* atual = raiz->son; while (atual != NULL) { if (imprimir_caminho(atual, arquivo)) { return 1; } atual = atual->brother; } return 0; } int main() { TipoArv raiz; int n; char b[1025]; char acao[3], s1[1025], s2[1025]; scanf("%d", &n); scanf("%s", b); inicializa(&raiz); for (int k = 0; k < n; k++) { scanf("%s", acao); if (strcmp(acao, "-a") == 0) { scanf("%s %s", s1, s2); TipoArv* arquivo = cria(s1); TipoArv* pasta = buscar(&raiz, s2); if (pasta != NULL) { insere(pasta, arquivo); } } else if (strcmp(acao, "-m") == 0) { scanf("%s %s", s1, s2); TipoArv* arquivo = buscar(&raiz, s1); TipoArv* nova_pasta = buscar(&raiz, s2); if (arquivo != NULL && nova_pasta != NULL) { remover(arquivo->dad, s1); insere(nova_pasta, arquivo); } } else if (strcmp(acao, "-r") == 0) { scanf("%s", s1); TipoArv* j = buscar(&raiz, s1); if (j != NULL) { remover(&raiz, s1); } } } if (imprimir_caminho(&raiz, b) == 0) { printf("File not found!\n"); } else { imprimir_caminho(&raiz, b); } return 0; }
问题根源及修复方案
根节点查找起始错误
初始化后,实际的根节点是raiz.son(即\\root),但所有查找操作都从&raiz开始,而这个结构体的name字段未赋值,导致永远找不到\\root及其子节点。- 修复:在
main中调用buscar和imprimir_caminho时,传入raiz.son而非&raiz。
- 修复:在
打印函数的递归逻辑错误
找到目标节点后,代码错误地去递归遍历它的子节点,跳过了路径打印步骤。- 修复:删除判断子节点的分支,找到目标节点直接打印路径。
重复打印路径
main中当imprimir_caminho返回1时,重复调用了一次,导致路径打印两次。- 修复:只在返回0时打印错误信息,返回1时无需重复调用(因为
imprimir_caminho内部已经打印了路径)。
- 修复:只在返回0时打印错误信息,返回1时无需重复调用(因为
修复后的代码
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct TipoNo { char info; } TipoNo; typedef struct TipoArv { char name[100]; struct TipoArv* son; struct TipoArv* brother; struct TipoArv* dad; } TipoArv; // create TipoArv* cria(char c[]) { TipoArv* novo = (TipoArv*)malloc(sizeof(TipoArv)); strcpy(novo->name, c); novo->son = NULL; novo->brother = NULL; novo->dad = NULL; return novo; } // initialize void inicializa(TipoArv* a) { char t[1025] = "\\root"; TipoArv* raiz = cria(t); a->son = raiz; a->brother = NULL; a->dad = NULL; } // insert void insere(TipoArv* dad, TipoArv* subarv) { subarv->brother = dad->son; dad->son = subarv; subarv->dad = dad; } // remove void remover(TipoArv* raiz, char* arquivo) { if (raiz == NULL) return; TipoArv* atual = raiz->son; TipoArv* anterior = NULL; while (atual != NULL) { if (strcmp(atual->name, arquivo) == 0) { if (anterior == NULL) { raiz->son = atual->brother; } else { anterior->brother = atual->brother; } free(atual); return; } anterior = atual; atual = atual->brother; } atual = raiz->son; while (atual != NULL) { remover(atual, arquivo); atual = atual->brother; } } // search TipoArv* buscar(TipoArv* raiz, char name[]) { if (raiz == NULL) return NULL; if (strcmp(raiz->name, name) == 0) { return raiz; } TipoArv* atual = raiz->son; while (atual != NULL) { TipoArv* encontrado = buscar(atual, name); if (encontrado != NULL) { return encontrado; } atual = atual->brother; } return NULL; } // print int imprimir_caminho(TipoArv* raiz, char arquivo[]) { if (raiz == NULL) return 0; if (strcmp(raiz->name, arquivo) == 0) { printf("%s", raiz->name); TipoArv* atual = raiz->dad; while (atual != NULL) { printf(" -> %s", atual->name); atual = atual->dad; } printf("\n"); return 1; } TipoArv* atual = raiz->son; while (atual != NULL) { if (imprimir_caminho(atual, arquivo)) { return 1; } atual = atual->brother; } return 0; } int main() { TipoArv raiz; int n; char b[1025]; char acao[3], s1[1025], s2[1025]; scanf("%d", &n); scanf("%s", b); inicializa(&raiz); for (int k = 0; k < n; k++) { scanf("%s", acao); if (strcmp(acao, "-a") == 0) { scanf("%s %s", s1, s2); TipoArv* arquivo = cria(s1); TipoArv* pasta = buscar(raiz.son, s2); if (pasta != NULL) { insere(pasta, arquivo); } } else if (strcmp(acao, "-m") == 0) { scanf("%s %s", s1, s2); TipoArv* arquivo = buscar(raiz.son, s1); TipoArv* nova_pasta = buscar(raiz.son, s2); if (arquivo != NULL && nova_pasta != NULL) { remover(arquivo->dad, s1); insere(nova_pasta, arquivo); } } else if (strcmp(acao, "-r") == 0) { scanf("%s", s1); TipoArv* j = buscar(raiz.son, s1); if (j != NULL) { remover(raiz.son, s1); } } } if (imprimir_caminho(raiz.son, b) == 0) { printf("File not found!\n"); } return 0; }
内容的提问来源于stack exchange,提问作者Kruger
相关产品推荐
相关产品推荐

