我编写了一个批处理文件,它从用户那里获取输入以打开文件,然后显示一个菜单。然后,使用我编写的 .exe C 文件提示用户输入,并返回用户输入的数字,如果输入不是数字,则返回“-1”。无论我使用哪个选项,程序总是打开记事本,无论菜单选项如何。任何帮助都会很棒。我已经包含了批处理文件和我的 c 输入文件的代码。
我检查了我的 C 程序,它似乎返回了它应该返回的内容,因此可能只是批处理方面的格式问题。我列出来仅供引用。
批处理文件:
REM 1. Clear the screen
REM ------------------------------------------------------
cls
REM 2. Getting user input
REM ------------------------------------------------------
SET /p "FileToProcess=Please enter file(s) to process:"
REM 3. Checking for file
REM ------------------------------------------------------
IF EXIST "%FileToProcess%" (
cls
:MENU
ECHO 1. Open in Notepad
ECHO 2. Open in Word
ECHO 3. Open in Notepad ++
ECHO 4. Print
myChoice.exe
)
IF ERRORLEVEL 1 (
cd C:\Windows
notepad.exe %FileToProcess%
GOTO END
)
IF ERRORLEVEL 2 (
cd C:\Program Files (x86)\Microsoft Office\root\Office16
WINWORD.EXE %FileToProcess%
GOTO END
)
IF ERRORLEVEL 3 (
cd C:\Program Files\Notepad++
notepad++.exe %FileToProcess%
GOTO END
)
IF ERRORLEVEL 4 (
cd C:\Windows
notepad.exe /P %FileToProcess%
GOTO END
)
IF ERRORLEVEL -1 (
ECHO Sorry your input was not accepted!
pause
GOTO MENU
)
REM 4. Display error if no file found
REM -----------------------------------------------------
) ELSE (
ECHO File does not exist!
GOTO END
)
:END
C 输入程序代码:
#include <stdio.h>
#include <string.h>
#pragma warning(disable:4996)
// main
int main(void)
{
// variables
int num;
char userInput[10] = "";
// requesting input
printf("Please enter a menu option: ");
fgets(userInput, 81, stdin);
// checks input
if (sscanf(userInput, "%d", &num) == 1)
{
return num;
}
else
{
return -1;
}
return 0;
}
最佳答案
@ECHO OFF
SETLOCAL
REM 1. Clear the screen
REM ------------------------------------------------------
cls
REM 2. Getting user input
REM ------------------------------------------------------
SET /p "FileToProcess=Please enter file to process:" || exit /b 0
REM Strip double quotes
SET "FileToProcess=%FileToProcess:"=%"
REM 3. Checking for file
REM ------------------------------------------------------
IF NOT EXIST "%FileToProcess%" goto :FileNotExist
cls
:MENU
ECHO 1. Open in Notepad
ECHO 2. Open in Word
ECHO 3. Open in Notepad ++
ECHO 4. Print
myChoice.exe
IF %ERRORLEVEL% equ 1 (
cd /d "C:\Windows"
notepad.exe "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 2 (
cd /d "C:\Program Files (x86)\Microsoft Office\root\Office16"
WINWORD.EXE "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 3 (
cd /d "C:\Program Files\Notepad++"
notepad++.exe "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 4 (
cd /d "C:\Windows"
notepad.exe /P "%FileToProcess%"
) ELSE (
ECHO Sorry your input was not accepted!
pause
GOTO MENU
)
exit /b 0
REM 4. Display error if no file found
REM -----------------------------------------------------
:FileNotExist
>&2 ECHO File does not exist!
exit /b 1
- 路径双引号。
- 将
IF EXIST "%FileToProcess%"(
更改为如果不存在“%FileToProcess%”转到:FileNotExist
以避免需要成为一个大的括号 block 。 - 使用
else if
来避免使用大量goto :end
if errorlevel
更改为if %errorlevel%
实际情况 number 而不是 number 及更高。- 删除了不需要的
:end
标签。
关于c - 返回错误代码时批处理文件未打开正确的菜单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55438421/