これは私の最初の質問ですので、役に立たない質問でしたら申し訳ありません。
ユーザーがいくつかのパラメーターを使用してコマンドラインからプログラムを呼び出すシミュレーターのこのプロジェクトがあります。のようにMYPROG [options] filename
。
ファイル名が有効であることを確認する必要があります (ディレクトリ)。さらに使用するために名前を取得する必要があります。
コードの一部を次に示します。
char* ExtrairNome(char* alvo){
char* teste = alvo;
char* nome = NULL;
int barras = 0;
while(strcmp(teste, "") != 0){ //look for "/"
if (teste[0] == '/') barras++;
teste++;
}
teste = alvo;
if (barras > 0){
while(barras > 0){ //remove everything leaving the "filename.ias"
if (teste[0] == '/') barras--;
teste++;
}
}
int i = 0;
char aux[strlen(teste) - 4];
while (strcmp(teste, ".ias")){ //remove the ".ias"
aux[i] = teste[0];
teste++;
i++;
}
printf("random %d\n", barras); //this line fixes the bug!!
aux[i] = '\0';
nome = aux;
return nome;
}
この関数は、完全なファイル名を含む文字列を受け取り、拡張子やパスなしで名前のみを返す必要があります。しかし、戻る前にいくつかの変数を印刷した場合にのみ機能します。その行を削除すると、関数は何も返しません。範囲と関係があると思いますが、よくわかりません。どうすればこれを修正できますか?